ios UIScrollview 获取触摸事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5216413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIScrollview getting touch events
提问by Suresh D
How can I detect touch points in my UIScrollView
? The touches delegate methods are not working.
如何检测我的UIScrollView
? 触摸委托方法不起作用。
回答by Suresh D
Set up a tap gesture recognizer:
设置点击手势识别器:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];
and you will get the touches in:
你会接触到:
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
CGPoint touchPoint=[gesture locationInView:scrollView];
}
回答by Vicky
You can make your own UIScrollview subclass and then you can implement the following:
您可以创建自己的 UIScrollview 子类,然后可以实现以下内容:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"DEBUG: Touches began" );
UITouch *touch = [[event allTouches] anyObject];
[super touchesBegan:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"DEBUG: Touches cancelled");
// Will be called if something happens - like the phone rings
UITouch *touch = [[event allTouches] anyObject];
[super touchesCancelled:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"DEBUG: Touches moved" );
UITouch *touch = [[event allTouches] anyObject];
[super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"DEBUG: Touches ending" );
//Get all the touches.
NSSet *allTouches = [event allTouches];
//Number of touches on the screen
switch ([allTouches count])
{
case 1:
{
//Get the first touch.
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
switch([touch tapCount])
{
case 1://Single tap
break;
case 2://Double tap.
break;
}
}
break;
}
[super touchesEnded:touches withEvent:event];
}
回答by Nevin
If we're talking about the points inside the scrollview then you can hook with the delegate method:
如果我们谈论的是滚动视图中的点,那么您可以使用委托方法挂钩:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
and inside the method, read the property:
在方法内部,读取属性:
@property(nonatomic) CGPoint contentOffset
from the scrollView to get the coordination.
从 scrollView 获得协调。
回答by Bruno Bieri
This works also on touch down event.
这也适用于触地事件。
In the currently as correct marked answeryou can get a touch point
only at a "Tap" event. This event seems only to fire on "finger up" not on down.
在当前正确标记的答案中,您touch point
只能在“点击”事件中获得一个。这个事件似乎只在“手指向上”而不是向下时触发。
From the comment of yuf in the same answer you can get the touch point
from the underlying view also within the UIScrollView
.
从同一个答案中 yuf 的评论中,您也可以touch point
从UIScrollView
.
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch
{
CGPoint touchPoint = [touch locationInView:self.view];
return TRUE; // since we're only interested in the touchPoint
}
According to Apple's documentationthe gestureRecognizer
does:
根据苹果的文档,gestureRecognizer
这样做:
Ask the delegate if a gesture recognizer should receive an object representing a touch.
询问代理手势识别器是否应该接收代表触摸的对象。
which means to me that I can decide if a gestureRecognizer
should recieve a touch or not.
这对我来说意味着我可以决定是否gestureRecognizer
应该接受触摸。