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即可。
轉載于:https://www.cnblogs.com/xubaoaichiyu/p/5314062.html
總結
- 上一篇: SpringMVC的环境搭建
- 下一篇: JVM-class文件完全解析-方法表集