处理iOS设备上的触摸事件

时间:2013-01-21 09:52:29

标签: iphone ios ios4 gesture-recognition iphone-privateapi

我正在探索在iOS设备上处理触摸事件的方式。我推荐了这本书: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;
}

我遗失了哪些部分?另外,是否有另一种获取触摸事件信息并处理它们的方法?

3 个答案:

答案 0 :(得分:4)

更好的起点将在这里: 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

答案 1 :(得分:3)

您可以使用:

- (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子类获取触摸事件。

答案 2 :(得分:1)

您必须使用gesturerecognizer(例如uitapgesturerecognizeruiswipegesturerecognizerUITouch events