iOS中GestureRecognizer的6大手势与代理方法详细使用

本文深入讲解了iOS中各种手势识别器的使用方法,包括点按、长按、清扫、旋转、捏合和拖拽等常见手势。通过实例代码展示了如何在UIImageView上添加并响应这些手势,适用于希望增强应用交互性的iOS开发者。
部署运行你感兴趣的模型镜像

#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.\

[self setUpPinch];

[self setUpRotation];

[self setUpPan];

}

#pragma mark - 手势代理方法

// 是否允许开始触发手势

//- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

//{

//    return NO;

//}

// 是否允许同时支持多个手势,默认是不支持多个手势

// 返回yes表示支持多个手势

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

return YES;

}

// 是否允许接收手指的触摸点

//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{

//    // 获取当前的触摸点

//    CGPoint curP = [touch locationInView:self.imageView];

//

//    if (curP.x < self.imageView.bounds.size.width * 0.5) {

//        return NO;

//    }else{

//        return YES;

//    }

//}

1.#pragma mark - 点按手势

- (void)setUpTap

{

// 创建点按手势

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

tap.delegate = self;

[_imageView addGestureRecognizer:tap];

}

- (void)tap:(UITapGestureRecognizer *)tap

{

NSLog(@"%s",__func__);

}

2.#pragma mark - 长按手势

// 默认会触发两次

- (void)setUpLongPress

{

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

[self.imageView addGestureRecognizer:longPress];

}

- (void)longPress:(UILongPressGestureRecognizer *)longPress

{

if (longPress.state == UIGestureRecognizerStateBegan) {

NSLog(@"%s",__func__);

}

}

3.#pragma mark - 清扫

- (void)setUpSwipe

{

// 默认轻扫的方向是往右

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

swipe.direction = UISwipeGestureRecognizerDirectionUp;

[self.imageView addGestureRecognizer:swipe];

// 如果以后想要一个控件支持多个方向的轻扫,必须创建多个轻扫手势,一个轻扫手势只支持一个方向

// 默认轻扫的方向是往右

UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];

swipeDown.direction = UISwipeGestureRecognizerDirectionDown;

[self.imageView addGestureRecognizer:swipeDown];

}

- (void)swipe

{

NSLog(@"%s",__func__);

}

4.#pragma mark - 旋转手势

- (void)setUpRotation

{

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

rotation.delegate = self;

[self.imageView addGestureRecognizer:rotation];

}

// 默认传递的旋转的角度都是相对于最开始的位置

- (void)rotation:(UIRotationGestureRecognizer *)rotation

{

self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);

// 复位

rotation.rotation = 0;

// 获取手势旋转的角度

NSLog(@"%f",rotation.rotation);

}

5.#pragma mark - 捏合

- (void)setUpPinch

{

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

pinch.delegate = self;

[self.imageView addGestureRecognizer:pinch];

}

- (void)pinch:(UIPinchGestureRecognizer *)pinch

{

self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);

// 复位

pinch.scale = 1;

}

6.#pragma mark - 拖拽

- (void)setUpPan

{

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

[self.imageView addGestureRecognizer:pan];

}

- (void)pan:(UIPanGestureRecognizer *)pan

{

// 获取手势的触摸点

// CGPoint curP = [pan locationInView:self.imageView];

// 移动视图

// 获取手势的移动,也是相对于最开始的位置

CGPoint transP = [pan translationInView:self.imageView];

self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);

// 复位

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

//  NSLog(@"%@",NSStringFromCGPoint(curP));

}

转载于:https://www.cnblogs.com/CJH5209/p/6027251.html

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值