OC 触摸事件


下面是View与手势互动的方法

当然也能像手机的图库内样实现相片缩放和放大的功能

只需要在imageView上加上

      self.myimabeView.userInteractionEnabled = YES;

这样就能实现下面的效果了



 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sigleTapAction:)];
    [self.view addGestureRecognizer:singleTap];
    
    //双击
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)];
    doubleTap.numberOfTapsRequired = 2;
    [self.view addGestureRecognizer:doubleTap];
    //单击要想执行必须双击失效
    [singleTap requireGestureRecognizerToFail:doubleTap];
    
    /**
     *  长按
     *
     *  @return
     */
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    [self.targetView addGestureRecognizer:longPress];
    
    /**
     *  捏合手势
     *
     * UIPinchGestureRecognizer
     */
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [self.targetView addGestureRecognizer:pinchGesture];
    
    /**
     *  拖拽
     *
     *  UIPanGestureRecognizer
     */
    
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [self.targetView addGestureRecognizer:panGesture];
    
    /**
     *  旋转
     *
     *  
     */
    
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [self.targetView addGestureRecognizer:rotationGesture];
    [pinchGesture requireGestureRecognizerToFail:rotationGesture];
    
    
    UISwipeGestureRecognizer *leftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipAction:)];
    leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:leftSwipeGesture];
    UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipAction:)];
    rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:rightSwipeGesture];
    UISwipeGestureRecognizer *upSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipAction:)];
    upSwipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
    [self.view addGestureRecognizer:upSwipeGesture];
    UISwipeGestureRecognizer *downSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipAction:)];
    downSwipeGesture.direction = UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:downSwipeGesture];
}
-(void)sigleTapAction:(UITapGestureRecognizer *)sender{
    
    if (sender.view.backgroundColor == [UIColor whiteColor]) {
        sender.view.backgroundColor = [UIColor blackColor];
    }else{
        sender.view.backgroundColor = [UIColor whiteColor];
    }
    
}

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

- (void)longPressAction:(UILongPressGestureRecognizer *)sender{
    
    if (sender.state == UIGestureRecognizerStateEnded) {
        UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"标题" message:@"选择照片" preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"调用照相机");
        }];
        [alertController addAction:photoAction];
        
        UIAlertAction *libraryAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"调用本地相册");
        }];
        [alertController addAction:libraryAction];
        
        UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"取消");
        }];
        [alertController addAction:cancleAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
    
}

-(void)pinchAction:(UIPinchGestureRecognizer *)sender{
  
    sender.view.transform = CGAffineTransformMakeScale(sender.scale, sender.scale);
}

-(void)panAction:(UIPanGestureRecognizer *)sender{
//    if (sender.state != UIGestureRecognizerStateEnded && sender.state != UIGestureRecognizerStateFailed) {
//        CGPoint location = [sender locationInView:sender.view.superview];
//        sender.view.center = location;
//    }
    
//    CGPoint center = sender.view.center;
//    CGPoint translation = [sender translationInView:self.view];
//    sender.view.center = CGPointMake(center.x + translation.x, center.y + translation.y);
//    [sender setTranslation:CGPointZero inView:self.view];
    
    
    //视图前置操作
    [sender.view.superview bringSubviewToFront:sender.view];
   
    CGPoint center = sender.view.center;
    CGFloat cornerRadius = sender.view.frame.size.width / 2;
    CGPoint translation = [sender translationInView:self.view];
    //NSLog(@"%@", NSStringFromCGPoint(translation));
    sender.view.center = CGPointMake(center.x + translation.x, center.y + translation.y);
    [sender setTranslation:CGPointZero inView:self.view];
   
//    if (sender.state == UIGestureRecognizerStateEnded) {
//            //计算速度向量的长度,当他小于200时,滑行会很短
//        CGPoint velocity = [sender velocityInView:self.view];
//        CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
//       CGFloat slideMult = magnitude / 200;
//        //NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult); //e.g. 397.973175, slideMult: 1.989866
//        
//              //基于速度和速度因素计算一个终点
//        float slideFactor = 0.1 * slideMult;
//        CGPoint finalPoint = CGPointMake(center.x + (velocity.x * slideFactor),
//                                                                                              center.y + (velocity.y * slideFactor));
//        //限制最小[cornerRadius]和最大边界值[self.view.bounds.size.width - cornerRadius],以免拖动出屏幕界限
//        finalPoint.x = MIN(MAX(finalPoint.x, cornerRadius),
//                                                                  self.view.bounds.size.width - cornerRadius);
//        finalPoint.y = MIN(MAX(finalPoint.y, cornerRadius),
//                                                                  self.view.bounds.size.height - cornerRadius);
//        
//                 //使用 UIView 动画使 view 滑行到终点
//               [UIView animateWithDuration:slideFactor*2
//                                                   delay:0
//                                                 options:UIViewAnimationOptionCurveLinear
//                                              animations:^{
//                                                    sender.view.center = finalPoint;
//                                                  }
//                                             completion:nil];
 //            }
    
}

- (void)rotationAction:(UIRotationGestureRecognizer *)sender{
    sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation);
    sender.rotation = 0.0;
}

-(void)swipAction:(UISwipeGestureRecognizer *)sender{
    switch (sender.direction) {
        case UISwipeGestureRecognizerDirectionRight:
        {
            CATransition *animation=[CATransition animation];
            animation.delegate=self;
            animation.duration=1.0f;
            animation.timingFunction=UIViewAnimationCurveEaseInOut;
            animation.type=kCATransitionMoveIn;
            animation.subtype=kCATransitionFromLeft;
            [sender.view.layer addAnimation:animation forKey:@"move in"];
        }
            break;
        case UISwipeGestureRecognizerDirectionLeft:
        {
            NSLog(@"向左扫动了屏幕");
            
            CATransition *animation=[CATransition animation];
            animation.delegate=self;
            animation.duration=1.0f;
            animation.timingFunction=UIViewAnimationCurveEaseInOut;
            animation.type=kCATransitionMoveIn;
            animation.subtype=kCATransitionFromRight;
            [sender.view.layer addAnimation:animation forKey:@"move in"];
        }
            break;
        case UISwipeGestureRecognizerDirectionUp:
        {
            CATransition *animation=[CATransition animation];
            animation.delegate=self;
            animation.duration=1.0f;
            animation.timingFunction=UIViewAnimationCurveEaseInOut;
            animation.type=kCATransitionMoveIn;
            animation.subtype=kCATransitionFromBottom;
            [sender.view.layer addAnimation:animation forKey:@"move in"];
        }
            break;
        case UISwipeGestureRecognizerDirectionDown:
        {
            CATransition *animation=[CATransition animation];
            animation.delegate=self;
            animation.duration=1.0f;
            animation.timingFunction=UIViewAnimationCurveEaseInOut;
            animation.type=kCATransitionMoveIn;
            animation.subtype=kCATransitionFromTop;
            [sender.view.layer addAnimation:animation forKey:@"move in"];
        }
            break;
            
        default:
            break;
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值