iOS 手势合集
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor purpleColor];
_imageView = [[UIImageView alloc]init];
_imageView.frame = CGRectMake(80, 120, 160, 240);
_imageView.image = [UIImage imageNamed:@"L80.jpg"];
_imageView.userInteractionEnabled = YES;//是图片可以点击
_imageView.multipleTouchEnabled = YES;//让ImageView也支持多点触控
[self.view addSubview:_imageView];
//单击
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
tap1.numberOfTapsRequired = 1;//需要轻击的次数 默认为1
tap1.numberOfTouchesRequired = 1;//响应这个时间需要的手指个数默认为1
[_imageView addGestureRecognizer:tap1];
//双击
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTap:)];
tap2.numberOfTapsRequired = 2;//需要轻击的次数 默认为1
[_imageView addGestureRecognizer:tap2];
[tap1 requireGestureRecognizerToFail:tap2];//tap1这个手势需要在tap2这个手势失败后再执行
//捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
pinch.delegate = self;
[_imageView addGestureRecognizer:pinch];
//旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[_imageView addGestureRecognizer:rotation];
//移动
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[_imageView addGestureRecognizer:pan];
//清扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionUp;
[_imageView addGestureRecognizer:swipe];
[pan requireGestureRecognizerToFail:swipe];
//长按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
longPress.minimumPressDuration = 2;
longPress.allowableMovement = 20;
[_imageView addGestureRecognizer:longPress];
}
#pragma mark - 长按
- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
if(longPress.state ==UIGestureRecognizerStateBegan)
{
NSLog(@"111111");
}
else if(longPress.state == UIGestureRecognizerStateEnded)
{
NSLog(@"222");
}
}
#pragma mark - 清扫
- (void)swipe:(UISwipeGestureRecognizer *)swipe
{
_imageView.alpha = _imageView.alpha - 0.2;
}
#pragma mark - 移动
- (void)pan:(UIPanGestureRecognizer *)pan
{
CGPoint point = [pan locationInView:self.view];
_imageView.center = CGPointMake(_imageView.center.x+point.x, _imageView.center.y+point.y);
[pan setTranslation:CGPointZero inView:self.view];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
#pragma mark - 旋转手势
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
_imageView.transform = CGAffineTransformRotate(_imageView.transform, rotation.rotation);
rotation.rotation = 0;
}
#pragma mark - 捏合手势
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
_imageView.transform = CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);
//_imageView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
pinch.scale = 1;
}
#pragma mark - 单击
- (void)singleTap:(UITapGestureRecognizer *)tap
{
[UIView animateWithDuration:0.3 animations:^{
CGRect bounds = _imageView.bounds;
bounds.size.width *=1.2;
bounds.size.height *=1.2;
_imageView.bounds = bounds;
}];
}
#pragma mark - 双击
- (void)doubleTap:(UITapGestureRecognizer *)tap
{
[UIView animateWithDuration:0.3 animations:^{
CGRect bounds = _imageView.bounds;
bounds.size.width /=1.2;
bounds.size.height /=1.2;
_imageView.bounds = bounds;
}];
}
————————————————
原文链接:https://blog.csdn.net/weixin_39963603/article/details/103847734