美文网首页
几种常用的iOS手势操作

几种常用的iOS手势操作

作者: Boy_iOS | 来源:发表于2016-06-05 10:10 被阅读2609次
    #import "ViewController.h"
    
    @interface ViewController ()<UIGestureRecognizerDelegate>
    
    /** 图片 */
    @property (nonatomic, weak) IBOutlet UIImageView *imageView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.imageView.userInteractionEnabled = YES;
        
        [self setUpTap];
        
        [self setUpLongPress];
        
        [self setUpSwipe];
        
        [self setUpRotation];
        
        [self setPinch];
        
        [self setPan];
    }
    
    /*------------长按手势----------------*/
    
    - (void)setUpTap {
        
        // 创建点击事件
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
        
        tap.delegate = self;
        
        [self.imageView addGestureRecognizer:tap];
        
    }
    
    // 点击事件
    - (void)tap {
        
        NSLog(@"%s", __func__);
    }
    
    /*------------长按手势----------------*/
    
    - (void)setUpLongPress {
        
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
        longPress.delegate = self;
    
        [self.imageView addGestureRecognizer:longPress];
    }
    
    // 长按手势事件
    - (void)longPress:(UILongPressGestureRecognizer *)sender {
        
        // 注意:长安手势可以触发两次事件,我们需要判断手势状态,如下
        if (sender.state == UIGestureRecognizerStateEnded) {
            
            NSLog(@"%s", __func__);
        }
    }
    
    /*------------轻扫手势----------------*/
    
    - (void)setUpSwipe {
        
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
        
        swipe.delegate = self;
    
        // 注意:如果以后想要一个控件支持多个方向的清扫,必须创建多个清扫手势,一个清扫手势只能支持一个方向
        // 默认清扫手势的方向是向右
        swipe.direction = UISwipeGestureRecognizerDirectionRight;
        
        [self.imageView addGestureRecognizer:swipe];
    }
    
    // 轻扫手势事件
    - (void)swipe:(UISwipeGestureRecognizer *)sender {
        
        NSLog(@"%s", __func__);
    }
    
    /*------------旋转手势----------------*/
    
    - (void)setUpRotation {
        
        UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
        
        rotation.delegate = self;
    
        [self.imageView addGestureRecognizer:rotation];
    }
    
    // 旋转手势事件
    - (void)rotation:(UIRotationGestureRecognizer *)sender {
        
        // 注意:手势传递的旋转角度都是相对于最开始的位置
        
        CGFloat rotation = sender.rotation;
        
        self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation);
        
        // 复位
        sender.rotation = 0;
        
        NSLog(@"%s", __func__);
    }
    
    /*------------捏合手势----------------*/
    
    - (void)setPinch {
        
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
        
        pinch.delegate = self;
    
        [self.imageView addGestureRecognizer:pinch];
    }
    
    // 捏合手势事件
    - (void)pinch:(UIPinchGestureRecognizer *)sender {
        
        CGFloat scale = sender.scale;
        
        self.imageView.transform = CGAffineTransformScale(self.imageView.transform, scale, scale);
        
        // 复位
        sender.scale = 1.0;
        
        NSLog(@"%s", __func__);
    }
    
    /*------------拖拽手势----------------*/
    
    - (void)setPan {
        
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        
        pan.delegate = self;
        
        [self.imageView addGestureRecognizer:pan];
    }
    
    // 拖拽手势事件
    - (void)pan:(UIPanGestureRecognizer *)sender {
        
        // 获取手势的移动,也是相对于最开始的位置
        CGPoint transP = [sender translationInView:self.imageView];
        
        self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);
        
        // 复位
        [sender setTranslation:CGPointZero inView:self.imageView];
        
        NSLog(@"%s", __func__);
    }
    
    #pragma mark - <UIGestureRecognizerDelegate>
    
    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
        
        return YES;
    }
    
    // 是否允许同时支持多个手势, 默认不支持多个手势
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
        
        return YES;
    }
    
    // 是否可以接收手指的触摸点(touch)
    // 实例:该方法可以控制让一个视图,左边能点击,右边不能点击
    //- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    //    
    //    CGPoint currentPoint = [touch locationInView:self.imageView];
    //    
    //    if (currentPoint.x > self.imageView.bounds.size.width * 0.5) {
    //        
    //        return YES;
    //    }
    //    
    //    return NO;
    //}
    
    @end
    
    

    相关文章

      网友评论

          本文标题:几种常用的iOS手势操作

          本文链接:https://www.haomeiwen.com/subject/gxcrdttx.html