利用UIGestureRecognizer來對手勢進行處理:
@interface HMViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imagView;
@end
@implementation HMViewController
(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.// pan
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];[_imagView addGestureRecognizer:pan];
}(void)pan:(UIPanGestureRecognizer *)pan
{
// 獲取手指移動的位置
CGPoint trans = [pan translationInView:_imagView];_imagView.transform = CGAffineTransformTranslate(_imagView.transform, trans.x, trans.y);
// 復位
[pan setTranslation:CGPointZero inView:_imagView];
NSLog(@”%@”,NSStringFromCGPoint(trans));
}
warning pinch
- (void)addPinch
{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
// 設置代理的原因:想要同時支持多個手勢
pinch.delegate = self;
[_imagView addGestureRecognizer:pinch];
}
(void)pinch:(UIPinchGestureRecognizer *)pinch
{
_imagView.transform = CGAffineTransformScale(_imagView.transform, pinch.scale, pinch.scale);// 復位
pinch.scale = 1;
}
// Simultaneous:同時
// 默認是不支持多個手勢
// 當你使用一個手勢的時候就會調用
- (BOOL)gestureRecognizer:(UIGestureRecognizer )gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer )otherGestureRecognizer
{
return YES;
}
warning rotation
(void)addRotation
{
// rotation
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[_imagView addGestureRecognizer:rotation];
}(void)rotation:(UIRotationGestureRecognizer *)rotation
{
NSLog(@”%f”,rotation.rotation);
// _imagView.transform = CGAffineTransformMakeRotation(rotation.rotation);
_imagView.transform = CGAffineTransformRotate(_imagView.transform, rotation.rotation);// 復位
rotation.rotation = 0;
}
warning Swipe
(void)addSwipe
{
// Swipe
// 一個手勢只能識別一個方向
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[_imagView addGestureRecognizer:swipe];
}(void)swipe:(UISwipeGestureRecognizer *)swipe
{
NSLog(@”swipe”);
}
warning longPress
(void)addLongPress
{
// longPress
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];[_imagView addGestureRecognizer:longPress];
}(void)longPress:(UILongPressGestureRecognizer *)longPress
{
// 根據狀態執行事件
if (longPress.state == UIGestureRecognizerStateBegan) {NSLog(@"longPress");
}
}
warning tap
(void)addTap
{
// tap
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
// 點按多少次才能觸發手勢
// tap.numberOfTapsRequired = 2;
//
// // 必須多少個手指觸摸才能觸發手勢
// tap.numberOfTouchesRequired = 2;tap.delegate = self;
[_imagView addGestureRecognizer:tap];
}
// 這個觸摸點能否觸發手勢
//- (BOOL)gestureRecognizer:(UIGestureRecognizer )gestureRecognizer shouldReceiveTouch:(UITouch )touch
//{
// CGPoint currentPoint = [touch locationInView:_imagView];
//
// if (currentPoint.x < _imagView.bounds.size.width * 0.5) {
// return NO;
// }else{
//
// return YES;
// }
//}
- (void)tap:(UITapGestureRecognizer *)tap
{
NSLog(@”tap”);
}