UITapGestureRecognizer(敲击)
UipinchGestureRecognizer(缩放)
UIRotationGestureRecognizer(旋转)
UISwipeGestureRecognizer(轻扫)
UIPanGestureRecognizer(拖拽)
UIScreenEdgePanGestureRecognizer
UILongPRessGestureRecognizer(长按)
#import "ViewController.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIView *iconView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
#warning 要实现手势操作userInteractionEnabled必须设为YES;
// 1.旋转手势
// [self testRotate];
// 2.捏合手势
// [self testPinch];
// 3.同时进行捏合和旋转
// 同时进行捏合和旋转需要实现代理方法
// [self testPinchAndRotate];
// 4.测试拖拽
[self testPan];
}
#pragma mark - 拖拽手势
- (void)testPan
{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
[_iconView addGestureRecognizer:pan];
}
/**
* 拖拽手势
*/
- (void)panView:(UIPanGestureRecognizer *)pan
{
UIView *panView = pan.view; // 被拖拽的view
//1.view上移动的位置
CGPoint translation = [pan translationInView:panView]; // 在view上拖拽的位置
CGPoint center = panView.center;
center.x += translation.x;
center.y += translation.y;
panView.center = center;
// 2.清空移动的位置
[pan setTranslation:CGPointZero inView:panView];
}
#pragma mark - 代理方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
#pragma mark - 旋转和缩放
- (void)testPinchAndRotate
{
[self testPinch];
[self testRotate];
}
#pragma mark - 缩放手势
- (void)testPinch
{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
#warning 如果只是实现单个缩放手势可以不用实现代理方法
pinch.delegate = self;
[_iconView addGestureRecognizer:pinch];
}
/**
* 捏合
*/
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
pinch.scale = 1; // 可以认为每次捏合后缩放的比例都重新重置为1
}
#pragma mark - 旋转手势
- (void)testRotate
{
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[_iconView addGestureRecognizer:rotate];
}
/**
* 旋转
*/
- (void)rotate:(UIRotationGestureRecognizer *)rotate
{
rotate.view.transform = CGAffineTransformRotate(rotate.view.transform, rotate.rotation);
rotate.rotation = 0; // 每次旋转后旋转的角度重置为0
}
@end