Gesture | UIKit class |
---|---|
Tapping (any number of taps) | |
Pinching in and out (for zooming a view) | |
Panning or dragging | |
Swiping (in any direction) | |
Rotating (fingers moving in opposite directions) | |
Long press (also known as “touch and hold”) |
@interface ViewController () <UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.iconView.userInteractionEnabled = YES;
}
//一个view支持多个手势需要实习这个方法返回yes;旋转和缩放需要设置delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
UITapGestureRecognizer 点击操作:
//默认点击一次,设置点击几次
@property (nonatomic) NSUInteger numberOfTapsRequired; // Default is 1. The number of taps required to match
//默认1个,设置几个手指点击。
@property (nonatomic) NSUInteger numberOfTouchesRequired; // Default is 1. The number of fingers required to match
- (void)testTap{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappingView:)];
[self.iconView addGestureRecognizer:tap];
}
- (void)tappingView:(UITapGestureRecognizer *)gesture {
NSLog(@"tappingView---%@", NSStringFromCGPoint([gesture locationInView:gesture.view]));
}
UIPinchGestureRecognizer 缩放
//缩放的比例
@property (nonatomic) CGFloat scale; // scale relative to the touch points in screen coordinates
//缩放的速度
@property (nonatomic,readonly) CGFloat velocity; // velocity of the pinch in scale/second
- (void)testPinch{
UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
pin.delegate = self;
[self.iconView addGestureRecognizer:pin];
}
- (void)pinchView:(UIPinchGestureRecognizer *)pinch{
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);// pinch.view 指操作的view
pinch.scale = 1; //必须要重新赋值。
}
- (void)testPan{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(testPanView:)];
[self.iconView addGestureRecognizer:pan];
}
- (void)testPanView:(UIPanGestureRecognizer *)pan{
CGPoint poc = [pan translationInView:pan.view];
CGPoint temp = pan.view.center;
temp.x += poc.x;
temp.y += poc.y;
pan.view.center = temp;
[pan setTranslation:CGPointZero inView:pan.view];//清空Translation
NSLog(@"%@",NSStringFromCGPoint(poc));
}
UISwipeGestureRecognizer 清扫/滑动
- (void)testSwipe{
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(testSwipeView:)];
swipe.direction = UISwipeGestureRecognizerDirectionDown;//设置滑动的方向,只能设置一个方向
swipe.delegate = self;
[self.iconView addGestureRecognizer:swipe];
}
- (void)testSwipeView:(UISwipeGestureRecognizer *)swipe{
NSLog(@"testSwipeView");
}
UIRotationGestureRecognizer 旋转
- (void)testRotate{
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(testRotateView:)];
rotate.delegate = self;
[self.iconView addGestureRecognizer:rotate];
}
- (void)testRotateView:(UIRotationGestureRecognizer *)rotate{
rotate.view.transform = CGAffineTransformRotate(rotate.view.transform, rotate.rotation);
rotate.rotation = 0;
}