手势识别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