GesturesAndEventHandle常用手势

来源:互联网 发布:cad迷你看图 mac 编辑:程序博客网 时间:2024/05/01 10:09

 //单击手势必须在双击手势识别失败时才能识别

            [singleTaprequireGestureRecognizerToFail:doubleTap];

【触摸事件】

//触摸的传递是从下往上

//事件的处理是从上往下

1、开始

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

   printf("开始\n");

   /*

     timestamp:时间戳(从开机到触摸的时间间隔)

     phase:触摸状态

     tapCount:在某一时间段内的连续点击次数

     view:触摸的视图

     */

   UITouch *touch = [touchesanyObject];

    NSLog(@"timestamp:%.2lf", touch.timestamp);

}

//滑动时一直调用,不能做耗时操作

2、移动

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [supertouchesMoved:toucheswithEvent:event];

    //printf("移动\n");

   UITouch *touch = [touchesanyObject];

   if (touch.view ==self.label) {

       CGPoint currentLocation = [touchlocationInView:self.view];

       CGPoint previousLocation = [touchpreviousLocationInView:self.view];

       CGFloat deltaX = currentLocation.x-previousLocation.x;

       CGFloat deltaY = currentLocation.y-previousLocation.y;

       CGPoint center =self.label.center;

        center.x += deltaX;

        center.y += deltaY;

       self.label.center = center;

    }

}

3、结束

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [supertouchesEnded:toucheswithEvent:event];

   printf("结束\n");

   UITouch *touch = [touchesanyObject];

   if (touch.tapCount ==2) {

        self.label.backgroundColor =RandomColor;

    }

}

4、取消

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

   printf("取消\n”);

}

1、点击(tap)

    //手势都继承自UIGestureRecognizer

    //点击手势

    UITapGestureRecognizer *tgr = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapHandle:)];

    

    //设置点击次数

    tgr.numberOfTapsRequired =2;

    //触摸点的个数

    tgr.numberOfTouchesRequired =2;

    

    //添加手势

    [self.labeladdGestureRecognizer:tgr];

    [tgrrelease];

2、长按(longPress)

- (void)longPressHandle:(UILongPressGestureRecognizer *)lpgr {

   printf("%ld\n", lpgr.state);

    if (lpgr.state ==UIGestureRecognizerStateBegan) {

        printf("长按手势识别成功\n");

        self.label.transform = CGAffineTransformMakeScale(0.5,2.0);

    } elseif (lpgr.state ==UIGestureRecognizerStateEnded) {

        printf("长按手势识别结束\n");

        self.label.transform =CGAffineTransformIdentity;

    }

    //长按手势

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

    //设置长按识别的最短持续时间

    lpgr.minimumPressDuration =2;

    [self.labeladdGestureRecognizer:lpgr];

    [lpgrrelease];


3、慢速滑动,拖动(pan)

- (void)panHandle:(UIPanGestureRecognizer *)pgr {

    //printf("state:%ld\n", pgr.state);

    if (pgr.state ==UIGestureRecognizerStateChanged) {

        //找到手势所在的视图

       UIView *view = pgr.view;

       CGPoint offset = [pgrtranslationInView:self.view];

       CGPoint center = view.center;

        center.x += offset.x;

        center.y += offset.y;

        view.center = center;

        //偏移效果会累加,每次都需要清除

        [pgr setTranslation:CGPointZeroinView:self.view];

    }

}

    //滑动手势

    UIPanGestureRecognizer *pgr = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panHandle:)];

    [self.labeladdGestureRecognizer:pgr];

    [pgrrelease];

4、捏合(pinch)

- (void)pinchHandle:(UIPinchGestureRecognizer *)pgr {

    //printf("state:%ld\n", pgr.state);

    if (pgr.state ==UIGestureRecognizerStateChanged) {

 

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

        //捏合的比例效果会累加,每次都需重置

        pgr.scale =1.0;

    }

}


    //捏合手势

    UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinchHandle:)];

    [self.labeladdGestureRecognizer:pgr];

    [pgrrelease];

5、旋转(rotation)

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    //手势发生在同一视图上,允许同时有效

   if (gestureRecognizer.view == otherGestureRecognizer.view) {

       returnYES;

    }

    return NO;

}


- (void)pinchHandle:(UIPinchGestureRecognizer *)pgr {

    if (pgr.state ==UIGestureRecognizerStateChanged) {

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

        pgr.scale =1.0;

    }

}

    if (rgr.state ==UIGestureRecognizerStateChanged) {

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

        //旋转角度效果会叠加,每次都需清除

        rgr.rotation =0;

    }

    //旋转手势

    UIRotationGestureRecognizer *rgr = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotationHandle:)];

    rgr.delegate =self;

    [self.labeladdGestureRecognizer:rgr];

    [rgrrelease];

    //再添加捏合手势

    UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinchHandle:)];

    [self.labeladdGestureRecognizer:pgr];

    [pgrrelease];

