zoukankan      html  css  js  c++  java
  • 源码0310-手势识别简单使用

     利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势

     
    UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
    UITapGestureRecognizer(敲击)
    UIPinchGestureRecognizer(捏合,用于缩放)
    UIPanGestureRecognizer(拖拽)
    UISwipeGestureRecognizer(轻扫)
    UIRotationGestureRecognizer(旋转)
    UILongPressGestureRecognizer(长按)
    //  ViewController.m
    //  10-手势识别简单使用
    #import "ViewController.h"
    
    @interface ViewController ()<UIGestureRecognizerDelegate>
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        [self setUpPinch];
        
        [self setUpRotation];
    
        [self setUpPan];
        
    }
    #pragma mark - 手势代理方法
    // 是否允许开始触发手势
    //- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
    //{
    //    return NO;
    //}
    
    // 是否允许同时支持多个手势,默认是不支持多个手势
    // 返回yes表示支持多个手势
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
        return YES;
    }
    
    // 是否允许接收手指的触摸点
    //- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    //    // 获取当前的触摸点
    //    CGPoint curP = [touch locationInView:self.imageView];
    //    if (curP.x < self.imageView.bounds.size.width * 0.5) {
    //        return NO;
    //    }else{
    //        return YES;
    //    }
    //}
    
    
    #pragma mark - 点按手势
    
    - (void)setUpTap
    {
        // 创建点按手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        
        tap.delegate = self;
        
        [_imageView addGestureRecognizer:tap];
    }
    
    - (void)tap:(UITapGestureRecognizer *)tap
    {
        NSLog(@"%s",__func__);
    }
    
    #pragma mark - 长按手势
    // 默认会触发两次
    - (void)setUpLongPress
    {
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
        
        [self.imageView addGestureRecognizer:longPress];
    }
    - (void)longPress:(UILongPressGestureRecognizer *)longPress
    {
        if (longPress.state == UIGestureRecognizerStateBegan) { 
            NSLog(@"%s",__func__);
        }
    }
    
    #pragma mark - 清扫
    - (void)setUpSwipe
    {
        // 默认轻扫的方向是往右
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
        
        swipe.direction = UISwipeGestureRecognizerDirectionUp;
        
        [self.imageView addGestureRecognizer:swipe];
        
        // 如果以后想要一个控件支持多个方向的轻扫,必须创建多个轻扫手势,一个轻扫手势只支持一个方向
        // 默认轻扫的方向是往右
        UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
        
        swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
        
        [self.imageView addGestureRecognizer:swipeDown];
    
        
    }
    
    - (void)swipe
    {
        NSLog(@"%s",__func__);
    }
    
    #pragma mark - 旋转手势
    - (void)setUpRotation
    {
        UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
        rotation.delegate = self;
        [self.imageView addGestureRecognizer:rotation];
    }
    // 默认传递的旋转的角度都是相对于最开始的位置
    - (void)rotation:(UIRotationGestureRecognizer *)rotation
    {
        self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
        // 复位
        rotation.rotation = 0;
        // 获取手势旋转的角度
        NSLog(@"%f",rotation.rotation);
    }
    
    #pragma mark - 捏合
    - (void)setUpPinch
    {
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
        pinch.delegate = self;
        [self.imageView addGestureRecognizer:pinch];
    }
    
    - (void)pinch:(UIPinchGestureRecognizer *)pinch
    {
        self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
        // 复位
        pinch.scale = 1;
    }
    
    #pragma mark - 拖拽
    - (void)setUpPan
    {  //创建手势识别器对象;with。。传入了处理的方法;
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; 
      //添加手势识别器到对应的View上; [self.imageView addGestureRecognizer:pan]; }
    - (void)pan:(UIPanGestureRecognizer *)pan { // 获取手势的触摸点 // CGPoint curP = [pan locationInView:self.imageView]; // 移动视图 // 获取手势的移动,也是相对于最开始的位置 CGPoint transP = [pan translationInView:self.imageView]; self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y); // 复位 [pan setTranslation:CGPointZero inView:self.imageView]; // NSLog(@"%@",NSStringFromCGPoint(curP)); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end

    本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
  • 相关阅读:
    数据类型比较(==)
    uniapp(一)
    小程序分包
    小程序网易云(五)
    java.lang的详解
    有哪些日常节省时间的诀窍?
    怎么把知乎的回答转化成自己的知识?
    linux下搭建hadoop环境
    linux下,免密码登录
    mac下创建用户及赋予sudo权限
  • 原文地址:https://www.cnblogs.com/laugh/p/6669336.html
Copyright © 2011-2022 走看看