手势
来源:互联网 发布:英汉同声翻译软件 编辑:程序博客网 时间:2024/05/01 18:22
手势包括:
- 拖拽 (pan)
- 划屏 (swipe)
- 旋转 (rotate)
- 捏合 (pinch)
- 点击 (tap)
- 长按 (long press)
Xcode自带模拟器模拟2个手指时,需要按住Option,配合触控板(鼠标)使用。
添加手势
通过给view addGestureRecognizer来添加一个手势
1.划屏
self.swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];self.swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;//向右滑动触发handleSwipes方法self.swipeGestureRecognizer.numberOfTouchesRequired = 1;//一个手指[self.view addGestureRecognizer:self.swipeGestureRecognizer];
handleSwipes
- (void)handleSwipes: (UISwipeGestureRecognizer *)sender { if (sender.direction == UISwipeGestureRecognizerDirectionDown) { NSLog(@"Swip down"); } else if (sender.direction == UISwipeGestureRecognizerDirectionUp) { NSLog(@"Swip up"); } else if (sender.direction == UISwipeGestureRecognizerDirectionLeft) { NSLog(@"Swip left"); } else if (sender.direction == UISwipeGestureRecognizerDirectionRight) { NSLog(@"Swip right"); }}
2.旋转
self.rotationGestureRecongnizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotation:)];[self.view addGestureRecognizer:_rotationGestureRecongnizer];
handleRotation
- (void)handleRotation: (UIRotationGestureRecognizer *)sender { _imageView.transform = CGAffineTransformMakeRotation(self.rotationAngleInRadians + sender.rotation); if (sender.state == UIGestureRecognizerStateEnded) { self.rotationAngleInRadians += sender.rotation; }}
rotationAngleInRadians是自定义的property
拖拽
_panGestureRecongizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];[self.view addGestureRecognizer:_panGestureRecongizer];
handlePan
- (void)handlePan: (UIPanGestureRecognizer *)sender { if (sender.state != UIGestureRecognizerStateEnded && sender.state != UIGestureRecognizerStateFailed) { CGPoint location = [sender locationInView:sender.view.superview]; self.imageView.center = location;//更新位置 }}
捏合
_pinchGestrueRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinches:)];[self.view addGestureRecognizer:_pinchGestrueRecognizer];
handlePinches
- (void)handlePinches:(UIPinchGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateEnded) { _currentScale = sender.scale; } else if (sender.state == UIGestureRecognizerStateBegan && _currentScale != 0.0f) { sender.scale = _currentScale; } if (sender.scale != NAN && _currentScale != 0.0f) { sender.view.transform = CGAffineTransformMakeScale(sender.scale, sender.scale);//更新图像 }}
点击
_tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];_tapGestureRecognizer.numberOfTouchesRequired = 2;//需要2个手指才有效_tapGestureRecognizer.numberOfTapsRequired = 3;//需要点击3下[self.view addGestureRecognizer:_tapGestureRecognizer];
handleTap
- (void)handleTap: (UITapGestureRecognizer *)sender { for (NSUInteger touchCount = 0; touchCount < _tapGestureRecognizer.numberOfTouchesRequired; touchCount++) { CGPoint touchPoint = [sender locationOfTouch:touchCount inView:sender.view]; NSLog(@"Touch %lu, %@", touchCount + 1, NSStringFromCGPoint(touchPoint)); }}
长按
_longGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongpresses:)];_longGestureRecognizer.numberOfTouchesRequired = 2;//需要2个手指_longGestureRecognizer.allowableMovement = 100;_longGestureRecognizer.minimumPressDuration = 1.0;//最少按1s[self.view addGestureRecognizer:_longGestureRecognizer];
handleLongpresses
- (void)handleLongpresses: (UILongPressGestureRecognizer *)sender { if ([sender isEqual:_longGestureRecognizer]) { if (sender.numberOfTouchesRequired == _longGestureRecognizer.numberOfTouchesRequired) { CGPoint touchPoint1 = [sender locationOfTouch:0 inView:sender.view]; CGPoint touchPoint2 = [sender locationOfTouch:1 inView:sender.view]; CGFloat midPointX = (touchPoint1.x + touchPoint2.x) / 2.0; CGFloat midPointY = (touchPoint1.y + touchPoint2.y) / 2.0; CGPoint midPoint = CGPointMake(midPointX, midPointY); _imageView.center = midPoint;//更新位置 } else { NSLog(@"Long press, fingers' not right"); } }}
效果
0 0
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- Node.js HelloWord Linux(Ubuntu)
- 日经春秋 20150829
- 设计模式(一):单例模式
- hdu 5402 Travelling Salesman Problem
- iOS学习之CoreData模糊查找,分页查找,多模型文件处理
- 手势
- 日经社説 20150829 課題残る新競技場の整備計画
- linux命令(2):gzip命令
- 重载引用参数(c++ primer plus -6th)
- [C#]增强响应性,用加载窗体(Splash)来载入主窗体
- typeof constructor instanceof 判断类型
- 日经社説 20150829 農家のための農協へ抜本改革を着実に
- CString与char*转换(Unicode和多字节字符集)
- HDU 5341 Gcd and Lcm