手势

来源:互联网 发布:nba免费直播软件 编辑:程序博客网 时间:2024/04/25 13:37

我们常用的手势有6种,下面我用代码简单给大家介绍一下,希望对大家有所帮助

- (void)createSubViews{

    self.imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(70,130, 220, 350)];

    [_imageViewsetImage:[UIImageimageNamed:@"1.jpg"]];

    _imageView.userInteractionEnabled =YES;

    [self.viewaddSubview:_imageView];

    [_imageView release];

    

    

   UISegmentedControl *segmentedControl = [[UISegmentedControlalloc] initWithItems:[NSArrayarrayWithObjects:@"点击",@"长按",@"轻扫",@"捏合",@"旋转",@"拖拽",nil]];

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

    segmentedControl.momentary =YES;

    // 插入一个控件

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

    segmentedControl.tintColor = [UIColorblackColor];

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

    [self.viewaddSubview:segmentedControl];

    [segmentedControlrelease];

    

    

}

#pragma mari - UISegmentedControl的点击事件

- (void)action:(id)sender{

    // 分段点击按钮

    UISegmentedControl *seg = (UISegmentedControl *)sender;

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

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

    NSArray *gestures =self.imageView.gestureRecognizers;

    // 第二步:移除

   for (UIGestureRecognizer *gesin gestures) {

        [self.imageViewremoveGestureRecognizer:ges];

    }

    switch (seg.selectedSegmentIndex) {

       case 0:

        {

           // 创建一个点击手势

            UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tapAction:)];

            [self.imageViewaddGestureRecognizer:tap];

            [taprelease];

        }

           break;

       case 1:

        {

           // 长按手势

            UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPressAction:)];

            [self.imageViewaddGestureRecognizer:longPress];

            [longPressrelease];

        }

           break;

       case 2:

        {

            UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(swipeAction:)];

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

           // 设置轻扫的方向

            swipe.direction =UISwipeGestureRecognizerDirectionDown;

            [self.imageViewaddGestureRecognizer:swipe];

            [swiperelease];

        }

           break;

       case 3:

        {

           // 捏合手势

            UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc] initWithTarget:selfaction:@selector(pinchAciton:)];

            [self.imageViewaddGestureRecognizer:pinch];

            [pinchrelease];

        }

           break;

       case 4:

        {

           // 旋转手势

            UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizeralloc] initWithTarget:selfaction:@selector(rotateAction:)];

            [self.imageViewaddGestureRecognizer:rotate];

            [rotaterelease];

        }

           break;

       case 5:

        {

           // 拖拽手势

            UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(panAction:)];

            [self.imageViewaddGestureRecognizer:pan];

            [panrelease];

        }

           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:CGPointZeroinView:self.imageView];

    

}




0 0
原创粉丝点击