iOS 手势事件


UITapGestureRecognizer轻拍手势
UISwipeGestureRecognizer轻扫手势
UILongPressGestureRecognizer长按手势
UIPanGestureRecognizer平移手势
UIPinchGestureRecognizer捏合(缩放)手势
UIRotationGestureRecognizer旋转手势
UIScreenEdgePanGestureRecognizer屏幕边缘平移

UITapGestureRecognizer(轻拍手势)


    //创建手势 使用initWithTarget:action:的方法创建

    UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapView:)];

    

    //设置属性

    //tap 手势一共两个属性,一个是设置轻拍次数,一个是设置点击手指个数

    //设置轻拍次数

    tap.numberOfTapsRequired =2;

    //设置手指字数

    tap.numberOfTouchesRequired =2;

    

    //别忘了添加到testView上

    [_testViewaddGestureRecognizer:tap];

    


#pragma mark --- UITapGestureRecognizer 轻拍手势事件 ---

-(void)tapView:(UITapGestureRecognizer *)sender{

    //设置轻拍事件改变testView的颜色

    _testView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];

}



UISwipeGestureRecognizer (轻扫手势)

我们仍然使用testView这一场景

//创建手势

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipeView:)];

    //设置属性,swipe也是有两种属性设置手指个数及轻扫方向

    swipe.numberOfTouchesRequired =2;

    //设置轻扫方向(默认是从左往右)

    //direction是一个枚举值有四个选项,我们可以设置从左往右,从右往左,从下往上以及从上往下

    //设置轻扫方向(默认是从左往右)

    swipe.direction =UISwipeGestureRecognizerDirectionLeft;

   [_testViewaddGestureRecognizer:tap];


#pragma mark swipe轻扫手势事件

-(void)swipeView:(UISwipeGestureRecognizer *)sender{

    _testView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];

}



UILongPressGestureRecognizer(长按手势)

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPress:)];

    //属性设置

    //最小长按时间

    longPress.minimumPressDuration =2;

    [_testView addGestureRecognizer:longPress];



#pragma mark langPress 长按手势事件

-(void)longPress:(UILongPressGestureRecognizer *)sender{

    //进行判断,在什么时候触发事件

    if (sender.state ==UIGestureRecognizerStateBegan) {

        NSLog(@"长按状态");

        //改变testView颜色

        _testView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];

    }

}



UIPanGestureRecognizer(平移手势)

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(panView:)];

    

    [_testView addGestureRecognizer:pan];


#pragma mark pan   平移手势事件

-(void)panView:(UIPanGestureRecognizer *)sender{

    CGPoint point = [sender translationInView:_testView];

    //    sender.view.transform = CGAffineTransformMake(1, 0, 0, 1, point.x, point.y);

    

    //平移一共两种移动方式

    //第一种移动方法:每次移动都是从原来的位置移动

    //    sender.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);

    

    //第二种移动方式:以上次的位置为标准(移动方式 第二次移动加上第一次移动量)

    sender.view.transform =CGAffineTransformTranslate(sender.view.transform, point.x, point.y);

    //增量置为o

    [sender setTranslation:CGPointZeroinView:sender.view];

    

    _testView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1]; 

}


UIScreenEdgePanGestureRecognizer(屏幕边缘平移手势)

    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizeralloc]initWithTarget:selfaction:@selector(screenEdgePanView:)];

    

    //注意:,使用屏幕边界平移手势,需要注意两点

    //1. 视图位置(屏幕边缘)

    //2. 设置edges属性

    // 设置屏幕边缘手势支持方法

    screenEdgePan.edges = UIRectEdgeLeft;

    //属性设置

    [_testView addGestureRecognizer:screenEdgePan];


#pragma mark screenEdgePan 屏幕边界平移事件

-(void)screenEdgePanView:(UIScreenEdgePanGestureRecognizer *)sender{

    

    //计算偏移量

    CGPoint point = [sender translationInView:_testView];

    //进行平移

    sender.view.transform =CGAffineTransformMakeTranslation(point.x, point.y);  

}



UIPinchGestureRecognizer(捏合手势)

    

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc] initWithTarget:selfaction:@selector(pinchView:)];

    [_testView addGestureRecognizer:pinch];



#pragma mark pinch 捏合手势事件

-(void)pinchView:(UIPinchGestureRecognizer *)sender{

    //scale 缩放比例

    //    sender.view.transform = CGAffineTransformMake(sender.scale, 0, 0, sender.scale, 0, 0);

    //每次缩放以原来位置为标准

    //    sender.view.transform = CGAffineTransformMakeScale(sender.scale, sender.scale);

    

    //每次缩放以上一次为标准

    sender.view.transform =CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);

    //重新设置缩放比例 1是正常缩放.小于1时是缩小(无论何种操作都是缩小),大于1时是放大(无论何种操作都是放大)

    sender.scale = 1;  

}


UIRotationGestureRecognizer(旋转手势)

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizeralloc] initWithTarget:selfaction:@selector(rotationView:)];

    

    [_testView addGestureRecognizer:rotation];



-(void)rotationView:(UIRotationGestureRecognizer *)sender{

    //    sender.view.transform = CGAffineTransformMake(cos(M_PI_4), sin(M_PI_4), -sin(M_PI_4), cos(M_PI_4), 0, 0);

    //捏合手势两种改变方式

    //以原来的位置为标准

    //    sender.view.transform = CGAffineTransformMakeRotation(sender.rotation);//rotation 是旋转角度

    

    //两个参数,以上位置为标准

    sender.view.transform =CGAffineTransformRotate(sender.view.transform, sender.rotation);

    //消除增量

    sender.rotation = 0.0;  

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值