UI各种手势(轻拍,长按,旋转,捏合,清扫,拖拽)

本文介绍了一个使用Objective-C实现的手势识别系统,通过不同类型的触摸屏手势(如轻拍、长按、旋转等)来控制屏幕上的图片变化。文章详细展示了如何设置手势识别器并为每个手势定义相应的响应行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

-(void)loadView

{

    self.view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];

    self.view.backgroundColor = [UIColor redColor];

    

    imageV = [[UIImageView alloc]initWithFrame:CGRectMake(70, 150, 200, 200)];

    

    imageV.backgroundColor = [UIColor blueColor];

    

    [self.view addSubview:imageV];

    

    [imageV release];

    

    NSArray *arr = [NSArray arrayWithObjects:@"轻拍",@"长按",@"旋转",@"捏合",@"轻扫",@"拖拽", nil];

    

    UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:arr];

    

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

    

    seg.frame = CGRectMake(0, 400, 320, 40);

    

    imageV.userInteractionEnabled = YES;

    

    [self.view addSubview:seg];

    

    

}

-(void)tap:(UISegmentedControl *)tap

{

   for(id gesture in imageV.gestureRecognizers)

   {

       [imageV removeGestureRecognizer:gesture];

   }

    

    switch (tap.selectedSegmentIndex)

    {

        case 0:

        {

            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dotap:)];

            

            [imageV addGestureRecognizer:tap];

            

            imageV.userInteractionEnabled = YES;

            

            [tap release];

            

            break;

        }

            case 1:

        {

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

            

            [imageV addGestureRecognizer:longPress];

            

            [longPress release];

            

            break;

        }

            case 2:

        {

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

            

            [imageV addGestureRecognizer:rotationPress];

            

            [rotationPress release];

            break;

        }

           case 3:

        {

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

            [imageV addGestureRecognizer:pinchPress];

            

            [pinchPress release];

            break;

        }

            case 4:

        {

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

            [imageV addGestureRecognizer:swipePress];

            [swipePress release];


            

            

            UISwipeGestureRecognizer *swipePress1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipePress:)];

            [imageV addGestureRecognizer:swipePress1];

            swipePress.direction = UISwipeGestureRecognizerDirectionLeft  ;

            

            [swipePress1 release];

            break;

            

                    }

            case 5:

        {

            UIPanGestureRecognizer *panPress = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panPress:)];

            

            [imageV addGestureRecognizer:panPress];

            

            [panPress release];

            break;

        }

        default:

            break;

    }

}

-(void)dotap:(UITapGestureRecognizer *)dotap//轻按随机换图

{

    NSString *imageName = [NSString stringWithFormat:@"h%d.jpeg",arc4random()%8+1];

    

    imageV.image = [UIImage imageNamed:imageName];

}

-(void)longPress:(UILongPressGestureRecognizer *)longPress//长按按顺序换图

{

   

    if(longPress.state == UIGestureRecognizerStateEnded)

    {

       

        


       NSString *imageName = [NSString stringWithFormat:@"h%d.jpeg",i%8+1] ;

    

    imageV.image = [UIImage imageNamed:imageName];

        

        i = i+1;

    }

    

}

-(void)rotationPress:(UIRotationGestureRecognizer *)rotationPress//旋转

{

    [imageV setTransform:CGAffineTransformMakeRotation(rotationPress.rotation)];  

}


-(void)pinchPress:(UIPinchGestureRecognizer *)pinchPress//捏合

{

    [imageV setTransform:CGAffineTransformMakeScale(pinchPress.scale, pinchPress.scale)];

}


-(void)swipePress:(UISwipeGestureRecognizer *)swipePress//轻扫

{

    [UIView beginAnimations:nil context:nil];

    

    [UIView setAnimationTransition:(swipePress.direction == UISwipeGestureRecognizerDirectionLeft )?UIViewAnimationTransitionFlipFromLeft:UIViewAnimationTransitionFlipFromRight forView:imageV cache:YES];

    

    

       [UIView commitAnimations];

}

-(void)panPress:(UIPanGestureRecognizer *)panPress //拖拽

{

    CGPoint point =[panPress translationInView:imageV];

    

    imageV.transform = CGAffineTransformMakeTranslation(point.x, point.y);

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值