六种手势识别,你用了哪些?——董鑫

//1,轻击手势(TapGestureRecognizer)

//新建tap手势
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
//设置点击次数和点击手指数
tapGesture.numberOfTapsRequired = 1; //点击次数
tapGesture.numberOfTouchesRequired = 1; //点击手指数
[self.view addGestureRecognizer:tapGesture];

//轻击手势触发方法
-(void)tapGesture:(id)sender
{
    //轻击后要做的事情
}
//2,长按手势(LongPressGestureRecognizer)

//添加长摁手势
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
//设置长按时间
longPressGesture.minimumPressDuration = 0.5; //(2秒)
[self.view addGestureRecognizer:longPressGesture];

//常摁手势触发方法
-(void)longPressGesture:(id)sender
{
    UILongPressGestureRecognizer *longPress = sender;
    if (longPress.state == UIGestureRecognizerStateBegan)
    {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"长按触发" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
        [alter show];
    }
}
//3,轻扫手势(SwipeGestureRecognizer)

//添加轻扫手势
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //默认向右
[self.view addGestureRecognizer:swipeGesture];

//添加轻扫手势
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //默认向右
[self.view addGestureRecognizer:swipeGestureLeft];

//轻扫手势触发方法
-(void)swipeGesture:(id)sender
{
    UISwipeGestureRecognizer *swipe = sender;
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        //向左轻扫做的事情
    }
    if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
    {
        //向右轻扫做的事情
    }
}
//4,捏合手势(PinchGestureRecognizer)

//添加捏合手势
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
[self.view addGestureRecognizer:pinchGesture];

////捏合手势触发方法
-(void) pinchGesture:(id)sender
{
    UIPinchGestureRecognizer *gesture = sender;
    
    //手势改变时
    if (gesture.state == UIGestureRecognizerStateChanged)
    {
        //捏合手势中scale属性记录的缩放比例
        _imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
    }
    
    //结束后恢复
    if(gesture.state==UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:0.5 animations:^{
            _imageView.transform = CGAffineTransformIdentity;//取消一切形变
        }];
    }
}
//5,拖动手势(PanGestureRecognizer)

//添加拖动手势
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[self.view addGestureRecognizer:panGesture];

//拖动手势
-(void) panGesture:(id)sender
{
    UIPanGestureRecognizer *panGesture = sender;
    
    CGPoint movePoint = [panGesture translationInView:self.view];
    
    //做你想做的事儿
}
//6,旋转手势(RotationGestureRecognizer)

//添加旋转手势
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
[self.view addGestureRecognizer:rotationGesture];

//旋转手势
-(void)rotationGesture:(id)sender
{
    
    UIRotationGestureRecognizer *gesture = sender;
    
    if (gesture.state==UIGestureRecognizerStateChanged)
    {
        _imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);
    }
    
    if(gesture.state==UIGestureRecognizerStateEnded)
    {
        
        [UIView animateWithDuration:1 animations:^{
            _imageView.transform=CGAffineTransformIdentity;//取消形变
        }];
    }
    
}

 

转载于:https://www.cnblogs.com/sixindev/p/4489428.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值