iOS的基本手势

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;
}


转载于:https://www.cnblogs.com/applekevin/p/4968108.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值