首页 > 代码库 > ios之UIGestureRecognizer手势基础使用解析

ios之UIGestureRecognizer手势基础使用解析

UIGestureRecognizer 的子类分别有很多手势,通过 不用的手势可以执行不同的操作,下面来介绍下他们的基本使用方法所有手势配置基本相同,只是针对不同的手势里面有部分属性可以设置,比如说tap点进去看他有两个参数可以设置一个是点击次数,和点击手指数可设置。如果不知道这个手势能配置说明参数,那么点击进入相应的.h 文件查看

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecongnizer:)];//点击手势
    [tapGesture setNumberOfTouchesRequired:1];
    [tapGesture setNumberOfTapsRequired:1];
    [_view addGestureRecognizer:tapGesture];
    [tapGesture release];
- (void)processGestureRecongnizer:(UIGestureRecognizer *)gesture
{
    if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) {
        [self positionAnimation];
    }
}

#pragma mark -- InitUserInterface
- (void)initUserInterface
{
    _view = [[UIView alloc]init];
    [_view setBounds:CGRectMake(0, 0, 200, 200)];
    [_view setCenter:CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds))];
    [_view setBackgroundColor:[UIColor grayColor]];
    [self.view addSubview:_view];
    //两手指拨动手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)];
    [pinch setDelegate:self];//设置代理
    [_view addGestureRecognizer:pinch]; //对view添加这个手势
    [pinch release];
    //旋转手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)];
    [rotation setDelegate:self];
    [_view addGestureRecognizer:rotation];
    [rotation release];
    //长按手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)];
    longPress.minimumPressDuration = 2;
    [_view addGestureRecognizer:longPress];
    [longPress release];
    //滑动手势--左
    UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)];
    [leftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];//配置滑动方向
    [self.view addGestureRecognizer:leftSwipe];
    [leftSwipe release];
    //滑动手势--右
    UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)];
    [rightSwipe setDirection:UISwipeGestureRecognizerDirectionRight];
    [self.view addGestureRecognizer:rightSwipe];
    [rightSwipe release];
    //拖移手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)];
    [pan setDelegate:self];
    [_view addGestureRecognizer:pan];
    [pan release];
    

}
#pragma mark -- GestureRrecognizer methods
//代理方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return NO;
}
// 手势action 让对应的手势执行相应地操作
- (void)processGestureRecognizer:(UIGestureRecognizer *)gesture
{
    //判断手势类型
    if ([gesture isKindOfClass:[UIPinchGestureRecognizer class]])
    {
        UIPinchGestureRecognizer *pinch = (UIPinchGestureRecognizer *)gesture;
        static float lastScale;//静态变量记录上次大小
        if (pinch.state == UIGestureRecognizerStateBegan) {
            lastScale = pinch.scale;// 手势开始把初始scale赋给静态变量方便更新
        }else if (pinch.state == UIGestureRecognizerStateChanged){
            [_view setTransform:CGAffineTransformScale(_view.transform, 1+(pinch.scale - lastScale), 1+(pinch.scale - lastScale))];//让View进行动态的放大或缩小
            lastScale = pinch.scale;// 更新scale的值 --(这样做让view保持变化后的状态而不会是初始状态)
            //此方法不需要更新lastScale的值  都是从原型开始
//            [_view setTransform:CGAffineTransformMakeScale(1+(pinch.scale - lastScale), 1+(pinch.scale - lastScale))];
        }
    }else if ([gesture isKindOfClass:[UIRotationGestureRecognizer class]])
    {
        UIRotationGestureRecognizer *rotation = (UIRotationGestureRecognizer *)gesture;
        static float lastRotation;
        if (rotation.state == UIGestureRecognizerStateBegan) {
            lastRotation = rotation.rotation;
        }else if (rotation.state == UIGestureRecognizerStateChanged){
            [_view setTransform:CGAffineTransformRotate(_view.transform, rotation.rotation - lastRotation)];
            lastRotation = rotation.rotation;
        }
    }
    else if ([gesture isKindOfClass:[UILongPressGestureRecognizer class]])
    {
        UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)gesture;
        if (longPress.state == UIGestureRecognizerStateBegan) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"Long Press Begin" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
            [alert release];
        }
    }
    else if ([gesture isKindOfClass:[UISwipeGestureRecognizer class]])
    {
        UISwipeGestureRecognizer *swipe = (UISwipeGestureRecognizer *)gesture;
        if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
            [UIView animateWithDuration:1 animations:^{
                [_view setCenter:CGPointMake(CGRectGetMinX(self.view.bounds), CGRectGetMidY(self.view.bounds))];
            }];
        }else{
            [UIView animateWithDuration:1 animations:^{
                [_view setCenter:CGPointMake(CGRectGetMaxX(self.view.bounds), CGRectGetMidY(self.view.bounds))];
            }];
        }
        
    }
    else if ([gesture isKindOfClass:[UIPanGestureRecognizer class]])
    {
        UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gesture;
        static CGPoint lastLocation;
        if (pan.state == UIGestureRecognizerStateBegan) {
            lastLocation = _view.center;
        }else if (pan.state == UIGestureRecognizerStateChanged)
        {
            CGPoint translationPoint = [pan translationInView:self.view];
            _view.center = CGPointMake(translationPoint.x + lastLocation.x, translationPoint.y+ lastLocation.y);
        }else if (pan.state == UIGestureRecognizerStateEnded)
        {
            lastLocation = CGPointZero;
        }
    }
}



ios之UIGestureRecognizer手势基础使用解析