iOS 常见的六种 手势

宋俊艾
2023-12-01

六种手势如下:

UITapGestureRecognizer

[UIPinchGestureRecognizer](捏合)

[UIRotationGestureRecognizer](旋转)

([UISwipeGestureRecognizer]滑动,快速移动)

[UIPanGestureRecognizer](平移,慢速移动)

[UILongPressGestureRecognizer](长按)

以上六种只是常用的一些手势,在这里作此简述

创建图片对象

// 创建图片对象

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];

// 设置图片内容

imageView.image = [UIImage imageNamed:@“自己图片名”];

// 设置图片居中

imageView.center = self.view.center;

// 设置用户交互

imageView.userInteractionEnabled = YES;

// 添加到视图上

[self.view addSubview:imageView];
UITapGestureRecognizer (点击手势)

//1.创建手势对象

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];

//2.手势相关的属性

//点击次数(默认1)

tapGesture.numberOfTapsRequired = 1;

//手指的个数(默认1)

tapGesture.numberOfTouchesRequired = 1;

//3.把手势与视图相关联

[imageView addGestureRecognizer:tapGesture];
// 点击事件

-(void)tapClick:(UITapGestureRecognizer *)tap{

NSLog(@“点击图片了”);

}
UIPinchGestureRecognizer (捏合手势)

//捏合手势

UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pichClick:)];

[imageView addGestureRecognizer:pinchGesture];

// 捏合事件

-(void)pichClick:(UIPinchGestureRecognizer *)pinch{

//缩放的系数

NSLog(@"%.2lf",pinch.scale);

//固定写法

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

//重置缩放系数(否则系数会累加)

pinch.scale = 1.0;

}
UIRotationGestureRecognizer (旋转手势)

//旋转手势

UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationClick:)];

[imageView addGestureRecognizer:rotationGesture];

-(void)rotationClick:(UIRotationGestureRecognizer *)rotation{

NSLog(@“qqq”);

}
UISwipeGestureRecognizer (滑动手势)

// 滑动手势

UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeClick:)];

// 设置轻扫的方向

leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;

[self.view addGestureRecognizer:leftSwipe];

UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeClick:)];

// 右扫

rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;

[self.view addGestureRecognizer:rightSwipe];

// 滑动事件

-(void)swipeClick:(UISwipeGestureRecognizer *)swpie{

// 如果是左扫

if (swpie.direction == UISwipeGestureRecognizerDirectionLeft ) {

NSLog(@“左扫!”);

}else{

NSLog(@“右扫!”);

}
}

UIPanGestureRecognizer (平移手势)

//平移手势

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panClick:)];

[imageView addGestureRecognizer:panGesture];

// 平移事件

-(void)panClick:(UIPanGestureRecognizer *)pan{

NSLog(@“响应了。。。”);

//通过pan手势,能够获取到pan.view在self.view上的偏移量

CGPoint point = [pan translationInView:self.view];

NSLog(@“x=%.2lf y=%.2lf”,point.x,point.y);

//改变中心点坐标(原来的中心点+偏移量=当前的中心点)

CGPoint newCenter = CGPointMake(pan.view.center.x + point.x, pan.view.center.y + point.y);

// CGPointZero<==>CGPointMake(0,0)

//限制拖动范围

newCenter.y = MAX(pan.view.frame.size.height/2, newCenter.y);

newCenter.y = MIN(self.view.frame.size.height - pan.view.frame.size.height/2, newCenter.y);

newCenter.x = MAX(pan.view.frame.size.width/2, newCenter.x);

newCenter.x = MIN(self.view.frame.size.width - pan.view.frame.size.width/2, newCenter.x);

pan.view.center = newCenter;

//每次调用之后,需要重置手势的偏移量,否则偏移量会自动累加

[pan setTranslation:CGPointZero inView:self.view];

}
UILongPressGestureRecognizer (长按手势)

//长按手势

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)];

//用几个手指触屏,默认1

longPressGesture.numberOfTouchesRequired = 1;

//设置最短长按时间,单位为秒(默认0.5)

longPressGesture.minimumPressDuration = 1;

//设置手势识别期间所允许的手势可移动范围

longPressGesture.allowableMovement = 10;

[imageView addGestureRecognizer:longPressGesture];

// 长按事件

-(void)longPressClick:(UILongPressGestureRecognizer *)press{

//state属性是所有手势父类提供的方法,用于记录手势的状态

if(press.state == UIGestureRecognizerStateBegan){

NSLog(@“长按手势开始响应!”);

}else if (press.state == UIGestureRecognizerStateChanged){

NSLog(@“长按手势状态发生改变!”);

}else{

NSLog(@“长按手势结束!”);

}
}

 类似资料: