`

iOS手势

阅读更多
iOS开发中遇到的手势相关的动作有以下几类(Apple iOS框架提供的简单实现)
UITapGestureRecognizer(包含单击、双击、多次点击等等)
UILongPressGestureRecognizer(长按手势)
UIPanGestureRecognizer(平移手势)
UISwipeGestureRecognizer(手指在屏幕上很快的滑动)
UIPinchGestureRecognizer(手指缩放操作)
UIRotationGestureRecognizer(手指旋转操作)

向指定View添加手势
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanFrom:)];
[self.view addGestureRecognizer:backPanRecognizer];
panRecognizer.maximumNumberOfTouches = 1;
panRecognizer.delegate = self;


从指定view移除手势
[self.view removeGestureRecognizer:backPanRecognizer];

1.点击手势(单击、双击)
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleTap];
singleTap.delegate = self;
[singleTap setNumberOfTapsRequired:1];//1次点击
[singleTap setNumberOfTouchesRequired:1];//1个手指操作

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
[self.view addGestureRecognizer:doubleTap];
[doubleTap setDelegate:self];
[doubleTap setNumberOfTapsRequired:2];//2次点击
[doubleTap setNumberOfTouchesRequired:1];//2个手指操作

[singleTap requireGestureRecognizerToFail:doubleTap];//防止双击触发两次单击事件
[doubleTap requireGestureRecognizerToFail:singleTap];


在hadnleSingleTap:和handleDoubleTap:中进行单击和双击事件的处理

2.长按手势
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[self.view addGestureRecognizer:longPressGesture];
[longPressGesture setMinimumPressDuration:1.0f];//最短按下持续时间1秒


在handleLongPress:方法中进行长按时间的处理

3.平移手势
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanFrom:)];
[self.view addGestureRecognizer:backPanRecognizer];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];


在handlePanFrom:方法中进行平移手势的处理
-(void)handlePanFrom:(UIPanGestureRecognizer *)recognizer{
    //拿到手指目前的位置
    CGPoint location = [recognizer locationInView:self.view];
    CGPoint tranlation = [recognizer translationInView:self.view];
    switch (recognizer.state) {
        //手指动作开始
        case UIGestureRecognizerStateBegan:
//            aView.transform = CGAffineTransformIdentity;
            break;
        //手指动作进行
        case UIGestureRecognizerStateChanged:
            //监听拖动动作
            //如果已经在起始位置则不允许再向右拖动
            if (tranlation.x <= 0) {
                aView.transform = CGAffineTransformMakeTranslation(tranlation.x, 0);
            }
            break;
        //手指动作结束
        case UIGestureRecognizerStateEnded:
        {
            //得到拖动速度
            CGPoint v = [recognizer velocityInView:self.view];
            //拖动速度大于500则直接remove视图
            if (v.x < -500) {
                [aView removeFromSuperView];
            } else {
                if (tranlation.x <= 0) {
                    aView.transform = CGAffineTransformMakeTranslation(tranlation.x, 0);
                }
                //如果手指当前位置大于280像素处,remove视图
                if (location.x < 80) {
                    [aView removeFromSuperView];
                } else {
                    //否则视图回到起始位置
                    [UIView animateWithDuration:0.3 animations:^{
                        aView.transform = CGAffineTransformMakeTranslation(0, 0);
                    }];
                }
            }
        }
            break;
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateFailed:
            break;
        default:
            break;
    }
}

4.快速滑动手势
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[self.view addGestureRecognizer:leftSwipe];
[leftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];

UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)];
[self.view addGestureRecognizer:rightSwipe];
[rightSwipe setDirection:UISwipeGestureRecognizerDirectionRight];

在handleSwipe:方法中处理快速滑动事件
- (void)handleSwipe:(UISwipeGestureRecognizer *)sender
{
switch (sender.direction) {
        case UISwipeGestureRecognizerDirectionLeft:
        {
            imgView = [[UIImageView alloc] initWithFrame:CGRectMake(kDeviceWidth, 0, 120, 160)];
            [imgView setImage:[UIImage imageNamed:@"redGirl.jpg"]];
            [self.view addSubview:imgView];
            [UIView animateWithDuration:0.5f animations:^{
                imgView.frame = CGRectMake(0, 0, 120, 160);
            } completion:^(BOOL finished) {
            }];
        }
            break;
        case UISwipeGestureRecognizerDirectionRight:
        {
            imgView = [[UIImageView alloc] initWithFrame:CGRectMake(-kDeviceWidth, 0, 120, 160)];
            [imgView setImage:[UIImage imageNamed:@"whiteGirl.jpg"]];
            [self.view addSubview:imgView];
            [UIView animateWithDuration:0.5f animations:^{
                imgView.frame = CGRectMake(0, 0, 120, 160);
            } completion:^(BOOL finished) {
            }];
        }
            break;
        default:
            break;
    }
}

5.缩放手势和旋转手势,待利用到再补充
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics