ios-UIGesture扩展手势

来源:互联网 发布:华帝 方太 老板知乎 编辑:程序博客网 时间:2024/05/18 10:19

1、平滑手势

- (void)viewDidLoad {    [super viewDidLoad];    UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"01"]];    imageView.frame=CGRectMake(0, 0, 200, 200);    imageView.userInteractionEnabled=YES;//设置图像可交互    //创建一个平滑手势    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panClick:)];    [imageView addGestureRecognizer:pan];//将手势添加到图像视图中    [self.view addSubview:imageView];}//只有手指按住屏幕在屏幕上运动的时候就会执行这个函数也就是说只要手指在屏幕上发生任何坐标变化都会调用该函数-(void)panClick:(UIPanGestureRecognizer *)pan{    //获取移动的坐标相对于该视图对象    CGPoint pt=[pan translationInView:self.view];    NSLog(@"pt.x=%.2f, pt.y=%.2f",pt.x,pt.y);    //获取移动时的相对速度    CGPoint pv=[pan velocityInView:self.view];}
在imageView中取消手势

[imageView removeGestureRecognizer:pan];

2、滑动手势

UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeClick:)];    //设置滑动手势接受事件的类型    swipe.direction=UISwipeGestureRecognizerDirectionLeft;    //UISwipeGestureRecognizerDirectionLeft:向左滑动    //UISwipeGestureRecognizerDirectionRight:向右滑动    //UISwipeGestureRecognizerDirectionup:向上滑动    //UISwipeGestureRecognizerDirectionDown:向下滑动//我们这里也可以设置两个滑动手势 swipe.direction=UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;//如果我们只想添加一个点击函数的话,我们就要在这个点击函数中进行确定是滑向那一边的手势
3、长按手势

 UILongPressGestureRecognizer * longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(pressLong:)];    [imageView addGestureRecognizer:longPress];    //设置长按手势的时间,默认0.5s为长按手势,小于0.5秒的就为点击手势了    longPress.minimumPressDuration=0.5;-(void)pressLong:(UILongPressGestureRecognizer *)press{    //手指的状态对象,当长按达到0.5s的时候就会触发函数    if(press.state==UIGestureRecognizerStateBegan)    {        NSLog(@"状态开始");    }    //当手指离开屏幕时就是结束状态    else if(press.state==UIGestureRecognizerStateEnded)    {        NSLog(@"结束状态");    }}