ios开发——手势

在ios开发中,系统自带了6种手势,分别是:
UIPinchGestureRecognizer 捏合
UILongPressGestureRecognizer 长按
UIRotationGestureRecognizer 旋转
UIPanGestureRecognizer 拖动
UISwipeGestureRecognizer 轻扫
UITapGestureRecognizer 点击

1.单击双击

//单击双击
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)];

    [imageView addGestureRecognizer:singleTap];

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)];
    doubleTap.numberOfTapsRequired = 2;//需要点击的次数
    [imageView addGestureRecognizer:doubleTap];
    //双击响应时,单击需要失效
    [singleTap requireGestureRecognizerToFail:doubleTap];

对应的方法

-(void)singleTapAction:(UITapGestureRecognizer *)sender{
    NSLog(@"你点击了我");
}

-(void)doubleTapAction:(UITapGestureRecognizer *)sender{
    NSLog(@"你双击了我");
}

2.长按

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

    [imageView addGestureRecognizer:longPress];

对应的方法

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

    if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"你按了我很久");
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"保存图片" delegate:self cancelButtonTitle:@"确定" destructiveButtonTitle:@"下载图片" otherButtonTitles:@"保存到相册",@"打开相册",@"打开相机", nil];
        [sheet showInView:self.view];
    }

}

3.捏合

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [self.view addGestureRecognizer:pinch];

对应的方法

-(void)pinchAction:(UIPinchGestureRecognizer *)sender{
    NSLog(@"正在捏合");
//    imageView.transform = CGAffineTransformScale(imageView.transform, sender.scale, sender.scale);
    imageView.transform = CGAffineTransformMakeScale(sender.scale, sender.scale);


}

4.旋转

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [self.view addGestureRecognizer:rotation];

    [pinch requireGestureRecognizerToFail:rotation];

对应的方法

-(void)rotationAction:(UIRotationGestureRecognizer *)sender{
    NSLog(@"正在旋转");
//    imageView.transform = CGAffineTransformRotate(imageView.transform, sender.rotation);
    imageView.transform = CGAffineTransformMakeRotation(sender.rotation);
}

5.拖动

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[self.view addGestureRecognizer:pan];

对应的方法

-(void)panAction:(UIPanGestureRecognizer *)sender{
    //转换坐标系
    CGPoint point = [sender translationInView:self.view];
    //移动
    imageView.center = CGPointMake(imageView.center.x+point.x, imageView.center.y+point.y);
    //
    [sender setTranslation:CGPointZero inView:self.view];
}

6.轻扫

UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [imageView addGestureRecognizer:leftSwipe];

    UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [imageView addGestureRecognizer:rightSwipe];

对应的方法

-(void)swipeAction:(UISwipeGestureRecognizer *)sender{
    switch (sender.direction) {
        case UISwipeGestureRecognizerDirectionLeft:
        {
            NSLog(@"你向左轻扫");
            count++;
            if (count > images.count-1) {
                [[[UIAlertView alloc] initWithTitle:@"" message:@"已经是最后一张" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
                count--;
                return;
            }

            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:imageView cache:YES];
            [UIView commitAnimations];
            imageView.image = images[count];
        }
            break;

        case UISwipeGestureRecognizerDirectionRight:
        {
            NSLog(@"你向右轻扫");
            count--;
            if (count < 0) {
                [[[UIAlertView alloc] initWithTitle:@"" message:@"已经是第一张了" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
                count++;
                return;
            }

            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:imageView cache:YES];
            [UIView commitAnimations];
            imageView.image = images[count];

        }
            break;


        default:
            break;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值