iOS开发-六大手势(tap、pan、swipe、longPress、rotation、pinch)

gesture.jpg

前言:相信大家在工作中对iOS手势的使用是非常之多的,今天闲来无事对常用的六大手势进行了简单的整理,在此系统的给大家简单介绍一下基本的使用。

1、UITapGestureRecognizer轻触手势,这个在使用中可以说是用的最多的一种手势,常用于给没有点击事件的控件添加该手势达到类似于button点击事件的效果,具体用法如下:

#import "TapViewController.h"

@interface TapViewController ()

@end

@implementation TapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self test];
}

- (void)test {
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150, 100)];
    imageView.center = self.view.center;
    imageView.userInteractionEnabled = YES;
    imageView.image = [UIImage imageNamed:@"c001"];
    [self.view addSubview:imageView];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)];
    [imageView addGestureRecognizer:tap];
}

- (void)tapImage:(UITapGestureRecognizer *)tap {
    
    UIImageView * imageView = (UIImageView *)tap.view;
    
    static int flag = 0;
    flag ^= 1;
    if (flag) {
        
        imageView.frame = CGRectMake(0, 0, 300, 200);
        NSLog(@"点击了图片放大!");
    } else {
        
        imageView.frame = CGRectMake(0, 0, 150, 100);
        NSLog(@"点击了图片缩小!");
    }
    imageView.center = self.view.center;
}
@end

效果图如下:

tap.gif

2、UIPanGestureRecognizer拖拽手势,常用于需要对控件进行手动退拽以改变控件的frame的场景中,具体用法如下:

#import "PanViewController.h"

@interface PanViewController ()

// 图片初始中心点
@property (nonatomic) CGPoint startImageCenter;
// 手势初始中心点
@property (nonatomic) CGPoint startGCenter;

@end

@implementation PanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self test];
}

- (void)test {
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 80, 200, 150)];
    imageView.userInteractionEnabled = YES;
    imageView.image = [UIImage imageNamed:@"c001"];
    [self.view addSubview:imageView];
    
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panImage:)];
    [imageView addGestureRecognizer:pan];
}

- (void)panImage:(UIPanGestureRecognizer *)pan {
    
    UIImageView * imageView = (UIImageView *)pan.view;
    
    if (pan.state == UIGestureRecognizerStateBegan) {
        
        //记录中心位置
        self.startImageCenter = imageView.center;
        self.startGCenter = [pan locationInView:self.view];
        return;
    }
    
    //非开始阶段
    //获得手势移动的距离 现在的位置
    CGPoint nowGCenter = [pan locationInView:self.view];
    float x = nowGCenter.x - self.startGCenter.x;
    float y = nowGCenter.y - self.startGCenter.y;
    //计算imageview的相对位移
    imageView.center = CGPointMake(self.startImageCenter.x+x, self.startImageCenter.y+y);
    
//    if (pan.state == UIGestureRecognizerStateEnded) {
//        
//        imageView.center = self.startImageCenter;
//    }
}

效果图如下:

pan.gif

3、UISwipeGestureRecognizer轻扫手势,常用于对控件往对应的方向有滑动操作时的场景中,具体用法如下:

#import "SwipeViewController.h"

@interface SwipeViewController ()

@end

@implementation SwipeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self test];
}

- (void)test {
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 80, 200, 150)];
    imageView.center = self.view.center;
    imageView.userInteractionEnabled = YES;
    imageView.image = [UIImage imageNamed:@"c001"];
    [self.view addSubview:imageView];
    
    UISwipeGestureRecognizer * swipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeImage:)];
    //设置扫的方向
    swipe1.direction = UISwipeGestureRecognizerDirectionUp;
    [imageView addGestureRecognizer:swipe1];
    
    UISwipeGestureRecognizer * swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeImage:)];
    //设置扫的方向
    swipe2.direction = UISwipeGestureRecognizerDirectionLeft;
    [imageView addGestureRecognizer:swipe2];
    
    UISwipeGestureRecognizer * swipe3 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeImage:)];
    //设置扫的方向
    swipe3.direction = UISwipeGestureRecognizerDirectionDown;
    [imageView addGestureRecognizer:swipe3];
    
    UISwipeGestureRecognizer * swipe4 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeImage:)];
    //设置扫的方向
    swipe4.direction = UISwipeGestureRecognizerDirectionRight;
    [imageView addGestureRecognizer:swipe4];
}

-(void)swipeImage:(UISwipeGestureRecognizer *)swipe{
    //实现要添加的功能
    NSLog(@"扫到了图片");
    UIImageView * imageView = (UIImageView *)swipe.view;
    
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        
        imageView.center = CGPointMake(imageView.center.x - 40, imageView.center.y);
        NSLog(@"图片向左移动了");
    }else if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        
        imageView.center = CGPointMake(imageView.center.x + 40, imageView.center.y);
        NSLog(@"图片向右移动了");
    }else if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
        
        imageView.center = CGPointMake(imageView.center.x, imageView.center.y + 40);
        NSLog(@"图片向下移动了");
    }else if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
        
        imageView.center = CGPointMake(imageView.center.x, imageView.center.y - 40);
        NSLog(@"图片向上移动了");
    }  
}
@end

效果图如下:

swipe.gif

4、UILongPressGestureRecognizer长按手势,常用于对控件进行长按操作时的场景中,具体用法如下:

#import "LongPressViewController.h"

@interface LongPressViewController ()

