IOS 手勢詳解,ios手勢詳解
在IOS中手勢可以讓使用者有很好的體驗,因此我們有必要去瞭解一下手勢。
(在設定手勢是有很多值得注意的地方)
*是需要設定為Yes的點擊無法響應*
*要把手勢添加到所需點擊的View,否則無法響應*
手勢共有六種,下面我會分開介紹。
點選手勢
//// ViewController.m// CX-手勢詳解//// Created by ma c on 16/3/24.// Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (nonatomic, strong) UIImageView * imageView;@end@implementation ViewController#pragma mark - set_and_get-(UIImageView *)imageView{ if (!_imageView) { _imageView = [[UIImageView alloc]init]; UIImage * image = [UIImage imageNamed:@"nvshen.jpg"]; _imageView.bounds = (CGRect){CGPointZero,image.size}; _imageView.center = self.view.center; //互動一定要設定為YES 否則無法實現手勢 _imageView.userInteractionEnabled = YES; _imageView.image = image; } return _imageView;}- (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.imageView]; //點選手勢 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doAction:)]; //點擊一下生效 tap.numberOfTapsRequired = 1; UITapGestureRecognizer * tapNew = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doAction:)]; //點擊兩下生效 tapNew.numberOfTapsRequired = 2; //在imageView上添加手勢 [self.imageView addGestureRecognizer:tap]; [self.imageView addGestureRecognizer:tapNew]; //當點擊兩下生效時,使點擊一下失效 [tap requireGestureRecognizerToFail:tapNew]; }-(void)doAction:(UITapGestureRecognizer *)tap{ if (tap.numberOfTapsRequired == 1) { NSLog(@"點擊一下"); }else if(tap.numberOfTapsRequired == 2 ){ NSLog(@"點擊兩下"); } }@end
拖動手勢
//// ViewController.m// CX-手勢詳解//// Created by ma c on 16/3/24.// Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (nonatomic, strong) UIImageView * imageView;@end@implementation ViewController#pragma mark - set_and_get-(UIImageView *)imageView{ if (!_imageView) { _imageView = [[UIImageView alloc]init]; UIImage * image = [UIImage imageNamed:@"nvshen.jpg"]; _imageView.bounds = (CGRect){CGPointZero,image.size}; _imageView.center = self.view.center; //互動一定要設定為YES 否則無法實現手勢 _imageView.userInteractionEnabled = YES; _imageView.image = image; } return _imageView;}- (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.imageView]; //拖動手勢 UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(doAction:)]; [self.view addGestureRecognizer:pan]; }-(void)doAction:(UIPanGestureRecognizer *)pan{ //擷取位移量 CGPoint point = [pan translationInView:self.imageView]; //通過改變self。imageView的Center來實現拖動 self.imageView.center = CGPointMake(self.imageView.center.x + point.x , self.imageView.center.y + point.y); //複位 如果不進行複位 會在改變的基礎上改變 從而使效果不對 [pan setTranslation:CGPointZero inView:self.imageView]; }@end
長按手勢
//// ViewController.m// CX-手勢詳解//// Created by ma c on 16/3/24.// Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (nonatomic, strong) UIImageView * imageView;@end@implementation ViewController#pragma mark - set_and_get-(UIImageView *)imageView{ if (!_imageView) { _imageView = [[UIImageView alloc]init]; UIImage * image = [UIImage imageNamed:@"nvshen.jpg"]; _imageView.bounds = (CGRect){CGPointZero,image.size}; _imageView.center = self.view.center; //互動一定要設定為YES 否則無法實現手勢 _imageView.userInteractionEnabled = YES; _imageView.image = image; } return _imageView;}- (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.imageView]; //長按手勢 UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(doAction:)]; [self.imageView addGestureRecognizer:longPress]; }-(void)doAction:(UILongPressGestureRecognizer *)longPress{ if (longPress.state == UIGestureRecognizerStateBegan) { NSLog(@"開始"); } else if (longPress.state == UIGestureRecognizerStateEnded){ NSLog(@"結束"); } }@end
撥動手勢
//// ViewController.m// CX-手勢詳解//// Created by ma c on 16/3/24.// Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (nonatomic, strong) UIImageView * imageView;@end@implementation ViewController#pragma mark - set_and_get-(UIImageView *)imageView{ if (!_imageView) { _imageView = [[UIImageView alloc]init]; UIImage * image = [UIImage imageNamed:@"nvshen.jpg"]; _imageView.bounds = (CGRect){CGPointZero,image.size}; _imageView.center = self.view.center; //互動一定要設定為YES 否則無法實現手勢 _imageView.userInteractionEnabled = YES; _imageView.image = image; } return _imageView;}- (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.imageView]; //撥動手勢 UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(doAction:)]; //需要設定 預設為右 /* 預設是UISwipeGestureRecognizerDirectionRight。所需的方向刷。可指定多個方向是否會導致相同的行為(例如,UITableView滑動刪除) */ swipe.direction = UISwipeGestureRecognizerDirectionLeft; [self.imageView addGestureRecognizer:swipe]; }-(void)doAction:(UISwipeGestureRecognizer *)swipe{ if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) { NSLog(@"左"); } else if (swipe.direction == UISwipeGestureRecognizerDirectionRight){ NSLog(@"右"); } else if (swipe.direction == UISwipeGestureRecognizerDirectionDown){ NSLog(@"下"); } else if (swipe.direction == UISwipeGestureRecognizerDirectionUp){ NSLog(@"上"); }}@end
捏合手勢
(在捏合和旋轉手勢中我們需要一些操作)
*按住option 在觸碰到觸摸板的時候會出現類比出現的兩根手指*
*如果你所操作的view不在兩個觸摸點的位置,可以按住shift進行移動*
*當進行捏合旋轉的時候,一定要把觸摸板按下,才可進行操作*
//// ViewController.m// CX-手勢詳解//// Created by ma c on 16/3/24.// Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (nonatomic, strong) UIImageView * imageView;@end@implementation ViewController#pragma mark - set_and_get-(UIImageView *)imageView{ if (!_imageView) { _imageView = [[UIImageView alloc]init]; UIImage * image = [UIImage imageNamed:@"nvshen.jpg"]; _imageView.bounds = (CGRect){CGPointZero,image.size}; _imageView.center = self.view.center; //互動一定要設定為YES 否則無法實現手勢 _imageView.userInteractionEnabled = YES; _imageView.image = image; } return _imageView;}- (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.imageView]; //捏合手勢 UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(doAction:)]; [self.imageView addGestureRecognizer:pinch]; }-(void)doAction:(UIPinchGestureRecognizer *)pinch{ //持續改變 self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale); //複位 pinch.scale = 1; }@end
旋轉手勢
//// ViewController.m// CX-手勢詳解//// Created by ma c on 16/3/24.// Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (nonatomic, strong) UIImageView * imageView;@end@implementation ViewController#pragma mark - set_and_get-(UIImageView *)imageView{ if (!_imageView) { _imageView = [[UIImageView alloc]init]; UIImage * image = [UIImage imageNamed:@"nvshen.jpg"]; _imageView.bounds = (CGRect){CGPointZero,image.size}; _imageView.center = self.view.center; //互動一定要設定為YES 否則無法實現手勢 _imageView.userInteractionEnabled = YES; _imageView.image = image; } return _imageView;}- (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.imageView]; //旋轉手勢 UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(doAction:)]; [self.imageView addGestureRecognizer:rotation]; }-(void)doAction:(UIRotationGestureRecognizer *)rotation{ //持續改變 self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation); //複位 rotation.rotation = 0; }@end
有一點值得注意的是,旋轉手勢和捏合手勢是不可以同時操作的,想要同時操作可以通過代理實現,如下。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
在上面的代碼實現時返回YES即可。