[button addTarget:self action:@selector(touchDown:event:) forControlEvents:UIControlEventTouchDown]; [button addTarget:self action:@selector(drag:event:) forControlEvents:UIControlEventTouchDragInside]; [button addTarget:self action:@selector(drag:event:) forControlEvents:UIControlEventTouchDragOutside]; [button addTarget:self action:@selector(touchUp:event:) forControlEvents:UIControlEventTouchUpInside]; [button addTarget:self action:@selector(touchUp:event:) forControlEvents:UIControlEventTouchUpOutside]; - (void)touchDown:(UIButton *)sender event:(UIEvent *)event { //begin only called when I move my finger } - (void)drag:(UIButton *)sender event:(UIEvent *)event { //called when I move my finger,after touchDown was called } - (void)touchUp:(UIButton *)sender event:(UIEvent *)event { }
我的应用程序的根视图控制器是tabbarviewcontroller,每个选项卡都是导航视图控制器.在聊天场景的viewWillAppear方法中,我隐藏了标签栏.
结果是,在设备上,当我触摸时,它没有被调用,当我移动我的手指时,它被调用.
注意:
>使用长按手势识别器也不起作用.
>如果我将按钮远离标签栏区域,它可以在设备上运行.
>在模拟器上,一切都很好.
我创建了一个示例项目:
https://drive.google.com/folderview?id=0B_9_90avvmZtRmRDeHFkbFJLaFk&usp=sharing
解决方法
Because a scroll view has no scroll bars,it must know whether a touch signals an intent to scroll versus an intent to track a subview in the content. To make this determination,it temporarily intercepts a touch-down event by starting a timer and,before the timer fires,seeing if the touching finger makes any movement. If the timer fires without a significant change in position,the scroll view sends tracking events to the touched subview of the content view. If the user then drags their finger far enough before the timer elapses,the scroll view cancels any tracking in the subview and performs the scrolling itself. Subclasses can override the
touchesShouldBegin:withEvent:inContentView:
,pagingEnabled,
andtouchesShouldCancelInContentView:
methods (which are called by the scroll view) to affect how the scroll view handles scrolling gestures.
如果您不想要此行为,可以将parentview.delaysContentTouches设置为NO您可能还需要将canCancelContentTouches设置为NO