1.单击
-(void)createTapGesture{
//点击
//UITapGestureRecognizer:继承自手势基类UIGestureRecognizer
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//设置点击的手指个数:默认是1
tap.numberOfTouchesRequired = 1;
//设置点击次数:默认是1
//单击、双击
tap.numberOfTapsRequired = 2;
//当前视图默认只支持单点触摸,不支持多点触摸,所以要开启视图的多点触摸模式
self.view.multipleTouchEnabled=YES;
//设置点击事件的优先级
//双击事件的优先级大于单击事件的优先级
[singelTap requireGestureRecognizerToFail:doubleTap];
[_iv addGestureRecognizer:tap];
[tap release];
}
-(void)tapAction:(UITapGestureRecognizer *)tap{
//单击:修改背景颜色
self.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}
2.移动
-(void)createPanGesture{
//移动
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
//把手势添加在_iv上
[_iv addGestureRecognizer:pan];
[pan release];
}
-(void)panAction:(UIPanGestureRecognizer *)pan{
//获取pan手势在视图中的位置 --> point
CGPoint pt = [pan locationInView:self.view];
//修改_iv的center
_iv.center = pt;
//获取 拖动手势的偏移量
//手势偏移量 相对的视图 要和 _imageView相对的坐标视图保持一致(相对的都是同一个视图)
CGPoint offSet = [pan translationInView:self.view];//相对于self.view的偏移量
//修改中心点位置
CGPoint point = _imageView.center;
_imageView.center = CGPointMake(point.x+offSet.x, point.y+offSet.y);
//把手势的偏移量 每次重置(0,0)CGPointZero
[pan setTranslation:CGPointZero inView:self.view];
}
3.旋转
-(void)createRotationGesture{
//旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotaionAction:)];
rotation.delegate = self;
[_iv addGestureRecognizer:rotation];
[rotation release];
}
-(void)rotaionAction:(UIRotationGestureRecognizer *)rotate{
//视图的变换
//第一个参数:CGAffineTransform --> 跟随要进行变换的视图
//第二个参数:角度 --> 跟随手指的旋转角度 --> rotate.rotation
[_iv setTransform:CGAffineTransformRotate(_iv.transform, rotate.rotation)];
//旋转角度置为0
rotate.rotation = 0;
//<2>不需要将rotate.rotation置0
[_iv setTransform:CGAffineTransformMakeRotation(rotate.rotation)];
}
4.缩放/捏合
-(void)createPinchGesture{
//缩放
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
//想让旋转和缩放同时触发,需要分别在旋转和缩放的手势创建过程中设置代理
pinch.delegate = self;
//把缩放手势添加到要进行缩放的视图上
[_iv addGestureRecognizer:pinch];
[pinch release];
}
-(void)pinchAction:(UIPinchGestureRecognizer *)pinch{
//视图变换
//第一个参数:CGAffineTransform类型 --> 跟随要进行变换的视图
//第二/三个参数:在x/y方向的位移 --> 根据手势的捏合程度进行缩放
[_iv setTransform:CGAffineTransformScale(_iv.transform, pinch.scale, pinch.scale)];
//把pinch.scale 比例置为1
pinch.scale = 1;
//<2>不需要把scale置为1
//参数1:等比例改变x方向的内容
//参数2:等比例改变y方向的内容
[_iv setTransform:CGAffineTransformMakeScale(pinch.scale, pinch.scale)];
}
5.长按:删除/发消息等
-(void)createLongPressGesture{
//长按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
longPress.delegate = self;
//是指手指根数
[longPress setNumberOfTouchesRequired:1];
//设置长按的最小时间值,如果不超过该时间就不会触发长按事件
[longPress setMinimumPressDuration:1];
[_iv addGestureRecognizer:longPress];
[longPress release];
}
-(void)longPressAction:(UILongPressGestureRecognizer *)longPress{
//每个手势都是有状态的
//1.如果当前手势状态是开始 --> 开始录音
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"开始录音");
}
//2.如果当前手势状态是停止 --> 录音结束,发送消息
if (longPress.state == UIGestureRecognizerStateEnded) {
//判断录音是否完成 --> 有录音才发送
NSLog(@"录音结束,发送消息");
}
}
6.滑动
-(void)createSwipeGesture{
//滑动手势:让_iv一次移动5
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
//需要指定滑动方向:一个滑动手势只能指定一个方向,如果需要设置多个方向的滑动,要创建多个滑动手势
[swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[_iv addGestureRecognizer:swipe];
[swipe release];
}
-(void)swipeAction:(UISwipeGestureRecognizer *)swipe{
//滑动
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
//如果是向左滑动 --> _iv向左移5个单位
_iv.frame = CGRectMake(_iv.frame.origin.x - 5, _iv.frame.origin.y, _iv.frame.size.width, _iv.frame.size.height);
}
}
/*
如果要想让两个没有相互影响的手势同时生效 那么必须要设置这两个手势的代理,两个手势委托代理来处理就可以了
代理需要遵守协议
比如 旋转和捏合没有相互影响可以设置同时生效 那么就需要设置旋转手势的代理和捏合手势的代理
同一个代理
*/
#pragma mark - UIGestureRecognizerDelegate协议
//是否允许 两个手势同时生效
//NO不允许
//YES允许
//两个手势 必须要设置代理
缩放+捏合必须要设置代理
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
NSLog(@"first:%@",[gestureRecognizer class]);
NSLog(@"second:%@",[otherGestureRecognizer class]);
return YES;
}