接上篇:transform
手势配合使用transform
手势可以添加在任何想要的地方,一般添加在imageView上。
GestureRecognizer 手势识别器
使用手势,首先要确定控件的手势交互式打开的:
//打开交互
self.rv.imv.userInteractionEnabled = YES;
1、轻拍
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
设置属性
tap.numberOfTapsRequired = 1; // 次数
tap.numberOfTouchesRequired = 2; // 触控数
//轻拍
- (void)tapAction:(UIGestureRecognizer *)sender
{
NSLog(@"tap");
}
//将手势添加在视图上
[self.rv.imv addGestureRecognizer:tap];
2、长按
UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.rv.imv addGestureRecognizer:longPress];
//长按
- (void)longPress:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan) {
// 开始
sender.view.bounds = CGRectMake(0, 0, 300, 300);
} else if (sender.state == UIGestureRecognizerStateEnded) {
// 结束
sender.view.bounds = CGRectMake(0, 0, 250, 250);
}
}
3、轻扫
UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
属性 轻扫方向 只能相对的方向,左右或上下
swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
//轻扫
- (void)swipeAction:(UISwipeGestureRecognizer *)sender
{
NSLog(@"swipe");
}
//将手势添加在视图上
[self.rv.imv addGestureRecognizer:swipe];
4、拖动
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
//拖动
- (void)panAction:(UIPanGestureRecognizer *)sender
{
// 获取坐标
CGPoint point = [sender translationInView:sender.view];
sender.view.transform = CGAffineTransformTranslate(sender.view.transform, point.x, point.y);
// 清零
[sender setTranslation:CGPointZero inView:sender.view];
}
//将手势添加在视图上
//将手势添加在视图上
[self.rv.imv addGestureRecognizer:pan];
5、旋转
UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
//旋转
- (void)rotationAction:(UIRotationGestureRecognizer *)sender
{
sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation);
sender.rotation = 0;
}
//将手势添加在视图上
[self.rv.imv addGestureRecognizer:rotation];
6、捏合
UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
//捏合
- (void)pinchAction:(UIPinchGestureRecognizer *)sender
{
sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
// 置1
sender.scale = 1;
}
//将手势添加在视图上
[self.rv.imv addGestureRecognizer:pinch];