监听手指长按在屏幕(view)的路径,具体一连串坐标

监听手指长按在view的路径,具体一连串坐标

UITouch只提供了点击屏幕的具体坐标。没有提供,手指的屏幕滑动,具体一连串坐标。
只好通过以下方法来,监听长按的路径。
直接上代码:

#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;
    //长按时间,多久后开始触发
    longPress.minimumPressDuration = 1;

}
-(void)longPressedOncell:(UILongPressGestureRecognizer *)longPress{
    CGPoint p = [(UILongPressGestureRecognizer *)longPress locationInView: touchView];
/*
UIGestureRecognizerStateBegan    可以理解为长按手势开始时触发
UIGestureRecognizerStateChanged 可以理解为长按手势开始改变时触发
UIGestureRecognizerStateEnded  可以理解为长按手势结束时触发
还有其他的一些longPress.state不一一列出来了,感兴趣的可以自行了解

*/
    if (longPress.state == UIGestureRecognizerStateChanged) {
        
        NSLog(@"长按手势开始改变p=%@",NSStringFromCGPoint(p));
       
    }
if (longPress.state == UIGestureRecognizerStateBegan) {
        
        NSLog(@"长按手势开始触发p=%@",NSStringFromCGPoint(p));
     
    }
else {
        NSLog(@"结束结束");
        
    }
    
}
如果文章帮到您,喜欢点个赞,谢谢您。
文章内容出错,记得留言,感激不尽。

你可能感兴趣的:(监听手指长按在屏幕(view)的路径,具体一连串坐标)