6、摇晃(motion)

//创建��视图

    UIImageView *back = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"ShakeHideImg_women"]];

    CGSize size = self.view.frame.size;

    back.center =CGPointMake(size.width/2, size.height/2);

    [self.viewaddSubview:back];

    [backrelease];

    //修改view背景色,与图片保持一致

    self.view.backgroundColor = [UIColorcolorWithRed:41/255.0green:45/255.0blue:46/255.0alpha:1];

    //创建上下两张动画的图片

   UIImage *upImage = [UIImageimageNamed:@"Shake_Logo_Up"];

   UIImageView *upView = [[UIImageViewalloc]initWithImage:upImage];

    upView.center =CGPointMake(size.width/2, (size.height-upImage.size.height)/2-20);

    upView.tag =100;

    [self.viewaddSubview:upView];

    [upViewrelease];

    

   UIImage *downImage = [UIImageimageNamed:@"Shake_Logo_Down"];

   UIImageView *downView = [[UIImageViewalloc]initWithImage:downImage];

    downView.center =CGPointMake(size.width/2, (size.height+downImage.size.height)/2-20);

    downView.tag =200;

    [self.viewaddSubview:downView];

    [downViewrelease];

}

- (void)setAnimate {

#define MOVE_STEP  60

   UIView *upView = [self.viewviewWithTag:100];

   UIView *downView = [self.viewviewWithTag:200];

    [UIViewanimateWithDuration:0.5animations:^{

       //上面图片上移

        upView.transform =CGAffineTransformMakeTranslation(0, -MOVE_STEP);

       //上面图片下移

        downView.transform =CGAffineTransformMakeTranslation(0,MOVE_STEP);

    }completion:^(BOOL finished) {

        [UIViewanimateWithDuration:0.3animations:^{

            upView.transform =CGAffineTransformIdentity;

            downView.transform =CGAffineTransformIdentity;

        }completion:^(BOOL finished) {

            NSArray *names =@[@"shake_match.wav",@"shake_nomatch.wav"];

            [selfperformSelector:@selector(playMusicWithName:)withObject:names[arc4random()%2]afterDelay:0.5];

        }];

    }];

}


- (void)playMusicWithName:(NSString *)name {

    NSArray *names = [namecomponentsSeparatedByString:@"."];

   NSString *file = [[NSBundlemainBundle]pathForResource:names[0]ofType:names[1]];

   NSURL *url = [NSURLfileURLWithPath:file];


   SystemSoundID soundId;

    AudioServicesCreateSystemSoundID((CFURLRef)url, &soundId);

    AudioServicesPlayAlertSound(soundId);

}


- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    printf("开始晃动\n");

    [selfsetAnimate];

    [selfplayMusicWithName:@"shake_sound_male.wav"];

}


- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    printf("晃动结束\n");

}


- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    printf("晃动取消\n");

}


7、快速滑动,轻扫(swipe)

- (void)swipeHandle:(UISwipeGestureRecognizer *)sgr {

    //printf("识别成功\n");

#define MOVE_STEP   50

   CGPoint center = sgr.view.center;

    

   switch (sgr.direction) {

        caseUISwipeGestureRecognizerDirectionRight:

            center.x +=MOVE_STEP;

           break;

        caseUISwipeGestureRecognizerDirectionLeft:

            center.x -=MOVE_STEP;

           break;

        caseUISwipeGestureRecognizerDirectionDown:

            center.y +=MOVE_STEP;

           break;

        caseUISwipeGestureRecognizerDirectionUp:

            center.y -=MOVE_STEP;

           break;

       default:

           break;

    }

    sgr.view.center = center;

}

    UISwipeGestureRecognizerDirection dir[] = {

        UISwipeGestureRecognizerDirectionRight,

        UISwipeGestureRecognizerDirectionLeft,

        UISwipeGestureRecognizerDirectionUp,

        UISwipeGestureRecognizerDirectionDown

    };

   for (NSInteger i=0; i<sizeof(dir)/sizeof(dir[0]); i++) {

        //轻扫手势,相当与具有单一方向的滑动手势

        UISwipeGestureRecognizer *sgr = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipeHandle:)];

        //设置轻扫的方向

        sgr.direction = dir[i];

        [self.labeladdGestureRecognizer:sgr];

        [sgrrelease];

    }


0 0
原创粉丝点击