iOS中怎样实现button在scrollview上的点击事件

2024-11-22 22:02:58
推荐回答(1个)
回答(1):

一举手个scrollview上有几个按钮在scrollview上add 了一个单击正散嫌事件

singletap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[singletap setNumberOfTapsRequired:1];
[scrollview addGestureRecognizer:singletap];
这样点击按钮,不会响应按钮的事掘笑件,直接跳到handleSingleTap去了?
单击事件不是应该先被直接单击的subview处理,如果没有处理才提交到supview处理嘛?
怎么才能让按钮响应单击事件?

使用
UIGestureRecognizerDelegate的一个方法判断点击的是哪个view,确定是否响应事件。
singletap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[singletap setNumberOfTapsRequired:1];
singletap.delegate = self;
[scrollview addGestureRecognizer:singletap];

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if(touch.view != scrollview){
return NO;
}else
return YES;
}