事件运用案例
2017-04-03 本文已影响24人
前年的邂逅_Jerry
一、视图遮挡按钮,按钮无法响应点击事件。
如图一所示,红色按钮在蓝色视图的下面,如何来点击蓝色视图,让红色按钮也能响应他的点击事件呢?
重写蓝色视图的pointInside方法,使其返回NO.其原理在事件响应原理已经做出了解释。
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
NSLog(@"%s",__func__);
return NO;
//return [super pointInside:point withEvent:event];
}
二、将按钮添加到scollView上,当按钮出现高亮显示的时候无法滑动scrollView,当滑动scrollView的时候无法显示按钮的高亮状态。
图二.png1、delaysContentTouches
默认状态下这个属性为YES,当设置为NO的情况下,手指触摸scrollView上的按钮区域,按钮的高亮状态会立马显示,但是scrollView的滑动事件被取消。
2、canCancelContentTouches
默认状态下这个属性为YES,它会自动调用- (BOOL)touchesShouldCancelInContentView:(UIView *)view;
这个方法。如果为NO,则不会调用这个方法。如果touchesShouldCancelInContentView返回为NO,scrollView不会传递touch事件给subViews。
要想scrollView上的按钮同时响应scrollView的滑动事件和按钮的高亮事件,必须做如下设置:
//设置为NO,当手指在按钮上的时候,滑动scrollView时,会立马响应按钮的高亮状态,
//如果该属性设置为YES,那么要等手指放在按钮上停留一会按钮才会响应高亮状态。
scrollView.delaysContentTouches = NO;
scrollView.canCancelContentTouches = YES;
重写scrollView方法
- (BOOL)touchesShouldCancelInContentView:(UIView *)view{
[super touchesShouldCancelInContentView: view];
return YES;
}
三、如果将scrollView下面的子视图也能响应touch事件,那么必须重写scrollView的touch方法。如下所示:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"%s",__func__);
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"%s",__func__);
[self.nextResponder touchesMoved:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"%s",__func__);
[self.nextResponder touchesCancelled:touches withEvent:event];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"%s",__func__);
[self.nextResponder touchesEnded:touches withEvent:event];
}
四、UIScollView上的触摸案例
2017-04-03 21_57_26.gif代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 100, WIN_WIDTH - 60, (WIN_WIDTH - 60) / 2)];
scrollView.backgroundColor = [UIColor whiteColor];
scrollView.pagingEnabled = YES;
scrollView.clipsToBounds = NO;
scrollView.contentSize = CGSizeMake(5 * (WIN_WIDTH - 60), (WIN_WIDTH - 60) / 2);
[self.view addSubview:scrollView];
NSArray * imageArr = @[@"0",@"1",@"2",@"3",@"4"];
for (NSInteger i = 0; i < 5; i++) {
UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(i * (WIN_WIDTH - 60) + 20,0, WIN_WIDTH - 80, (WIN_WIDTH - 60) / 2)];
imgView.image = [UIImage imageNamed:imageArr[i]];
[scrollView addSubview:imgView];
}
}
五、UIScreenEdgePanGestureRecognizer
- (void)createScreenGestureView {
UIScreenEdgePanGestureRecognizer *screenEdgePanGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
NSArray *gestureArray = self.navigationController.view.gestureRecognizers;
for (UIGestureRecognizer *gesture in gestureArray) {
if ([gesture isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
[gesture requireGestureRecognizerToFail:screenEdgePanGesture];
}
}
screenEdgePanGesture.edges = UIRectEdgeRight;
[self.showView addGestureRecognizer:screenEdgePanGesture];
}
#pragma mark - event response
- (void)panAction:(UIScreenEdgePanGestureRecognizer *)gesture
{
//找到手势作用的视图
UIView *view = [self.view hitTest:[gesture locationInView:gesture.view] withEvent:nil];
NSLog(@"view.tag %ld, gesture.view %ld", view.tag, gesture.view.tag);
if (UIGestureRecognizerStateBegan == gesture.state || UIGestureRecognizerStateChanged == gesture.state) {
CGPoint translationPoint = [gesture translationInView:gesture.view];
_backgroundView.center = CGPointMake(center_x+translationPoint.x, center_y);
} else {
[UIView animateWithDuration:.3f animations:^{
_backgroundView.center = CGPointMake(center_x, center_y);
}];
}
}
六、scrollView上添加UISlider,如果scrollView的contentSize比frame要大,那么滑动UISlider会滑动scrollView。设置delaysContentTouches属性为NO。
代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
MyScrollView * scrollView = [[MyScrollView alloc] initWithFrame:CGRectMake(0, 0, WIN_WIDTH, WIN_HEIGHT)];
scrollView.contentSize = CGSizeMake(WIN_WIDTH * 2.0, WIN_HEIGHT * 2.0);
[self.view addSubview:scrollView];
scrollView.delaysContentTouches = NO;
UISlider * slider = [[UISlider alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];
[scrollView addSubview:slider];
}