首页 > 代码库 > 我的IOS学习之路(三):手势识别器
我的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 *)otherGestureRecognizer43 {44 return YES;45 }46 47 //长按操作48 - (void) longPress: (UILongPressGestureRecognizer *)ges49 {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 *)ges61 {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 *)ges82 {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 *)ges91 {92 // NSLog(@"get in...");93 //因为CGAffineTransformScale函数会将缩放比例进行累乘,所以需要将手势识别器的缩放比例设置194 ges.view.transform = CGAffineTransformScale(ges.view.transform, ges.scale, ges.scale);95 ges.scale = 1.0f;96 }
我的IOS学习之路(三):手势识别器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。