我的IOS学习之路(三):手势识别器
在iOS的学习中,对于手势的处理是极为重要的,如对于图片,我们经常需要进行旋转,缩放以及移动等。这里做一下总结,详见代码。
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 UIImage *image = [UIImage imageNamed:@"018.png"]; 5 UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 6 imageView.frame = CGRectMake(80, 80, 200, 350); 7 [self.view addSubview:imageView]; 8 9 //开启用户交互 10 imageView.userInteractionEnabled = YES; 11 //缩放手势识别器 12 UIPinchGestureRecognizer *pinGes = [[UIPinchGestureRecognizer alloc] init]; 13 pinGes.delegate = self; 14 15 [pinGes addTarget:self action:@selector(pinGes:)]; 16 [imageView addGestureRecognizer:pinGes]; 17 18 //旋转手势识别器 19 UIRotationGestureRecognizer *rotaGes = [[UIRotationGestureRecognizer alloc] init]; 20 rotaGes.delegate = self; 21 imageView.userInteractionEnabled = YES; 22 [rotaGes addTarget:self action:@selector(rota:)]; 23 [imageView addGestureRecognizer:rotaGes]; 24 25 26 //移动手势识别器 27 UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] init]; 28 [panGes addTarget:self action:@selector(panGes:)]; 29 [imageView addGestureRecognizer:panGes]; 30 31 //长按手势识别器 32 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init]; 33 34 //设置长按时间标准(默认时间为0.5秒) 35 longPress.minimumPressDuration = 1.0; 36 [longPress addTarget:self action:@selector(longPress:)]; 37 [imageView addGestureRecognizer:longPress]; 38 39 } 40 41 //此方法的返回值表示手势识别器是否支持多手势操作 42 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 43 { 44 return YES; 45 } 46 47 //长按操作 48 - (void) longPress: (UILongPressGestureRecognizer *)ges 49 { 50 //开始长按时进行的操作 51 if (ges.state == UIGestureRecognizerStateBegan) { 52 NSLog(@"begin"); 53 //结束长按时进行的操作 54 }else if (ges.state == UIGestureRecognizerStateEnded){ 55 NSLog(@"end"); 56 } 57 } 58 59 //移动操作 60 - (void) panGes: (UIPanGestureRecognizer *)ges 61 { 62 //获取相对于父视图移动的距离 63 CGPoint point = [ges translationInView:self.view]; 64 //开启动画 65 [UIView beginAnimations:Nil context:Nil]; 66 //设置动画时间 67 [UIView setAnimationDuration:1]; 68 //通过形变属性移动 69 ges.view.transform = CGAffineTransformTranslate(ges.view.transform, point.x, point.y); 70 //因为手势识别器会对每次移动的距离进行累加,所以当移动一次后,需要将相对移动距离设置为(0,0); 71 [ges setTranslation:CGPointMake(0, 0) inView:self.view]; 72 73 //通过中心点移动 74 // ges.view.center = CGPointMake(ges.view.center.x + point.x, ges.view.center.y + point.y); 75 // [ges setTranslation:CGPointMake(0, 0) inView:self.view]; 76 //动画结束 77 [UIView commitAnimations]; 78 } 79 80 //旋转操作 81 - (void) rota: (UIRotationGestureRecognizer *)ges 82 { 83 //设置形变属性 84 ges.view.transform = CGAffineTransformRotate(ges.view.transform, ges.rotation); 85 //因为CGAffineTransformRotate函数会将每一次的旋转角度进行叠加,所以需要将手势识别器的旋转角度置0; 86 ges.rotation = 0; 87 } 88 89 //缩放操作 90 - (void)pinGes: (UIPinchGestureRecognizer *)ges 91 { 92 // NSLog(@"get in..."); 93 //因为CGAffineTransformScale函数会将缩放比例进行累乘,所以需要将手势识别器的缩放比例设置1 94 ges.view.transform = CGAffineTransformScale(ges.view.transform, ges.scale, ges.scale); 95 ges.scale = 1.0f; 96 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。