UI梳理——手势

手势分类:

轻拍手势:UITapGestureRecognizer
轻扫手势:UISwipeGestureRecognizer
长按手势:UILongpreesGestureRecongnzier
平移手势:UIPanGestureRecognizer
缩放手势:UIPinchGestureRecognizer
旋转手势:UIRotationGestureRecognizer
屏幕边界手势:UIScreenEdgePanGestureRecognizer
轻拍手势:UITapGestureRecognizer

手势的创建:

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    //设置手势
    //1、触发手势的手指个数
    tap.numberOfTouchesRequired = 1;
    //2、触发手势的轻拍次数
    tap.numberOfTapsRequired = 2;
    //3、让imageView添加轻拍手势
    [imageView addGestureRecognizer:tap];
  
    [self.view addSubview:imageView];
    [tap release];

方法的实现:

//前三个的方法都是这么实现的
- (void)tapAction:(UITapGestureRecognizer *)tap {
    //获取手势关联的视图
    UIImageView *imageView = (UIImageView *)tap.view; 
    //修改图片
    [imageView setImage:[UIImage imageNamed:@"mao.jpg"]];
}

轻扫手势:UISwipeGestureRecognizer

//设置轻扫方向
swipe.direction = UISwipeGestureRecognizerDirectionDown;

长按手势:

UILongpreesGestureRecongnzier

//长按手势触发的最短时间,默认0.5
longpress.minimumPressDuration = 3.0;

以下三个手势不需要设置属性, 这里只写他们的方法如何实现:

平移手势:UIPanGestureRecognizer




- (void)panAction(UIPanGestureRecognizer *)pan{
    //获取手势关联的视图
    UIImageView *imageView = (UIImageView *)pan.view;
    CGPoint p = [pan translationInView:imageView];
    
    //改变imageView的形变属性
    //1、发生回弹
//    imageView.transform = CGAffineTransformMakeTranslation(p.x, p.y);
    
    //2、不发生回弹
    imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y);
//    CGPointZero 清零
    [pan setTranslation:CGPointZero inView:imageView];
}

缩放手势:UIPinchGestureRecognizer

- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
    //获取手势关联的视图
    UIImageView *imageView = (UIImageView *)pinch.view;
    
    //改变imageView的形变属性
    //1、发生回弹,再次改变的时候会回到原来的基础上,然后改变
//    imageView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
    //仿射变换
    //2、不发生回弹,根据CGAffineTransformScale 中的imageView.transform,进行缩放
    imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
    
    //如果不设定,会发生累加
    pinch.scale = 1;
}

旋转手势:UIRotationGestureRecognizer

- (void)rotationAction:(UIRotationGestureRecognizer *)rotate{
    //获取手势关联的视图
    UIImageView *imageView = (UIImageView *)rotate.view;
    //改变imageView的形变属性
    //1、发生回弹
//    imageView.transform = CGAffineTransformMakeRotation(rotate.rotation);
    //2、不发生回弹
    imageView.transform = CGAffineTransformRotate(imageView.transform, rotate.rotation);
    rotate.rotation = 0;
}

屏幕边界手势:UIScreenEdgePanGestureRecognizer

推荐阅读更多精彩内容