您的位置:首页 > 移动开发 > IOS开发

iOS中的手势

2016-05-10 00:00 423 查看
手势

单击或者双击手势

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

//单击或者双击手势
[self addTapGesture];

}

//单击或者双击手势的方法
-(void)addTapGesture{

//单击
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]init];
//手指数量
tap1.numberOfTouchesRequired = 1;
//单击次数
tap1.numberOfTapsRequired = 1;
//添加单击方法
[tap1 addTarget:self action:@selector(haha:)];
[self.view addGestureRecognizer:tap1];

//双击
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]init];
//手指数量
tap2.numberOfTouchesRequired = 1;
//双击次数
tap2.numberOfTapsRequired = 2;
//添加双击方法
[tap2 addTarget:self action:@selector(haha2:)];
[self.view addGestureRecognizer:tap2];

//!!!注意:如果单击和双击同时进行,则取消单击手势(否则,如果单纯双击的话,会先出现一个单击,再出现一个双击)
[tap1 requireGestureRecognizerToFail:tap2];

}

//单击方法
-(void)haha:(UITapGestureRecognizer *)tap{

NSLog(@"单击");
}

//双击方法
-(void)haha2:(UITapGestureRecognizer *)tap{

NSLog(@"双击");
}


轻扫手势

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

//轻扫手势
[self addSwipeGesture];

}

//轻扫手势
-(void)addSwipeGesture{

UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]init];

//设置轻扫方向(如果下面要添加不同方向的方法,则这里不要设置,视具体情况而定)
swipe.direction = UISwipeGestureRecognizerDirectionUp;
//添加轻扫方法
[swipe addTarget:self action:@selector(haha:)];
[self.view addGestureRecognizer:swipe];

}

//轻扫方法
-(void)haha:(UISwipeGestureRecognizer *)swipe{

if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {

NSLog(@"向右轻扫");
}else if (swipe.direction == UISwipeGestureRecognizerDirectionLeft){

NSLog(@"向左轻扫");
}else if (swipe.direction == UISwipeGestureRecognizerDirectionUp){

NSLog(@"向上轻扫");
}

}


移动手势

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

//移动手势
[self addPanGesture];

}

//移动手势
-(void)addPanGesture{

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]init];
//添加移动手势的方法
[pan addTarget:self action:@selector(haha:)];
[self.view addGestureRecognizer:pan];

}

//移动手势的方法
-(void)haha:(UIPanGestureRecognizer *)pan{

NSLog(@"移动");
}


长按手势

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

//长按手势
[self addLongPressGesture];

}

//长按手势
-(void)addLongPressGesture{

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]init];

//长按开始时间(手指按住屏幕5秒,才会开始进入长按时间)
longPress.minimumPressDuration = 5;
//长按手势方法
[longPress addTarget:self action:@selector(haha:)];
[self.view addGestureRecognizer:longPress];

}

//长按手势的方法
-(void)haha:(UILongPressGestureRecognizer *)longPress{

if (longPress.state == UIGestureRecognizerStateBegan) {

NSLog(@"长按开始");
}else if (longPress.state == UIGestureRecognizerStateEnded){

NSLog(@"长按结束");
}

}


旋转手势

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

//旋转手势
[self addRotationGesture];

}

//旋转手势
-(void)addRotationGesture{

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]init];
//旋转手势方法
[rotation addTarget:self action:@selector(haha:)];
[self.view addGestureRecognizer:rotation];

}

//旋转手势的方法
-(void)haha:(UIRotationGestureRecognizer *)rotition{

NSLog(@"旋转");

//获取旋转的弧度制
CGFloat r = rotition.rotation;
float degress = 180/M_PI * r;
NSLog(@"%f", degress);

}


捏合手势

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

//捏合手势
[self addPinchGesture];

}

//捏合手势
-(void)addPinchGesture{

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]init];
//捏合手势方法
[pinch addTarget:self action:@selector(haha:)];
[self.view addGestureRecognizer:pinch];

}

//捏合手势的方法
-(void)haha:(UIPinchGestureRecognizer *)pinch{

NSLog(@"捏合手势");

CGFloat scale = [pinch scale];

self.view.transform = CGAffineTransformScale(self.view.transform, scale, scale);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: