生活随笔
收集整理的这篇文章主要介绍了
UIGestureRecognizer学习笔记
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
UIGestureRecognizer 是一个具体手势的基类,提供了较为简单的手势实现方式
The concrete subclasses of UIGestureRecognizer are the following:
一个gesture recognizer是针对一个特定的view的(包含其subview),用UIView的方法addGestureRecognize:去关联一个view
一个gesture recognizer是不参与UIView的事件响应链的
各个手势使用时的代码:
UITapGestureRecognizer
[cpp] view plaincopy
- (void)viewDidLoad { [super viewDidLoad]; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; if (![tapGesture respondsToSelector:@selector(locationInView:)]) { [tapGesture release]; tapGesture = nil; }else { tapGesture.delegate = self; tapGesture.numberOfTapsRequired = 1; tapGesture.numberOfTouchesRequired = 1; [self.view addGestureRecognizer:tapGesture]; } }
[cpp] view plaincopy
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer { UIView *view = [gestureRecognizer view]; switch (gestureRecognizer.state) { case UIGestureRecognizerStateEnded:{ NSLog(@"======UIGestureRecognizerStateEnded || UIGestureRecognizerStateRecognized"); break; } case UIGestureRecognizerStateFailed:{ NSLog(@"======UIGestureRecognizerStateFailed"); break; } case UIGestureRecognizerStatePossible:{ NSLog(@"======UIGestureRecognizerStatePossible"); break; } default:{ NSLog(@"======Unknow gestureRecognizer"); break; } } } - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ return YES; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return NO; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ return YES; }
UIPinchGestureRecognizer
[cpp] view plaincopy
- (void)viewDidLoad { [super viewDidLoad]; UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; if (![pinchGesture respondsToSelector:@selector(locationInView:)]) { [pinchGesture release]; pinchGesture = nil; }else { pinchGesture.delegate = self; [self.view addGestureRecognizer: pinchGesture]; } } - (void)handleGesture:(UIPinchGestureRecognizer *)gestureRecognizer { UIView *view = [gestureRecognizer view]; CGFloat scale = gestureRecognizer.scale; NSLog(@"======scale: %f", scale); CGFloat velocity = gestureRecognizer.velocity; NSLog(@"======scvelocityale: %f", velocity); switch (gestureRecognizer.state) { case UIGestureRecognizerStateEnded:{ NSLog(@"======UIGestureRecognizerStateEnded || UIGestureRecognizerStateRecognized"); break; } case UIGestureRecognizerStateBegan:{ NSLog(@"======UIGestureRecognizerStateBegan"); break; } case UIGestureRecognizerStateChanged:{ NSLog(@"======UIGestureRecognizerStateChanged"); gestureRecognizer.view.transform = CGAffineTransformScale(gestureRecognizer.view.transform, gestureRecognizer.scale, gestureRecognizer.scale); gestureRecognizer.scale = 1; break; } case UIGestureRecognizerStateCancelled:{ NSLog(@"======UIGestureRecognizerStateCancelled"); break; } case UIGestureRecognizerStateFailed:{ NSLog(@"======UIGestureRecognizerStateFailed"); break; } case UIGestureRecognizerStatePossible:{ NSLog(@"======UIGestureRecognizerStatePossible"); break; } default:{ NSLog(@"======Unknow gestureRecognizer"); break; } } }
UIRotationGestureRecognizer
[cpp] view plaincopy
- (void)viewDidLoad { [super viewDidLoad]; UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; if (![rotationGesture respondsToSelector:@selector(locationInView:)]) { [rotationGesture release]; rotationGesture = nil; }else { rotationGesture.delegate = self; [self.view addGestureRecognizer:rotationGesture]; } } - (void)handleGesture:(UIRotationGestureRecognizer *)gestureRecognizer { UIView *view = [gestureRecognizer view]; CGFloat rotation = gestureRecognizer.rotation; NSLog(@"===rotation: %f", rotation); CGFloat velocity = gestureRecognizer.velocity; NSLog(@"======velocity: %f", velocity); switch (gestureRecognizer.state) { case UIGestureRecognizerStateEnded:{ NSLog(@"======UIGestureRecognizerStateEnded || UIGestureRecognizerStateRecognized"); break; } case UIGestureRecognizerStateBegan:{ NSLog(@"======UIGestureRecognizerStateBegan"); break; } case UIGestureRecognizerStateChanged:{ NSLog(@"======UIGestureRecognizerStateChanged"); gestureRecognizer.view.transform = CGAffineTransformRotate(gestureRecognizer.view.transform, gestureRecognizer.rotation); gestureRecognizer.rotation = 0; break; } case UIGestureRecognizerStateCancelled:{ NSLog(@"======UIGestureRecognizerStateCancelled"); break; } case UIGestureRecognizerStateFailed:{ NSLog(@"======UIGestureRecognizerStateFailed"); break; } case UIGestureRecognizerStatePossible:{ NSLog(@"======UIGestureRecognizerStatePossible"); break; } default:{ NSLog(@"======Unknow gestureRecognizer"); break; } } }
UISwipeGestureRecognizer
[cpp] view plaincopy
- (void)viewDidLoad { [super viewDidLoad]; UISwipeGestureRecognizer *swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; if (![swipeGestureRight respondsToSelector:@selector(locationInView:)]) { [swipeGestureRight release]; swipeGestureRight = nil; }else { swipeGestureRight.delegate = self; swipeGestureRight.numberOfTouchesRequired = 1; swipeGestureRight.direction = UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:swipeGestureRight]; } UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; if (![swipeGestureLeft respondsToSelector:@selector(locationInView:)]) { [swipeGestureLeft release]; swipeGestureLeft = nil; }else { swipeGestureLeft.delegate = self; swipeGestureLeft.numberOfTouchesRequired = 1; swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; [self.view addGestureRecognizer:swipeGestureLeft]; } UISwipeGestureRecognizer *swipeGestureUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; if (![swipeGestureUp respondsToSelector:@selector(locationInView:)]) { [swipeGestureUp release]; swipeGestureUp = nil; }else { swipeGestureUp.delegate = self; swipeGestureUp.numberOfTouchesRequired = 1; swipeGestureUp.direction = UISwipeGestureRecognizerDirectionUp; [self.view addGestureRecognizer:swipeGestureUp]; } UISwipeGestureRecognizer *swipeGestureDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; if (![swipeGestureDown respondsToSelector:@selector(locationInView:)]) { [swipeGestureDown release]; swipeGestureDown = nil; }else { swipeGestureDown.delegate = self; swipeGestureDown.numberOfTouchesRequired = 1; swipeGestureDown.direction = UISwipeGestureRecognizerDirectionDown; [self.view addGestureRecognizer:swipeGestureDown]; } } - (void)handleGesture:(UISwipeGestureRecognizer *)gestureRecognizer { UIView *view = [gestureRecognizer view]; UISwipeGestureRecognizerDirection direction = gestureRecognizer.direction; switch (direction) { case UISwipeGestureRecognizerDirectionRight: { NSLog(@"direction==UISwipeGestureRecognizerDirectionRight"); break; } case UISwipeGestureRecognizerDirectionLeft: { NSLog(@"direction==UISwipeGestureRecognizerDirectionLeft"); break; } case UISwipeGestureRecognizerDirectionUp: { NSLog(@"direction==UISwipeGestureRecognizerDirectionUp"); break; } case UISwipeGestureRecognizerDirectionDown: { NSLog(@"direction==UISwipeGestureRecognizerDirectionDown"); break; } default: break; } switch (gestureRecognizer.state) { case UIGestureRecognizerStateEnded:{ NSLog(@"======UIGestureRecognizerStateEnded || UIGestureRecognizerStateRecognized"); break; } default:{ NSLog(@"======Unknow gestureRecognizer"); break; } } }
UIPanGestureRecognizer
[cpp] view plaincopy
- (void)viewDidLoad { [super viewDidLoad]; UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; view.backgroundColor = [UIColor blueColor]; [self.view addSubview:view]; UIPanGestureRecognizer *panPressGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; if (![panPressGesture respondsToSelector:@selector(locationInView:)]) { [panPressGesture release]; panPressGesture = nil; }else { panPressGesture.delegate = self; panPressGesture.maximumNumberOfTouches = NSUIntegerMax; panPressGesture.minimumNumberOfTouches = 1; [view addGestureRecognizer:panPressGesture]; } } - (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer { UIView *view = [gestureRecognizer view]; switch (gestureRecognizer.state) { case UIGestureRecognizerStateBegan:{ NSLog(@"======UIGestureRecognizerStateBegan"); break; } case UIGestureRecognizerStateChanged:{ NSLog(@"======UIGestureRecognizerStateChanged"); CGPoint translation = [gestureRecognizer translationInView:self.view]; view.center = CGPointMake(gestureRecognizer.view.center.x + translation.x, gestureRecognizer.view.center.y + translation.y); [gestureRecognizer setTranslation:CGPointMake(0, 0) inView:self.view]; break; } case UIGestureRecognizerStateCancelled:{ NSLog(@"======UIGestureRecognizerStateCancelled"); break; } case UIGestureRecognizerStateFailed:{ NSLog(@"======UIGestureRecognizerStateFailed"); break; } case UIGestureRecognizerStatePossible:{ NSLog(@"======UIGestureRecognizerStatePossible"); break; } case UIGestureRecognizerStateEnded:{ NSLog(@"======UIGestureRecognizerStateEnded || UIGestureRecognizerStateRecognized"); CGPoint velocity = [gestureRecognizer 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); float slideFactor = 0.1 * slideMult; CGPoint finalPoint = CGPointMake(view.center.x + (velocity.x * slideFactor), view.center.y + (velocity.y * slideFactor)); finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width); finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height); [UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ view.center = finalPoint; } completion:nil]; break; } default:{ NSLog(@"======Unknow gestureRecognizer"); break; } } }
UILongPressGestureRecognizer
[cpp] view plaincopy
- (void)viewDidLoad { [super viewDidLoad]; UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; if (![longPressGesture respondsToSelector:@selector(locationInView:)]) { [longPressGesture release]; longPressGesture = nil; }else { longPressGesture.delegate = self; longPressGesture.numberOfTapsRequired = 0; longPressGesture.minimumPressDuration = 0.1f; longPressGesture.numberOfTouchesRequired = 1; longPressGesture.allowableMovement = 10; [self.view addGestureRecognizer:longPressGesture]; } } - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer { UIView *view = [gestureRecognizer view]; switch (gestureRecognizer.state) { case UIGestureRecognizerStateEnded:{ NSLog(@"======UIGestureRecognizerStateEnded || UIGestureRecognizerStateRecognized"); break; } case UIGestureRecognizerStateBegan:{ NSLog(@"======UIGestureRecognizerStateBegan"); break; } case UIGestureRecognizerStateChanged:{ NSLog(@"======UIGestureRecognizerStateChanged"); break; } case UIGestureRecognizerStateCancelled:{ NSLog(@"======UIGestureRecognizerStateCancelled"); break; } case UIGestureRecognizerStateFailed:{ NSLog(@"======UIGestureRecognizerStateFailed"); break; } case UIGestureRecognizerStatePossible:{ NSLog(@"======UIGestureRecognizerStatePossible"); break; } default:{ NSLog(@"======Unknow gestureRecognizer"); break; } } } - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ CGPoint currentPoint = [gestureRecognizer locationInView:self.view]; if (CGRectContainsPoint(CGRectMake(0, 0, 100, 100), currentPoint) ) { return YES; } return NO; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return NO; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ return YES; }
转载于:https://www.cnblogs.com/Sucri/p/4704933.html
《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读
总结
以上是生活随笔为你收集整理的UIGestureRecognizer学习笔记的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。