OC 触摸事件
来源:互联网 发布:手游存档软件 编辑:程序博客网 时间:2024/06/09 22:51
下面是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
- OC 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- VS2015输入反斜杠显示成人民币符号的问题
- 指示项,轮播图,圆角图片可拉伸,popwindow
- Android 开发如何做好内存优化
- 使用OutputDebugString帮助调试
- JSP 文件上传
- OC 触摸事件
- 扣丁学堂——ActionBar
- python中的多线程
- 【学习笔记】自定义关于wchar_t*的相关函数
- Makefile各类符号
- 动画气泡指示当前滑动值--第三方开源--DiscreteSeekbar
- 服务器启动时Webapp的web.xml中配置的加载顺序
- 基本套接字编程(3) -- select篇
- mybatis 加二级缓存