iOS-UIGestureRecognizer(手势)

1、点击手势

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    tap.delegate = self;
    [self.imageView addGestureRecognizer:tap];
#pragma mark -
#pragma mark - 点击手势实现的方法
-(void)tapAction:(UITapGestureRecognizer *)tap{

    NSLog(@"点击了");
}

2、轻扫手势

 UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];
    swip.direction = UISwipeGestureRecognizerDirectionUp;

    [self.imageView addGestureRecognizer:swip];
#pragma mark -
#pragma mark - 轻扫手势实现方法
-(void)swipAction:(UISwipeGestureRecognizer *)swip{

    NSLog(@"清扫了");

}

3、长按手势

UILongPressGestureRecognizer *longPres = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longAction:)];
    [self.imageView addGestureRecognizer:longPres];
#pragma mark -
#pragma mark - 长按手势实现方法
//默认长按会有两次触发效果,即点击时和取消点击时都会调用实现的方法
-(void)longAction:(UILongPressGestureRecognizer *)longPres{

    //设置点击时处理
    if (longPres.state == UIGestureRecognizerStateBegan) {
          NSLog(@"长按了");
    }

}

4、捏合手势

UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinAction:)];
    [self.imageView addGestureRecognizer:pin];
#pragma mark -
#pragma mark - 捏合手势
- (void)pinAction:(UIPinchGestureRecognizer *)pin{

    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pin.scale, pin.scale);

    //复位
    pin.scale = 1;
}

5、旋转手势

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
    rotation.delegate = self;
    [self.imageView addGestureRecognizer:rotation];
#pragma mark -
#pragma mark - 旋转手势
-(void)rotationAction:(UIRotationGestureRecognizer *)rotaion{

    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotaion.rotation);
    //复位
    rotaion.rotation = 0;
}

6、拖拽手势

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    [self.imageView addGestureRecognizer:pan];
#pragma mark -
#pragma mark - 拖拽手势
-(void)panAction:(UIPanGestureRecognizer *)pan{

    //获取手势的移动,也是相对于最开始的位置
    CGPoint transP = [pan translationInView:self.imageView];
    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);


    //复位
    [pan setTranslation:CGPointZero inView:self.imageView];

}

常用代理方法:

//是否允许开始点击
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{


    return 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;
    }

    return YES;
}

代理方法都是可选的,想通过代理方法实现手势的某个效果 ,就把该手势设置代理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值