天天看點

Gesture Recognizer(添加手勢)

1.UITapGestureRecognizer   點選手勢

2.UILongPressGestureRecognizer   長按手勢

3.UISwipeGestureRecognizer   輕掃手勢

4.UIPinchGestureRecognizer   捏合手勢

5.UIRotationGestureRecognizer   旋轉手勢

6.UIPanGestureRecognizer   拖拽手勢

1. 分段控件

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"點選", @"長按", @"輕掃", @"捏合", @"旋轉", @"拖拽", nil]];

    segmentedControl.frame = CGRectMake(30, 400, 315, 50);

    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 alloc] initWithTarget:self action:@selector(tapAction:)];

            [self.imageView addGestureRecognizer:tap];

            [tap release];

        }

            break;

        case 1:

        {

            // 長按手勢

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

            [self.imageView addGestureRecognizer:longPress];

            [longPress release];

        }

            break;

        case 2:

        {

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

            // 一個輕掃手勢隻能有一個輕掃方向

            // 設定輕掃的方向

            swipe.direction = UISwipeGestureRecognizerDirectionDown;

            [self.imageView addGestureRecognizer:swipe];

            [swipe release];

        }

            break;

        case 3:

        {

            // 捏合手勢

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

            [self.imageView addGestureRecognizer:pinch];

            [pinch release];

        }

            break;

        case 4:

        {

            // 旋轉手勢

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

            [self.imageView addGestureRecognizer:rotate];

            [rotate release];

        }

            break;

        case 5:

        {

            // 拖拽手勢

            UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget: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];

}