監(jiān)聽手指長按在view的路徑,具體一連串坐標
UITouch只提供了點擊屏幕的具體坐標扰法。沒有提供蛹含,手指的屏幕滑動,具體一連串坐標塞颁。
只好通過以下方法來浦箱,監(jiān)聽長按的路徑。
直接上代碼:
#import "ViewController.h"
@interface ViewController ()
{
UIView *touchView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self longPressedOnView];
}
-(void)longPressedOnView{
touchView = [[UIView alloc] init];
touchView.backgroundColor = [UIColor brownColor];
touchView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height / 2 - [UIScreen mainScreen].bounds.size.height / 4, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 2);
[self.view addSubview:touchView];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressedOncell:)];
[touchView addGestureRecognizer:longPress];
longPress.allowableMovement = NO;
//長按時間祠锣,多久后開始觸發(fā)
longPress.minimumPressDuration = 1;
}
-(void)longPressedOncell:(UILongPressGestureRecognizer *)longPress{
CGPoint p = [(UILongPressGestureRecognizer *)longPress locationInView: touchView];
/*
UIGestureRecognizerStateBegan 可以理解為長按手勢開始時觸發(fā)
UIGestureRecognizerStateChanged 可以理解為長按手勢開始改變時觸發(fā)
UIGestureRecognizerStateEnded 可以理解為長按手勢結束時觸發(fā)
還有其他的一些longPress.state不一一列出來了酷窥,感興趣的可以自行了解
*/
if (longPress.state == UIGestureRecognizerStateChanged) {
NSLog(@"長按手勢開始改變p=%@",NSStringFromCGPoint(p));
}
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"長按手勢開始觸發(fā)p=%@",NSStringFromCGPoint(p));
}
else {
NSLog(@"結束結束");
}
}