@end

@implementation LongPressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self test];
}

- (void)test {
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150, 100)];
    imageView.center = self.view.center;
    imageView.userInteractionEnabled = YES;
    imageView.image = [UIImage imageNamed:@"c001"];
    [self.view addSubview:imageView];
    
    UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressImage:)];
    // 长按时间
    longPress.minimumPressDuration = 2;
    [imageView addGestureRecognizer:longPress];
}


-(void)longPressImage:(UILongPressGestureRecognizer *)longPress{
    
    UIImageView * imageView = (UIImageView *)longPress.view;
    
    if (longPress.state == UIGestureRecognizerStateBegan) {
        
        NSLog(@"长按了这张图片");
        [UIView animateWithDuration:1.0 animations:^{
            
            imageView.frame = CGRectMake(0, 0, 300, 200);
            imageView.center = self.view.center;
        }];
    }
    
    if (longPress.state == UIGestureRecognizerStateEnded) {
        
        NSLog(@"长按结束");
        [UIView animateWithDuration:1.0 animations:^{
            
            imageView.frame = CGRectMake(0, 0, 150, 100);
            imageView.center = self.view.center;
        }];
    }
}
@end

效果图如下:

longPress.gif

5、UIRotationGestureRecognizer旋转手势,常用于对控件的旋转操作的场景中,具体用法如下:

#import "RotationViewController.h"

@interface RotationViewController ()

// 图片初始角度
@property (nonatomic, assign) float startRotation;
@end

@implementation RotationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self test];
}

- (void)test {
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
    imageView.center = self.view.center;
    imageView.userInteractionEnabled = YES;
    imageView.image = [UIImage imageNamed:@"c001"];
    [self.view addSubview:imageView];
    
    UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationImage:)];
    [imageView addGestureRecognizer:rotation];
}

-(void)rotationImage:(UIRotationGestureRecognizer *)rotation{
    
    UIImageView * imageView = (UIImageView *)rotation.view;
    
    // 旋转图片
    imageView.transform = CGAffineTransformMakeRotation(rotation.rotation + self.startRotation);
    
    if (rotation.state == UIGestureRecognizerStateEnded) {
        
        self.startRotation += rotation.rotation;
    }
}
@end

效果图如下:

rotation.gif

6、UIPinchGestureRecognizer捏合缩放手势,常用于对图片的捏合缩放的操作场景中,具体用法如下:

#import "PinchViewController.h"

@interface PinchViewController ()

// 图片初始缩放系数
@property (nonatomic, assign) float startScale;
@end

@implementation PinchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self test];
}

- (void)test {
    
    self.startScale = 1.0;
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
    imageView.center = self.view.center;
    imageView.userInteractionEnabled = YES;
    imageView.image = [UIImage imageNamed:@"c001"];
    [self.view addSubview:imageView];
    
    UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchImage:)];
    [imageView addGestureRecognizer:pinch];
}


-(void)pinchImage:(UIPinchGestureRecognizer *)pinch{
    
    UIImageView * imageView = (UIImageView *)pinch.view;
    
    // 缩放图片
    imageView.transform = CGAffineTransformMakeScale(pinch.scale * self.startScale, pinch.scale * self.startScale);
    
    if (pinch.state == UIGestureRecognizerStateEnded) {
        
        self.startScale *= pinch.scale;
        if (self.startScale >= 2.0) {
            
            self.startScale = 2.0;
            [UIView animateWithDuration:0.3 animations:^{
                
                imageView.transform = CGAffineTransformMakeScale(self.startScale, self.startScale);
            }];
        } else if (self.startScale <= 0.5) {
            
            self.startScale = 0.5;
            [UIView animateWithDuration:0.3 animations:^{
                
                imageView.transform = CGAffineTransformMakeScale(self.startScale, self.startScale);
            }];
        }
    }  
}
@end

效果图如下:

pinch.gif

总结:每种手势创建的时候都是通过- (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action方法创建的,都对应的有相应的手势事件,在相应的手势事件中可以对添加手势的控件或者其他控件做出需求需要的操作。代码很简答,不过小编还是写了demo,在此共享给大家,地址如下:
https://github.com/guorenhao/GestureDemo.git

最后,还是希望该文章能够帮助到有需要的猿友们,可以解了有需求的朋友的燃眉之急,愿我们能够共同进步,在开发的道路上越走越远,谢谢!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,585评论 4 365
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,923评论 1 301
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,314评论 0 248
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,346评论 0 214
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,718评论 3 291
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,828评论 1 223
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,020评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,758评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,486评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,722评论 2 251
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,196评论 1 262
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,546评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,211评论 3 240
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,132评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,916评论 0 200
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,904评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,758评论 2 274

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,585评论 25 707
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,631评论 4 59
  • 昨晚犹豫很久要不要买张票一个人去看 对一个人看电影 以前的我从未想过会有一天 我会一个人独自去看电影 因为我觉得自...
    箱子里的我阅读 373评论 6 1
  • 有人在群里分享了一首歌《渔火闪闪》,很动听,莹莹暖暖的。深白今天录了一首歌《保留》,前奏特别美,婉转悠扬,像心的声...
    LOVE玲媛阅读 390评论 0 0
  • 手里握着一张开往南方的火车票,背着简单的行囊,只身一人排在等候检票的队伍里,做短暂的出游。人群里满是期待和兴奋的表...
    紫陌浅舞阅读 319评论 0 0