Last active
May 9, 2019 16:13
-
-
Save wenghengcong/ef5468c362d77f2f6bc89ca47ecdd7a1 to your computer and use it in GitHub Desktop.
事件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { | |
| if (!self.isUserInteractionEnabled || self.isHidden || self.alpha <= 0.01) return nil; | |
| CGFloat inset = 45.0f - 78.0f; | |
| CGRect touchRect = CGRectInset(self.bounds, inset, inset); | |
| if (CGRectContainsPoint(touchRect, point)) { | |
| for (UIView *subview in [self.subviews reverseObjectEnumerator]) { | |
| CGPoint convertedPoint = [subview convertPoint:point fromView:self]; | |
| UIView *hitTestView = [subview hitTest:convertedPoint withEvent:event]; | |
| if (hitTestView) { | |
| return hitTestView; | |
| } | |
| } | |
| return self; | |
| } | |
| return nil; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 父视图包含:三个subview self.scrollView\B\C | |
| // 实现:点击B、C也要同点击self.scrollView | |
| // 在父视图里实现以下方法 | |
| - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { | |
| UIView *hitTestView = [super hitTest:point withEvent:event]; | |
| if (hitTestView) { | |
| hitTestView = self.scrollView; | |
| } | |
| return hitTestView; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 自定义视图 | |
| @implementation CustomShakeView | |
| #pragma mark - Overrid Method | |
| - (BOOL)canBecomeFirstResponder { | |
| return YES; | |
| } | |
| - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { | |
| if (motion == UIEventSubtypeMotionShake) { | |
| CGFloat width = self.frame.size.width; | |
| CGFloat height = self.frame.size.height; | |
| UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)]; | |
| label.text = @"phone was shaked"; | |
| label.textAlignment = NSTextAlignmentCenter; | |
| [self addSubview:label]; | |
| } | |
| } | |
| - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { | |
| // nothing | |
| } | |
| - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event { | |
| // nothing | |
| } | |
| @end | |
| // 视图控制器 | |
| @interface ViewController () | |
| @property (nonatomic, strong) CustomShakeView *shakeView; | |
| @end | |
| @implementation ViewController | |
| - (void)viewDidLoad { | |
| self.shakeView = [[CustomShakeView alloc] initWithFrame:CGRectMake(0, 250, viewWidth, 60)]; | |
| self.shakeView.backgroundColor = [UIColor grayColor]; | |
| [self.view addSubview:_shakeView]; | |
| } | |
| - (void)viewWillAppear:(BOOL)animated { | |
| [super viewWillAppear:animated]; | |
| [self.shakeView becomeFirstResponder]; | |
| } | |
| - (void)viewWillDisappear:(BOOL)animated { | |
| [super viewWillDisappear:animated]; | |
| [self.shakeView resignFirstResponder]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment