iOS学习笔记-068.手势识别02——捏合、拖拽、旋转手势

手势识别02——捏合、拖拽、旋转手势

一、捏合手势

捏合手势使用的是 UIPinchGestureRecognizer

1.1 UIPinchGestureRecognizer.h

#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIGestureRecognizer.h>

NS_ASSUME_NONNULL_BEGIN

NS_CLASS_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED @interface UIPinchGestureRecognizer : UIGestureRecognizer

@property (nonatomic)          CGFloat scale;   //缩放的比例
@property (nonatomic,readonly) CGFloat velocity;            // velocity of the pinch in scale/second

@end

NS_ASSUME_NONNULL_END

1.2 代码示例

/====================捏合手势=======================

-(void)createPinGesture{
    //创建手势
    UIPinchGestureRecognizer * pinGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinGesture:)];
    //添加手势
    [_imageView addGestureRecognizer:pinGesture];
}

-(void)pinGesture:(UIPinchGestureRecognizer*)pinGesture{
    //缩放
    _imageView.transform = CGAffineTransformScale(_imageView.transform, pinGesture.scale, pinGesture.scale);
    //复位
    pinGesture.scale = 1;
}

//===========================================

1.3 图示

这里写图片描述

二、拖拽手势

捏合手势使用的是 UIPanGestureRecognizer

2.1 UIPanGestureRecognizer.h

//
//  UIPanGestureRecognizer.h
//  UIKit
//
//  Copyright (c) 2008-2016 Apple Inc. All rights reserved.
//

#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIGestureRecognizer.h>

NS_ASSUME_NONNULL_BEGIN

NS_CLASS_AVAILABLE_IOS(3_2) @interface UIPanGestureRecognizer : UIGestureRecognizer 

@property (nonatomic)          NSUInteger minimumNumberOfTouches __TVOS_PROHIBITED;   
@property (nonatomic)          NSUInteger maximumNumberOfTouches __TVOS_PROHIBITED;   

- (CGPoint)translationInView:(nullable UIView *)view;        //获取移动的偏移量               
- (void)setTranslation:(CGPoint)translation inView:(nullable UIView *)view;

- (CGPoint)velocityInView:(nullable UIView *)view;                           

@end

NS_ASSUME_NONNULL_END

2.2 代码示例

/====================拖拽手势=======================

-(void)createPanGesture{
    //创建手势
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];
    //添加手势
    [_imageView addGestureRecognizer:pan];
}

-(void)panGesture:(UIPanGestureRecognizer*)pan{
    //获取移动的偏移量
    CGPoint point = [pan translationInView:_imageView];
    _imageView.transform = CGAffineTransformTranslate(_imageView.transform, point.x, point.y);
//    //复位
    [pan setTranslation:CGPointZero inView:_imageView];
}

//===============================================

2.3 图示

这里写图片描述

三、旋转

3.1

#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIGestureRecognizer.h>

NS_ASSUME_NONNULL_BEGIN

NS_CLASS_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED @interface UIRotationGestureRecognizer : UIGestureRecognizer

@property (nonatomic)          CGFloat rotation;     //偏移弧度
@property (nonatomic,readonly) CGFloat velocity;    

@end

NS_ASSUME_NONNULL_END

3.2 代码示例

//=======================旋转手势========================
-(void)createRotationGesture{
    UIRotationGestureRecognizer * rota = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)];
    [_imageView addGestureRecognizer:rota];
}

-(void)rotationGesture:(UIRotationGestureRecognizer*)rotation{
    _imageView.transform = CGAffineTransformRotate(_imageView.transform, rotation.rotation);//弧度
    rotation.rotation = 0;
}

3.3 图示

这里写图片描述

四、缩放、拖拽、旋转三个手势同时使用

4.1 是否支持多个手势的方法

// 是否同时支持多个手势 
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

4.2 示例代码

//
//  ViewController.m
//  03_UIView61_手势识别2
//
//  Created by 杞文明 on 17/3/31.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _imageView.userInteractionEnabled = YES;
    [self createPinGesture];
    [self createPanGesture];
    [self createRotationGesture];
}

// 是否同时支持多个手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

//====================捏合手势=======================

-(void)createPinGesture{
    //创建手势
    UIPinchGestureRecognizer * pinGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinGesture:)];
    pinGesture.delegate = self;
    //添加手势
    [_imageView addGestureRecognizer:pinGesture];
}

-(void)pinGesture:(UIPinchGestureRecognizer*)pinGesture{
    //缩放
    _imageView.transform = CGAffineTransformScale(_imageView.transform, pinGesture.scale, pinGesture.scale);
    //复位
    pinGesture.scale = 1;
}

//===========================================

//====================拖拽手势=======================

-(void)createPanGesture{
    //创建手势
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];
    pan.delegate = self;
    //添加手势
    [_imageView addGestureRecognizer:pan];
}

-(void)panGesture:(UIPanGestureRecognizer*)pan{
    //获取移动的偏移量
    CGPoint point = [pan translationInView:_imageView];
    _imageView.transform = CGAffineTransformTranslate(_imageView.transform, point.x, point.y);
//    //复位
    [pan setTranslation:CGPointZero inView:_imageView];
}

//===============================================

//=======================旋转手势========================
-(void)createRotationGesture{
    UIRotationGestureRecognizer * rota = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)];
    rota.delegate = self;
    [_imageView addGestureRecognizer:rota];
}

-(void)rotationGesture:(UIRotationGestureRecognizer*)rotation{
    _imageView.transform = CGAffineTransformRotate(_imageView.transform, rotation.rotation);//弧度
    rotation.rotation = 0;
}

//===============================================


@end

4.3 图示

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值