Awe*_*eTN 5 touch coordinates ios
是否有可能获得触摸的x和y坐标?如果是这样,请有人提供一个非常简单的示例,其中坐标刚刚记录到控制台.
ico*_*ter 14
使用touchesBegan事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
NSLog(@"Touch x : %f y : %f", touchPoint.x, touchPoint.y);
}
Run Code Online (Sandbox Code Playgroud)
触摸开始时会触发此事件.
使用手势
在viewDidLoad:Method中注册您的UITapGestureRecognizer
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer:)];
[self.view setUserInteractionEnabled:YES];
[self.view addGestureRecognizer:tapGesture];
}
Run Code Online (Sandbox Code Playgroud)
设置tapGestureRecognizer功能
// Tap GestureRecognizer function
- (void)tapGestureRecognizer:(UIGestureRecognizer *)recognizer {
CGPoint tappedPoint = [recognizer locationInView:self.view];
CGFloat xCoordinate = tappedPoint.x;
CGFloat yCoordinate = tappedPoint.y;
NSLog(@"Touch Using UITapGestureRecognizer x : %f y : %f", xCoordinate, yCoordinate);
}
Run Code Online (Sandbox Code Playgroud)