在 iOS 设备上处理触摸事件
Posted
技术标签:
【中文标题】在 iOS 设备上处理触摸事件【英文标题】:Handling Touch Events on iOS devices 【发布时间】:2013-01-21 09:52:28 【问题描述】:我正在探索的方式。我参考了这本书:ios Open Application Development。为此,我重写了 mouseDown、mouseUp 方法并简单地在警告框中显示坐标。然而,被覆盖的方法永远不会被调用。下面是从 UIView 派生的 newView 类的代码:
-(void) _mouseDown: (GSEventRef) event
CGPoint point =GSEventGetLocationInWindow(event);
UIAlertView* alert;
NSString* alertString =[NSString stringWithFormat: @"%f %f", point.x, point.y];
alert = [[UIALertView alloc] initWithTitle:@"Success" message:alertString delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
-(BOOL) canHandleGestures
return YES;
有没有我遗漏的部分?另外,是否有其他方法可以获取有关触摸事件的信息并进行处理?
【问题讨论】:
【参考方案1】:更好的起点是这里: https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/multitouch_background/multitouch_background.html
这些是您可以在 UIView 子类中覆盖的方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
【讨论】:
【参考方案2】:你可以使用:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
用于从 UIView 子类获取触摸事件。
【讨论】:
【参考方案3】:您必须使用gesturerecognizer
(如uitapgesturerecognizer、uiswipegesturerecognizer 或UITouch events)
【讨论】:
以上是关于在 iOS 设备上处理触摸事件的主要内容,如果未能解决你的问题,请参考以下文章