iOS手势

6 篇文章 0 订阅

手势识别在iOS上非常重要,手势操作是移动设备的重要特征,极大的增加了移动设备使用便捷性。

1.UITapGestureRecognizer   点击手势

2.UILongPressGestureRecognizer   长按手势

3.UISwipeGestureRecognizer   轻扫手势

4.UIPinchGestureRecognizer   捏合手势

5.UIRotationGestureRecognizer   旋转手势

6.UIPanGestureRecognizer   拖拽手势

1. 分段控件

UISegmentedControl *segmentedControl = [[UISegmentedControl allocinitWithItems:[NSArray arrayWithObjects:@"点击"@"长按"@"轻扫"@"捏合"@"旋转"@"拖拽"nil]];

    segmentedControl.frame = CGRectMake(3040031550);

    segmentedControl.momentary = YES;

    // 插入一个控件

//    [segmentedControl insertSegmentWithTitle:@"图片" atIndex:6 animated:YES];

    segmentedControl.tintColor = [UIColor blackColor];

    [segmentedControl addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:segmentedControl];

    [segmentedControl release];

2. 分段控件点击事件

- (void)action:(id)sender{

    // 分段点击按钮

    UISegmentedControl *seg = (UISegmentedControl *)sender;

    // 移除imageView的所有手势,重新添加新的手势

    // 第一步:获得一个视图的所有手势

    NSArray *gestures = self.imageView.gestureRecognizers;

    // 第二步:移除

    for (UIGestureRecognizer *ges in gestures) {

        [self.imageView removeGestureRecognizer:ges];

    }

    switch (seg.selectedSegmentIndex) {

        case 0:

        {

            // 创建一个点击手势

            UITapGestureRecognizer *tap = [[UITapGestureRecognizer allocinitWithTarget:self action:@selector(tapAction:)];

            [self.imageView addGestureRecognizer:tap];

            [tap release];

        }

            break;

        case 1:

        {

            // 长按手势

            UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer allocinitWithTarget:self action:@selector(longPressAction:)];

            [self.imageView addGestureRecognizer:longPress];

            [longPress release];

        }

            break;

        case 2:

        {

            UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer allocinitWithTarget:self action:@selector(swipeAction:)];

            // 一个轻扫手势只能有一个轻扫方向

            // 设置轻扫的方向

            swipe.direction = UISwipeGestureRecognizerDirectionDown;

            [self.imageView addGestureRecognizer:swipe];

            [swipe release];

        }

            break;

        case 3:

        {

            // 捏合手势

            UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer allocinitWithTarget:self action:@selector(pinchAciton:)];

            [self.imageView addGestureRecognizer:pinch];

            [pinch release];

        }

            break;

        case 4:

        {

            // 旋转手势

            UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer allocinitWithTarget:self action:@selector(rotateAction:)];

            [self.imageView addGestureRecognizer:rotate];

            [rotate release];

        }

            break;

        case 5:

        {

            // 拖拽手势

            UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer allocinitWithTarget:self action:@selector(panAction:)];

            [self.imageView addGestureRecognizer:pan];

            [pan release];

        }

            break;

        default:

            break;

    }

    

}

#pragma mark - 内部的点击手势事件

#pragma mark 点击

- (void)tapAction:(UITapGestureRecognizer *)tap{

    // 通过手势获得手势所在的视图

//    UIImageView *aImage = (UIImageView *)tap.view;

    // 通过不同的状态做不同的事

    if (tap.state == UIGestureRecognizerStateBegan) {

        NSLog(@"开始点击");

    }

    NSLog(@"%s", __func__);

}

#pragma mark 长按

- (void)longPressAction:(UILongPressGestureRecognizer *)press{

    if (press.state == UIGestureRecognizerStateBegan) {

        NSLog(@"begin");

    }

}

#pragma mark 轻扫

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{

        NSLog(@"横扫千军");

}

#pragma mark 捏合

- (void)pinchAciton:(UIPinchGestureRecognizer *)pinch{

    NSLog(@"捏合");

    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);

    pinch.scale = 1.0f;

}

#pragma mark 旋转

- (void)rotateAction:(UIRotationGestureRecognizer *)ges{

    // 旋转的基础角度为0, 每次旋转的角度都是以当前中轴线为轴来判定的

    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, ges.rotation);

    ges.rotation = 0;

    NSLog(@"Rotation = %f", ges.rotation);

}

#pragma mark 拖拽

- (void)panAction:(UIPanGestureRecognizer *)pan{

    CGPoint point = [pan translationInView:self.imageView];

    // 偏移量会在每次偏移之后,在偏移后的基础上增加

    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, point.x, point.y);

    // 重新把偏移量归零

    [pan setTranslation:CGPointZero inView:self.imageView];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值