【发布时间】:2012-11-12 12:33:20
【问题描述】:
有没有一种方法可以检测 UIButton 内部的触摸而不需要用户从屏幕上移开手指?
示例: 如果您有两个按钮,并且用户点击了左侧的一个,然后将手指拖到右侧,则应用程序必须识别出您正在点击右侧的按钮。
【问题讨论】:
标签: objective-c ios
有没有一种方法可以检测 UIButton 内部的触摸而不需要用户从屏幕上移开手指?
示例: 如果您有两个按钮,并且用户点击了左侧的一个,然后将手指拖到右侧,则应用程序必须识别出您正在点击右侧的按钮。
【问题讨论】:
标签: objective-c ios
您应该能够使用已经存在的按钮事件来执行此操作。例如“Touch Drag Outside”、“Touch Up Outside”、“Touch Drag Exit”等
只需注册这些活动,看看哪些活动适合您的需求。
【讨论】:
我会使用 UIViewController 自己实现。
而不是使用按钮。
在屏幕上放置两个视图(每个按钮一个)您可以制作这些按钮、imageViews 或只是 UIViews,但要确保它们有 userInteractionEnabled = NO;。
然后在 UIViewController 中使用方法touchesBegan 和touchesMoved。
我会在 viewController 中保存一些状态,比如...
BOOL trackTouch;
UIView *currentView;
那么如果 touchesBegan 在您的某个视图中...
-(void)touchesBegan... (can't remember the full name)
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
if (CGRectContainsPoint(firstView, point)) {
trackTouch = YES
//deal with the initial touch...
currentView = firstView; (work out which view you are in and store it)
} else if (CGRectContainsPoint(secondView, point)) {
trackTouch = YES
//deal with the initial touch...
currentView = secondView; (work out which view you are in and store it)
}
}
然后在 touchesMoved...
- (void)touchesMoved... (can't remember the full name)
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
if (CGRectContainsPoint(secondView, point) and currentView != secondView)) {
// deal with the touch swapping into a new view.
currentView = secondView;
} else if (CGRectContainsPoint(firstView, point) and currentView != firstView)) {
// deal with the touch swapping into a new view.
currentView = firstView;
}
}
反正就是这样。
【讨论】: