我的IOS学习之路(三):手势识别器

时间:2023-11-18 12:52:20

  在iOS的学习中,对于手势的处理是极为重要的,如对于图片,我们经常需要进行旋转,缩放以及移动等。这里做一下总结,详见代码。

 - (void)viewDidLoad
 {
     [super viewDidLoad];
     UIImage *image = [UIImage imageNamed:@"018.png"];
     UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
     imageView.frame = CGRectMake(, , , );
     [self.view addSubview:imageView];

     //开启用户交互
     imageView.userInteractionEnabled = YES;
     //缩放手势识别器
     UIPinchGestureRecognizer *pinGes = [[UIPinchGestureRecognizer alloc] init];
     pinGes.delegate = self;

     [pinGes addTarget:self action:@selector(pinGes:)];
     [imageView addGestureRecognizer:pinGes];

     //旋转手势识别器
     UIRotationGestureRecognizer *rotaGes = [[UIRotationGestureRecognizer alloc] init];
     rotaGes.delegate = self;
     imageView.userInteractionEnabled = YES;
     [rotaGes addTarget:self action:@selector(rota:)];
     [imageView addGestureRecognizer:rotaGes];

     //移动手势识别器
     UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] init];
     [panGes addTarget:self action:@selector(panGes:)];
     [imageView addGestureRecognizer:panGes];

     //长按手势识别器
     UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init];

     //设置长按时间标准(默认时间为0.5秒)
     longPress.minimumPressDuration = 1.0;
     [longPress addTarget:self action:@selector(longPress:)];
     [imageView addGestureRecognizer:longPress];

 }

 //此方法的返回值表示手势识别器是否支持多手势操作
 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
 {
     return YES;
 }

 //长按操作
 - (void) longPress: (UILongPressGestureRecognizer *)ges
 {
     //开始长按时进行的操作
     if (ges.state == UIGestureRecognizerStateBegan) {
         NSLog(@"begin");
         //结束长按时进行的操作
     }else if (ges.state == UIGestureRecognizerStateEnded){
         NSLog(@"end");
     }
 }

 //移动操作
 - (void) panGes: (UIPanGestureRecognizer *)ges
 {
     //获取相对于父视图移动的距离
     CGPoint point = [ges translationInView:self.view];
     //开启动画
     [UIView beginAnimations:Nil context:Nil];
     //设置动画时间
     [UIView setAnimationDuration:];
     //通过形变属性移动
     ges.view.transform = CGAffineTransformTranslate(ges.view.transform, point.x, point.y);
     //因为手势识别器会对每次移动的距离进行累加,所以当移动一次后,需要将相对移动距离设置为(0,0);
     [ges setTranslation:CGPointMake(, ) inView:self.view];

     //通过中心点移动
 //    ges.view.center = CGPointMake(ges.view.center.x + point.x, ges.view.center.y + point.y);
 //    [ges setTranslation:CGPointMake(0, 0) inView:self.view];
     //动画结束
     [UIView commitAnimations];
 }

 //旋转操作
 - (void) rota: (UIRotationGestureRecognizer *)ges
 {
     //设置形变属性
     ges.view.transform = CGAffineTransformRotate(ges.view.transform, ges.rotation);
     //因为CGAffineTransformRotate函数会将每一次的旋转角度进行叠加,所以需要将手势识别器的旋转角度置0;
     ges.rotation = ;
 }

 //缩放操作
 - (void)pinGes: (UIPinchGestureRecognizer *)ges
 {
 //    NSLog(@"get in...");
     //因为CGAffineTransformScale函数会将缩放比例进行累乘,所以需要将手势识别器的缩放比例设置1
     ges.view.transform = CGAffineTransformScale(ges.view.transform, ges.scale, ges.scale);
     ges.scale = 1.0f;
 }