UIGestureRecognizer手势识别

8 篇文章 0 订阅

UIGestureRecognizer手势识别

UIGestureRecognizer 继承于 NSObject 共有七种手势

即:
1.UIPinchGestureRecognizer (捏合手势)

2.UITapGestureRecognizer (轻拍手势)

3.UIRotationGestureRecognizer (旋转手势)

4.UISwipeGestureRecognizer (轻扫手势)

5.UIPanGestureRecognizer (拖拽手势)

6.UIScreenEdgePanGestureRecognizer (屏幕边缘轻扫手势)

7.UILongPressGestureRecognizer (长按手势)

实现手势步骤

1创建手势对象

UIGestureRecognizer 的 七个子类并没有自身的初始化方法, 所以需要使用父类的 初始化方法,如下

- (instancetype)initWithTarget:(id)target action:(SEL)action

2将手势加在视图上, 注意视图的用户交互属性是否开启

- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer/* 创建对象 */

@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled/* 用户交互属性*/

3内存管理

- (void)release

4设置相关属性

5手势方法实现

实现创建对象 - (instancetype)initWithTarget:(id)target action:(SEL)action

中的 action:(SEL)action  

例如

1.UIPinchGestureRecognizer (捏合手势)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
/* 创建一个UIImageView对象 */
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
    imageView.frame = CGRectMake(0, 0, 300, 300);
    imageView.center = self.view.center;
    imageView.layer.cornerRadius = 50;
    imageView.layer.backgroundColor = [UIColor blackColor].CGColor;
    imageView.layer.masksToBounds = YES;
    [self.view addSubview:imageView];
    [imageView release];

/* 创建UIPinchGestureRecognizer对象*/    
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];

/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */
    [imageView addGestureRecognizer:pinch];
    imageView.userInteractionEnabled = YES;/* 默认NO */

    [pinch release];
}

/* 实现action:@selector(pinchAction:)*/
 - (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
    NSLog(@"捏合");

    /* 设置imageView的transform属性
     *使用CGAffineTransformScale函数改变比例实现缩放效果
     */
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    pinch.scale = 1;
}

2.UITapGestureRecognizer (轻拍手势)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
/* 创建一个UIImageView对象 */
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
    imageView.frame = CGRectMake(0, 0, 300, 300);
    imageView.center = self.view.center;
    imageView.layer.cornerRadius = 50;
    imageView.layer.backgroundColor = [UIColor blackColor].CGColor;
    imageView.layer.masksToBounds = YES;
    [self.view addSubview:imageView];
    [imageView release];

/* 创建UITapGestureRecognizer手势对象 */
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

/* 设置相关属性 */
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;

/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */
    [imageView addGestureRecognizer:tap];
    imageView.userInteractionEnabled = YES;/* 默认NO */

    [tap release];
}

/* 实现action:@selector(pinchAction:)*/
 - (void)tapAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"轻拍");
}

3.UIRotationGestureRecognizer (旋转手势)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
/* 创建一个UIImageView对象 */
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
    imageView.frame = CGRectMake(0, 0, 300, 300);
    imageView.center = self.view.center;
    imageView.layer.cornerRadius = 50;
    imageView.layer.backgroundColor = [UIColor blackColor].CGColor;
    imageView.layer.masksToBounds = YES;
    [self.view addSubview:imageView];
    [imageView release];

/* 创建UIRotationGestureRecognizer手势对象 */
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];

/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */
    [imageView addGestureRecognizer:rotation];
    imageView.userInteractionEnabled = YES;/* 默认NO */

    [rotation release];
}

/* 实现action:@selector(pinchAction:)*/
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
    NSLog(@"旋转");
    NSLog(@"%@", rotation);
    /* 设置imageView的transform属性
     * 使用 CGAffineTransformRotate 函数
     */
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    rotation.rotation = 0;
}

4.UISwipeGestureRecognizer (轻扫手势)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

/* 创建UISwipeGestureRecognizer手势对象 */
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];

/* 设置相关属性 */
    swipe.direction = UISwipeGestureRecognizerDirectionUp;

/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */
    [self.view addGestureRecognizer:swipe];

    [swipe release];
}

/* 实现action:@selector(pinchAction:)*/
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
    NSLog(@"轻扫");
}

5.UIPanGestureRecognizer (拖拽手势)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
/* 创建一个UIImageView对象 */
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
    imageView.frame = CGRectMake(0, 0, 300, 300);
    imageView.center = self.view.center;
    imageView.layer.cornerRadius = 50;
    imageView.layer.backgroundColor = [UIColor blackColor].CGColor;
    imageView.layer.masksToBounds = YES;
    [self.view addSubview:imageView];
    [imageView release];

/* 创建UIPanGestureRecognizer手势对象 */
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];

/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */
    [imageView addGestureRecognizer:pan];
    imageView.userInteractionEnabled = YES;/* 默认NO */

    [pan release];
}

/* 实现action:@selector(pinchAction:)*/
- (void)panAction:(UIPanGestureRecognizer *)pan
{
    NSLog(@"拖拽");

    /* 设置imageView的transform属性
     * 使用 CGAffineTransformTranslate 函数 
     */
    CGPoint point = [pan translationInView:pan.view];

    pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);

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

}

6.UIScreenEdgePanGestureRecognizer (屏幕边缘轻扫手势)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

/* 创建UIScreenEdgePanGestureRecognizer手势对象 */
    UIScreenEdgePanGestureRecognizer *screenEdge = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenAction:)];

/* 设置相关属性 */
    screenEdge.edges = 2;

/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */
    [self.view addGestureRecognizer:screenEdge];

    [screenEdge release];
}

/* 实现action:@selector(pinchAction:)*/
-(void)screenAction:(UIScreenEdgePanGestureRecognizer *)screen
{
    NSLog(@"屏幕边缘轻扫");
}

7.UILongPressGestureRecognizer (长按手势)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
/* 创建一个UIImageView对象 */
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
    imageView.frame = CGRectMake(0, 0, 300, 300);
    imageView.center = self.view.center;
    imageView.layer.cornerRadius = 50;
    imageView.layer.backgroundColor = [UIColor blackColor].CGColor;
    imageView.layer.masksToBounds = YES;
    [self.view addSubview:imageView];
    [imageView release];

/* 创建UILongPressGestureRecognizer手势对象 */
    UILongPressGestureRecognizer *long1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];

/* 将手势加在视图上, 注意视图的用户交互属性是否开启 */
    [imageView addGestureRecognizer:long1];
    imageView.userInteractionEnabled = YES;/* 默认NO */

    [long1 release];
}

/* 实现action:@selector(pinchAction:)*/
- (void)longAction:(UILongPressGestureRecognizer *)long1
{
    NSLog(@"长按");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值