标签:手势触摸
//创建UITapGestureRecognizer对象,并且初始化的时候就绑定事件
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)];
//连击几次才触发事件
recognizer.numberOfTapsRequired = 2;
//在对应的View上添加这个手势监听
[self.myView addGestureRecognizer:recognizer];
//创建UILongPressGestureRecognizer对象,并初始化的时候就绑定事件
UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)];
recognizer.minimumPressDuration = 2;//2秒后触发事件
[self.myView addGestureRecognizer:recognizer];
//创建UISwipeGestureRecognizer对象
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)];
//手势扫的方向
recognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.myView addGestureRecognizer:recognizer];
- (void)Rotate
{
//创建对象
UIRotationGestureRecognizer *recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];
recognizer.delegate = self;//每一个View要想支持多个手势,就要实现这个代理
[self.iconView addGestureRecognizer:recognizer];
}
//执行这个方法时候,会默认把旋转对象传过去
- (void)rotateView:(UIRotationGestureRecognizer *)recognizer
{
//recognizer.rotation等于旋转角度,加上前一个他的前一个角度,所以每次把前一个角度清0,这个值就等于旋转角度
//每次要把当前的transform+旋转的角度
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
//要把旋转的角度设为0
recognizer.rotation = 0;
}
- (void)Pinch
{
//创建对象
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
pinch.delegate = self;
[self.iconView addGestureRecognizer:pinch];
}
- (void)pinchView:(UIPinchGestureRecognizer *)pinch
{
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
pinch.scale = 1;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
[self.purpleView addGestureRecognizer:pan];
}
- (void)panView:(UIPanGestureRecognizer *)pan
{
switch (pan.state) {
case UIGestureRecognizerStateBegan: // 开始触发手势
break;
case UIGestureRecognizerStateEnded: // 手势结束
break;
default:
break;
}
// 1.在view上面挪动的距离
CGPoint translation = [pan translationInView:pan.view];
CGPoint center = pan.view.center;
center.x += translation.x;
center.y += translation.y;
pan.view.center = center;
// 2.清空移动的距离
[pan setTranslation:CGPointZero inView:pan.view];
}
目标View要想支持多个手势的必须实现UIGestureRecognizerDelegate代理
然后实现下面的方法
/**
* 是否允许多个手势识别器同时有效
* Simultaneously : 同时地
*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
标签:手势触摸
原文地址:http://blog.csdn.net/ttf1993/article/details/45847323