关闭

iOS开发笔记--多手势事件

150人阅读 评论(0) 收藏 举报

ios多手势事件


开发ios应用时我们经常用到多手势来处理事情,如给scrollView增加点击事件,scrollView不能响应view的touch事件,但有时候却要用到多手势事件,那么我们可以给这个scrollView增加自己的手指事件。给imageView增加点击、放大、旋转等都可以通过多手势来完成。
下面就介绍下各种的手势的使用方法,及注意事项。
- (void)viewDidLoad

 

{

    [super viewDidLoad];

    //为了能响应多手势事件,imageView的userInteractionEnabled属性要设为YES.

    self.imageView.userInteractionEnabled=YES;

    

    //1、手指点击事件

    //单指单击

    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self

                                                                                     action:@selector(fingerIncident:)];

//手指数

    singleFingerOne.numberOfTouchesRequired = 1;

//点击次数        

    singleFingerOne.numberOfTapsRequired = 1;  

//设置代理方法         

    singleFingerOne.delegateself;    

//增加事件者响应者,                 

    [self.imageView addGestureRecognizer:singleFingerOne]; 

    

    //单指双击

    UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self

                                                                                     action:@selector(fingerIncident:)];

    singleFingerTwo.numberOfTouchesRequired = 1;

    singleFingerTwo.numberOfTapsRequired = 2;

    singleFingerTwo.delegateself;

    [self.imageView addGestureRecognizer:singleFingerTwo];

    

    

    //双指单击

    UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self

                                                                                     action:@selector(fingerIncident:)];

    doubleFingerOne.numberOfTouchesRequired = 2;

    doubleFingerOne.numberOfTapsRequired = 1;

    doubleFingerOne.delegateself;

    [self.imageView addGestureRecognizer:doubleFingerOne];

    

    //双指双击

    UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self

                                                                                     action:@selector(fingerIncident:)];

    doubleFingerTwo.numberOfTouchesRequired = 2;

    doubleFingerTwo.numberOfTapsRequired = 2;

    doubleFingerTwo.delegateself;

    [self.imageView addGestureRecognizer:doubleFingerTwo];

    

    //如果不加下面的话,当单指双击时,会先调用单指单击中的处理,再调用单指双击中的处理

    [singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];

    //同理双指也是如此

    [doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];


    

    

    //2、手势为捏的姿势:按住option按钮配合鼠标来做这个动作在虚拟器上

    UIPinchGestureRecognizer *pinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:selfaction:@selector(handlePinchGesture:)];

    [self.imageViewaddGestureRecognizer:pinchGesture];//imageView添加手势识别

    [pinchGesture release];

    

    //3、旋转手势:按住option按钮配合鼠标来做这个动作在虚拟器上

    UIRotationGestureRecognizer *rotateGesture=[[UIRotationGestureRecognizer alloc]initWithTarget:selfaction:@selector(handleRotateGesture:)];

    [self.imageView addGestureRecognizer:rotateGesture];

    [rotateGesture release];

    

    //4、拖手势

    UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizeralloc]initWithTarget:self action:@selector(handlePanGesture:)];

    [self.imageView addGestureRecognizer:panGesture];

    [panGesture release];

    

    //当实现上面拖手势(UIPanGestureRecognizer)后将不能响应下面的左右划(UISwipeGestureRecognizer)事件,事件会被拖手势栏截,所以左右划和拖手势只能选其一

    //右划

    UISwipeGestureRecognizer *swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:selfaction:@selector(handleSwipeGesture:)];

    [self.imageView addGestureRecognizer:swipeGesture];

    [swipeGesture release];

    //左划

    UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:selfaction:@selector(handleSwipeGesture:)];

    swipeGesture.direction=UISwipeGestureRecognizerDirectionLeft;//不设置黑夜是右

    [self.imageView addGestureRecognizer:swipeLeftGesture];

    [swipeLeftGesture release];

    

    UILongPressGestureRecognizer *longpressGesutre=[[UILongPressGestureRecognizer alloc]initWithTarget:self

                                                                                              action:@selector(handleLongpressGesture:)];

    //长按时间为1

    longpressGesutre.minimumPressDuration=0.5;

    //允许15秒中运动

    longpressGesutre.allowableMovement=3;

    //所需触摸1

    longpressGesutre.numberOfTouchesRequired=1;

    [self.imageView addGestureRecognizer:longpressGesutre];

    [longpressGesutre release];

}


//手指点击事件

- (void)fingerIncident:(UITapGestureRecognizer *)sender

{

    if (sender.numberOfTouchesRequired==1) {

        //单指点击事件

        if(sender.numberOfTapsRequired == 1) {

            //单指单击

            NSLog(@"单指单击");

            [sender.view.layer removeAllAnimations];

        }

        else if(sender.numberOfTapsRequired ==2 ){

            //单指双击

            NSLog(@"单指双击");

        }

    }

    else if (sender.numberOfTouchesRequired==2) {

        //双指点击事件

        if(sender.numberOfTapsRequired == 1) {

            //双指单击

            NSLog(@"双指单击");

        }

        else if(sender.numberOfTapsRequired ==2 ){

            //双指双击

            NSLog(@"双指双击");

        }

    }

}


//手指左右滑动事件

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

    if (sender.direction==UISwipeGestureRecognizerDirectionLeft) {

        NSLog(@"左滑");

    }

    else{

        NSLog(@"右滑");

    }

}


//手指捏事件

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

    NSLog(@"");

    

    if([sender state] == UIGestureRecognizerStateEnded) {

        lastScale = 1.0;

        return;

    }

    

    CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);

    

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

    

    [sender.view setTransform:newTransform];

    lastScale = [sender scale];

}


//手指旋转事件

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

    NSLog(@"旋转");

    

    NSLog(@"sender.scale=%f",sender.rotation);

    NSLog(@"sender.velocity=%f",sender.velocity);


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

    sender.rotation=0;

}


//手指拖事件

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

    NSLog(@"");

    NSLog(@"sender%@",sender);

    CGPoint translation=[(UIPanGestureRecognizer*)sendertranslationInView:self.view];

    

    //开始

    if (sender.state==UIGestureRecognizerStateBegan) {

        x=sender.view.center.x;

        y=sender.view.center.y;

    }

    //进行中

    if (sender.state==UIGestureRecognizerStateChanged) {

        sender.view.center=CGPointMake(x+translation.x,y+translation.y);

    }

    //结束

    if (sender.state==UIGestureRecognizerStateEnded) {

        sender.view.center=CGPointMake(x+translation.x,y+translation.y);

    }

}


//手指长按事件

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

    NSLog(@"长按");

    if (sender.state==UIGestureRecognizerStateBegan) {

        CABasicAnimation* rotationAnimation_ = [CABasicAnimationanimationWithKeyPath:@"transform.rotation.z"];

        //角度转弧度

        rotationAnimation_.fromValue = [NSNumbernumberWithFloat:(-0.02)];

        rotationAnimation_.toValue = [NSNumber numberWithFloat:(0.02)];

        //动画时间

        rotationAnimation_.duration = 0.1f;

        //动画次数

        rotationAnimation_.repeatCount=FLT_MAX;

        //回退动画

        rotationAnimation_.autoreverses = YES;

        //动画开始结束的快慢,设置为加速

        rotationAnimation_.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];

        [[sender.view layeraddAnimation:rotationAnimation_forKey:@"revItUpAnimation"];

    }

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:28121次
    • 积分:440
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:90篇
    • 译文:0篇
    • 评论:1条
    最新评论