码迷,mamicode.com
首页 > 移动开发 > 详细

iOS的基本手势

时间:2015-11-16 10:38:53      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

1.点击手势:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
    //设定点击次序,识别手势的时候使用
    tapGesture.numberOfTapsRequired = 2;

2.长按手势:

 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGesture:)];
    //设置按压最短持续时间,默认0.5s
    longPress.minimumPressDuration = 1;

3.滑动手势:

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

4.捏合手势:

 UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)];
    pinchGesture.delegate = self;
5.旋转手势:

UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotationGesture:)];
    rotationGesture.delegate = self;
#pragma mark -
#pragma mark Handle Gesture Recoginzer

- (void)handleRotationGesture:(UIRotationGestureRecognizer*)gesture{
    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
        UIView *view = gesture.view;
        [self.view bringSubviewToFront:view];
        //旋转的弧度
        CGFloat rotation = gesture.rotation;
        view.transform = CGAffineTransformRotate(view.transform, rotation);
        
        //注意有累加
        gesture.rotation = 0.0;
    }
}

- (void)handlePinchGesture:(UIPinchGestureRecognizer*)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
        UIView *view = gesture.view;
        [self.view bringSubviewToFront:view];
        //得到缩放的比例,然后设置transform的缩放参数
        //gesture.scale 捏合时候的缩放比例
        CGFloat scale = gesture.scale;
        
        //设置view.transform scale 代表缩放
        //第一个参数,是基准值,在此基础上进行缩放
        //第二个参数,x 坐标方向的缩放比例
        //第三个参数,y 坐标方向的缩放比例
        view.transform = CGAffineTransformScale(view.transform, scale, scale);
        
        //由于手势效果会累积,所以需要给他复位
        gesture.scale = 1.0;
    }
}

- (void)handlePanGesture:(UIPanGestureRecognizer*)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
        //手势识别成功,并且手势持续进行中
        UIView *view = gesture.view;
        [self.view bringSubviewToFront:view];
        //手势在self.view上移动的多少
        CGPoint offset = [gesture translationInView:self.view];
        CGPoint newCenter = CGPointMake(view.center.x+offset.x, view.center.y+offset.y);
        view.center = newCenter;
        
        //手势的效果会累加,translation一直累加
        //重新初始化,去除原来的累加效果
        [gesture setTranslation:CGPointZero inView:self.view];
    }
}

//点击手势处理
- (void)handleTapGesture:(UITapGestureRecognizer*)gesture
{
    //gesture.view; 代表手势发生在哪一个view上
    NSLog(@"点击了2次,手势识别成功");
}

//手势识别的过程,首先接受到touchbegin的事件,开始启动手势识别,手势的状态
//UIGestureRecognizerStatePossible,然后继续接收并分析触摸事件,如果手势识别成功,手势的状态变为UIGestureRecognizerStateBegan
//如果手势继续进行,手势的状态变为UIGestureRecognizerStateChanged
//如果手势结束,状态变为UIGestureRecognizerStateEnded
//如果手势识别失败 UIGestureRecognizerStateFailed
- (void)handleLongPressGesture:(UILongPressGestureRecognizer*)gestrue
{
    if (gestrue.state == UIGestureRecognizerStateBegan || gestrue.state == UIGestureRecognizerStateChanged) {
        NSLog(@"长按手势识别成功");
    }
    if (gestrue.state == UIGestureRecognizerStateEnded) {
        NSLog(@"手势识别结束");
    }
}

#pragma mark -
#pragma mark UIGestureReconigerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if (gestureRecognizer.view == otherGestureRecognizer.view) {
        return YES;
    }
    return NO;
}


iOS的基本手势

标签:

原文地址:http://www.cnblogs.com/applekevin/p/4968108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!