我正在探索在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;
}
我遗失了哪些部分?另外,是否有另一种获取触摸事件信息并处理它们的方法?
答案 0 :(得分:4)
这些是你可以在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
(例如uitapgesturerecognizer,uiswipegesturerecognizer或UITouch events)