ios – 触摸时没有调用的UIButton触摸操作,但触摸移动时调用

前端之家收集整理的这篇文章主要介绍了ios – 触摸时没有调用的UIButton触摸操作,但触摸移动时调用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
用户触摸DOWN UIButton时,您需要执行操作
[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

解决方法

滚动视图中是否包含此按钮(包括集合或tableview)?如果是这样,那么这正是预期的行为.系统等待您查看是否要点击按钮或拖动滚动视图.来自Apple:

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, and touchesShouldCancelInContentView: methods (which are called by the scroll view) to affect how the scroll view handles scrolling gestures.

如果您不想要此行为,可以将parentview.delaysContentTouches设置为NO您可能还需要将canCancelContentTouches设置为NO

原文链接:https://www.f2er.com/iOS/329412.html

猜你在找的iOS相关文章