【发布时间】:2011-07-10 03:21:54
【问题描述】:
如何检测我的UIScrollView 中的触摸点?触摸委托方法不起作用。
【问题讨论】:
标签: objective-c ios cocoa-touch uiscrollview uitouch
如何检测我的UIScrollView 中的触摸点?触摸委托方法不起作用。
【问题讨论】:
标签: objective-c ios cocoa-touch uiscrollview uitouch
设置点击手势识别器:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];
你会接触到:
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
CGPoint touchPoint=[gesture locationInView:scrollView];
}
【讨论】:
您可以创建自己的 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];
}
【讨论】:
如果我们谈论的是滚动视图中的点,那么您可以使用委托方法:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
在方法内部,读取属性:
@property(nonatomic) CGPoint contentOffset
从scrollView获取坐标。
【讨论】:
这也适用于着陆事件。
在当前标记为正确的answer 中,您只能在“点击”事件中获得touch point。这个事件似乎只在“手指向上”时触发,而不是在向下时触发。
从同一答案中 yuf 的评论中,您也可以在 UIScrollView 中从底层视图中获取 touch point。
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch
{
CGPoint touchPoint = [touch locationInView:self.view];
return TRUE; // since we're only interested in the touchPoint
}
根据 Apple 的 documentation gestureRecognizer 是这样的:
询问代表手势识别器是否应该接收代表触摸的对象。
这对我来说意味着我可以决定 gestureRecognizer 是否应该收到触摸。
【讨论】: