IOS 七种手势详解(动图+Demo下载)

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的博客专栏,这个关于IOS SDK的专栏我会持续更新 IOS SDK详解

前言: 触摸是交互的核心,而手势是触摸的上层封装,易于使用,不易出错。本文介绍了7种常用手势,多数手势我都配合Core Animation举了一个例子。给读者一些参考。最后,Demo的链接我会放到最后。

Demo源代码下载

CSDN下载 GitHub下载

一 UIGestureRecognizer

UIGestureRecognizer是一个抽象类,定义了手势所需的一些基本的属性和方法。当然,也可以自定义自己的手势,近期不会更新相关文章,所以不在本文的考虑范畴。七种子类是常用的也是IOS SDK提供的类。

UITapGestureRecognizerUIPinchGestureRecognizerUIRotationGestureRecognizerUISwipeGestureRecognizerUIPanGestureRecognizerUIScreenEdgePanGestureRecognizerUILongPressGestureRecognizer

一些常用的方法

位置信息

获得触摸的位置- (CGPoint)locationInView:(UIView *)view获得某一个触摸的位置- (CGPoint)locationOfTouch:(NSUInteger)touchIndexinView:(UIView *)view当前手势有几个触摸- (NSUInteger)numberOfTouches

State

这个很重要,因为要通过State来判断手势的情况@property(nonatomic, readwrite) UIGestureRecognizerState state绑定手势的view@property(nonatomic, readonly) UIView *view

取消或者延迟

@property(nonatomic) BOOL cancelsTouchesInView@property(nonatomic) BOOL delaysTouchesBegan@property(nonatomic) BOOL delaysTouchesEnded

两个手势之间关系

– (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer二 UITapGestureRecognizer

Demo的效果图-tap一下,图片抖动

第一个手势,,我在storyboard上添加

然后,在响应手势的函数中添加动画

– (IBAction)tap:(UITapGestureRecognizer *)sender {CAKeyframeAnimation * animation = [CAKeyframeAnimation animation];animation.keyPath = @”position.x”;NSInteger initalPositionX = self.imageview.layer.position.x;animation.values = animation.keyTimes = animation.removedOnCompletion = YES;[self.imageview.layer addAnimation:animation forKey:@”keyFrame”];}

属性和方法介绍

@property(nonatomic) NSUInteger numberOfTapsRequired //手势需要tap几下 @property(nonatomic) NSUInteger numberOfTapsRequired //手势需要几根手指

三 UIPinchGestureRecognizer

效果如-pinch则放大图片

实现代码

-(void)viewWillAppear:(BOOL)animated{CGRect imageFrame;//按照设备的不同调整大小if ([[UIDevice currentDevice].model isEqualToString:@”ipad”]) {imageFrame = CGRectMake(0,0,300,200);}else{imageFrame = CGRectMake(0,0,240,160);}self.imageview = [[UIImageView alloc] initWithFrame:imageFrame];= [UIImage imageNamed:@”Image3.jpg”];= ;[];[self.imageview setUserInteractionEnabled:YES];//绑定手势UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];[self.imageview addGestureRecognizer:pinch];self.isLargeView = NO;;}-(void)pinch:(UIPinchGestureRecognizer *)pinch{if (pinch.state == UIGestureRecognizerStateRecognized) {if (!self.isLargeView && pinch.velocity > 0) {];= [UIColor blackColor];= 0.0;= [UIColor blueColor];[belowSubview:self.imageview];[UIView animateWithDuration:0.8delay:0.0options:UIViewAnimationOptionCurveEaseInOutanimations:^{= 1.0;if ([[UIDevice currentDevice].model isEqualToString:@”ipad”]) {= CGRectMake(0,300,768,512);}else{= CGRectMake(0,220,320,210);}}completion:^(BOOL finished) {self.isLargeView = YES;}];}if (self.isLargeView && pinch.velocity < 0) {[UIView animateWithDuration:0.8animations:^{= 0.0;= self.oldFrame;}completion:^(BOOL finished) {[self.backgroundView removeFromSuperview];self.backgroundView = nil;self.isLargeView = NO;}];}}}

属性和方法介绍 @property(nonatomic) CGFloat scale //scale的绝对值(相对最初的距离) @property(nonatomic, readonly) CGFloat velocity //速度

四 UIRotationGestureRecognizer不要轻言放弃,否则对不起自己

IOS 七种手势详解(动图+Demo下载)

相关文章:

你感兴趣的文章:

标签云: