UIGestureRecognizer

2018-01-07  本文已影响0人  Gui晨曦遇晓

UIGestureRecognizer

- a.Taps手势

-(void)viewDidLoad {
   [super viewDidLoad];
//    1.创建手势对象
   UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
   //2.设置手势的核心属性
   tap.numberOfTapsRequired = 5; //点几次
   tap.numberOfTouchesRequired = 1; //几个触摸点
   //3.将手势添加到某个视图中,当用户在该视图上做了相应的动作,就会触发手势,系统会捕获并调用手势的事件方法
   [self.myView addGestureRecognizer:tap];
}

- b.Swipe 手势 (轻扫)

-(void)viewDidLoad {
    [super viewDidLoad];
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]
    initWithTarget:self action:@selector(swipe:)];
    //上下左右 只 触发 左右
    swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:swipe];
}

- c. UILongPress 手势 长按

    -(void)viewDidLoad {
    [super viewDidLoad];
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
    //设置长按触发最小时间
    longPress.minimumPressDuration = 2;
    [self.view addGestureRecognizer:longPress];
}
-(void)longPress:(UILongPressGestureRecognizer*)gr {
    NSLog(@"%@",  NSStringFromCGPoint([gr locationInView:self.view]));
    if (gr.state == UIGestureRecognizerStateBegan) {
            NSLog(@"开始长按");
    }else if(gr.state == UIGestureRecognizerStateChanged) {
            NSLog(@"移动");
    }else if(gr.state == UIGestureRecognizerStateEnded) {
            NSLog(@"长按手势结束");
    }
}

- d. UIPan 手势 拖拽

    -(void)viewDidLoad {
    [super viewDidLoad];
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [self.view addGestureRecognizer:pan];
}
//
//
-(void)pan:(UIPanGestureRecognizer*)gr {
    NSLog(@"1--%@",NSStringFromCGPoint([gr locationInView:self.view]));
    NSLog(@"2--%@",NSStringFromCGPoint([gr translationInView:self.view]));

    //方式一
    if (gr.state == UIGestureRecognizerStateBegan) {
        self.startPos = self.imageView.center;
    }else {
        CGPoint translation = [gr translationInView:self.view];
        CGPoint center = self.imageView.center;
        center.x = self.startPos.x + translation.x;
        center.y = self.startPos.y + translation.y;
        self.imageView.center = center;
    }

    //方式二
    CGPoint translation = [gr translationInView:self.view];
    CGPoint center = self.imageView.center;
    center.x += translation.x;
    center.y += translation.y;
    self.imageView.center = center;
    [gr setTranslation:CGPointZero inView:self.view];

    //方式三
    if (gr.state == UIGestureRecognizerStateBegan) {
        //手势开始时 记录 起始位置
        self.startPos = [gr locationInView:self.view];
    }else if (gr.state == UIGestureRecognizerStateChanged){
        //获取本次 移动的位置
        CGPoint move = [gr locationInView:self.view];
        //图片的位置的等于当前位置 加上 移动位置-上次位置 的 便宜
        CGPoint center = self.imageView.center;
        center.x += move.x - self.startPos.x;
        center.y += move.y - self.startPos.y;
        self.imageView.center = center;
        //设置本次位置  为下次的 起始位置
        self.startPos = move;
    }
}

- e. UIPich 手势 捏合

    -(void)viewDidLoad {
    [super viewDidLoad];
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
    [self.view addGestureRecognizer:pinch];
}
-(void)pinch:(UIPinchGestureRecognizer*)gr {
    NSLog(@"速率 %.2f",gr.velocity);
    NSLog(@"缩放比 %.2f",gr.scale);
}

- f. UIRotation 手势 旋转

    -(void)viewDidLoad {
    [super viewDidLoad];
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
    [self.view addGestureRecognizer:rotation];
}
-(void)rotation:(UIRotationGestureRecognizer*)gr {
    NSLog(@"%.2f",gr.rotation);
}
上一篇 下一篇

猜你喜欢

热点阅读