iOS開發(fā)-六大手勢(tap、pan、swipe、longPress、rotation、pinch)

gesture.jpg

前言:相信大家在工作中對iOS手勢的使用是非常之多的,今天閑來無事對常用的六大手勢進(jìn)行了簡單的整理,在此系統(tǒng)的給大家簡單介紹一下基本的使用。

1、UITapGestureRecognizer輕觸手勢,這個在使用中可以說是用的最多的一種手勢,常用于給沒有點擊事件的控件添加該手勢達(dá)到類似于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拖拽手勢,常用于需要對控件進(jìn)行手動退拽以改變控件的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;
    }
    
    //非開始階段
    //獲得手勢移動的距離 現(xiàn)在的位置
    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輕掃手勢,常用于對控件往對應(yīng)的方向有滑動操作時的場景中,具體用法如下:

#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:)];
    //設(shè)置掃的方向
    swipe1.direction = UISwipeGestureRecognizerDirectionUp;
    [imageView addGestureRecognizer:swipe1];
    
    UISwipeGestureRecognizer * swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeImage:)];
    //設(shè)置掃的方向
    swipe2.direction = UISwipeGestureRecognizerDirectionLeft;
    [imageView addGestureRecognizer:swipe2];
    
    UISwipeGestureRecognizer * swipe3 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeImage:)];
    //設(shè)置掃的方向
    swipe3.direction = UISwipeGestureRecognizerDirectionDown;
    [imageView addGestureRecognizer:swipe3];
    
    UISwipeGestureRecognizer * swipe4 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeImage:)];
    //設(shè)置掃的方向
    swipe4.direction = UISwipeGestureRecognizerDirectionRight;
    [imageView addGestureRecognizer:swipe4];
}

-(void)swipeImage:(UISwipeGestureRecognizer *)swipe{
    //實現(xiàn)要添加的功能
    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長按手勢,常用于對控件進(jìn)行長按操作時的場景中,具體用法如下:

#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(@"長按結(jié)束");
        [UIView animateWithDuration:1.0 animations:^{
            
            imageView.frame = CGRectMake(0, 0, 150, 100);
            imageView.center = self.view.center;
        }];
    }
}
@end

效果圖如下:

longPress.gif

5、UIRotationGestureRecognizer旋轉(zhuǎn)手勢,常用于對控件的旋轉(zhuǎn)操作的場景中,具體用法如下:

#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;
    
    // 旋轉(zhuǎn)圖片
    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 ()

// 圖片初始縮放系數(shù)
@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

總結(jié):每種手勢創(chuàng)建的時候都是通過- (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action方法創(chuàng)建的,都對應(yīng)的有相應(yīng)的手勢事件,在相應(yīng)的手勢事件中可以對添加手勢的控件或者其他控件做出需求需要的操作。代碼很簡答,不過小編還是寫了demo,在此共享給大家,地址如下:
https://github.com/guorenhao/GestureDemo.git

最后,還是希望該文章能夠幫助到有需要的猿友們,可以解了有需求的朋友的燃眉之急,愿我們能夠共同進(jìn)步,在開發(fā)的道路上越走越遠(yuǎn),謝謝!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,057評論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,355評論 4 61
  • 昨晚猶豫很久要不要買張票一個人去看 對一個人看電影 以前的我從未想過會有一天 我會一個人獨自去看電影 因為我覺得自...
    箱子里的我閱讀 479評論 6 1
  • 有人在群里分享了一首歌《漁火閃閃》,很動聽,瑩瑩暖暖的。深白今天錄了一首歌《保留》,前奏特別美,婉轉(zhuǎn)悠揚,像心的聲...
    LOVE玲媛閱讀 511評論 0 0
  • 手里握著一張開往南方的火車票,背著簡單的行囊,只身一人排在等候檢票的隊伍里,做短暫的出游。人群里滿是期待和興奮的表...
    紫陌淺舞閱讀 385評論 0 0

友情鏈接更多精彩內(nèi)容