1.手势
将用户物理性触屏操作转换成对象存储起来,所有手势的父类UIGestureReconginzer
手势包括:
Tap:点击手势
Swipe:轻扫手势
注意:以上两个手势是一次性手势
LongPress:长按手势
Pan:拖拽手势
Pinch:捏合手势
Rotation:旋转手势
如何使用手势:
1创建指定手势对象,在创建时设定好当该类型手势触发时,系统会自动调用什么方法
2设置手势的核心属性
3将手势添加到某个视图中,即代表,当用户在该视图上触发了对应的手势,系统会自动捕获并调用手势对应的方法
image.png
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
//点击手势
-(void)tap{
UITapGestureRecognizer *tap= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
//设置核心 属性
//需要几个手指点
//tap.numberOfTouchesRequired = 2;
tap.numberOfTapsRequired = 1;
//将手势添加到视图上
[self.imageView addGestureRecognizer:tap];
}
//模拟两只手按住alt
-(void)tap:(UITapGestureRecognizer*)gesture{
NSLog(@"点击了 imageView");
}
//轻扫
-(void)swipe {
UISwipeGestureRecognizer *swipe= [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
//设置核心 属性
//只能上下或左右
swipe.direction = UISwipeGestureRecognizerDirectionUp |UISwipeGestureRecognizerDirectionDown; //将手势添加到视图上
[self.imageView addGestureRecognizer:swipe];
}
-(void)swipe:(UISwipeGestureRecognizer*)gesture{
NSLog(@"点击了 imageView");
}
//长按
-(void)longPress{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
longPress.minimumPressDuration = 3;
[self.imageView addGestureRecognizer:longPress];
}
-(void)longPress:(UILongPressGestureRecognizer*)gestrue{
if(gestrue.state == UIGestureRecognizerStateBegan){
NSLog(@"开始长按");
}
else if(gestrue.state == UIGestureRecognizerStateChanged)
{
NSLog(@"长按变化触发了");
}else if(gestrue.state == UIGestureRecognizerStateEnded){
NSLog(@"结束长按");
}
}
-(void)pan{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[self.imageView addGestureRecognizer:pan];
}
-(void)pan:(UIPanGestureRecognizer*)gesture{
CGPoint position = [gesture locationInView:self.view];
self.imageView.center = position;
}
-(void)pinch{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
[self.imageView addGestureRecognizer:pinch];
}
-(void)pinch:(UIPinchGestureRecognizer*)gesture{
NSLog(@"%f",gesture.scale);
}
//旋转手势
-(void)rotation{
UIRotationGestureRecognizer *rotation= [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
[self.imageView addGestureRecognizer:rotation];
}
-(void)rotation:(UIRotationGestureRecognizer*)gesture{
NSLog(@"%f",gesture.rotation);
}
- (void)viewDidLoad {
[super viewDidLoad];
//[self tap];
// [self swipe];
//[self longPress];这不晓得问啥在模拟器实现不了
// [self pan];
// [self pinch];
[self rotation];
}
网友评论