UIScrollView touchesBegan
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIScrollView touchesBegan相关的知识,希望对你有一定的参考价值。
所以我想做的就是当用户触摸UIScrollView时播放声音。 UIScrollViewDelegate具有scrollViewWillBeginDragging:方法,但它只在touchMoved上调用。我想让它在touchBegan上调用。
尝试touchesBegan:withEvent:但它没有任何接触。有人有线索吗?
我认为你必须将UIScrollView子类化才能做到这一点。
touchesBegan:withEvent:仅发送到UIView的子类。您可以在控制器中实现touchesBegan:withEvent:对吧?如果是这样,它将无法在那里工作......
或者,如果您在UIScrollView中放置了UIView的子类(您编写的),您也可以从那里捕获touchesBegan事件(但仅当用户触摸该特定子视图时)。默认情况下,UIScrollView会将触摸传递给其子视图(请参阅touchesShouldBegin:withEvent:inContentView:在UIScrollView中)。
改为使用Tap Gesture识别器:
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch)];
[recognizer setNumberOfTapsRequired:1];
[recognizer setNumberOfTouchesRequired:1];
[scrollView addGestureRecognizer:recognizer];
要么
制作你的UIScrollView
的subClass并实现所有
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// If not dragging, send event to next responder
if (!self.dragging){
[self.nextResponder touchesBegan: touches withEvent:event];
}
else{
[super touchesEnded: touches withEvent: event];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
// If not dragging, send event to next responder
if (!self.dragging){
[self.nextResponder touchesBegan: touches withEvent:event];
}
else{
[super touchesEnded: touches withEvent: event];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// If not dragging, send event to next responder
if (!self.dragging){
[self.nextResponder touchesBegan: touches withEvent:event];
}
else{
[super touchesEnded: touches withEvent: event];
}
}
改为使用Tap Gesture识别器:
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch)];
[recognizer setNumberOfTapsRequired:1];
[recognizer setNumberOfTouchesRequired:1];
[scrollView addGestureRecognizer:recognizer];
您还可以在控制器中响应这些事件。为此,您必须定义此功能(在您的控制器中):
- (BOOL)canBecomeFirstResponder {
return YES;
}
然后,在'viewDidAppear'中,您需要调用'becomeFirstResponder':
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
qazxsw poi问题的最新更新(包括Zoom ScrollView源代码的链接+ UIScrollView内部的一些优秀解释)。另外,查看Apple更新的here示例。
您应该在UIScrollView上添加touchesBegan:withEvent :,而不是在UIScrollViewDelegate上添加。 UIScrollView是UIResponder的子类,具有触摸事件。
Rajneesh071在swift 4中回答将故事板中的scrollView自定义类更改为ScrollViewSuite
CustomScrollView
您需要子类化滚动视图,然后实现touchesBegan方法。您还需要将delayTouchDown属性设置为false。
以上是关于UIScrollView touchesBegan的主要内容,如果未能解决你的问题,请参考以下文章
UIScrollView 与 touchesBegan 冲突解决方法