UIView gusterVier=[[UIView alloc]initWithFrame:CGRectMake(50, 10, 270, 400)];
gusterVier.backgroundColor=[UIColor orangeColor];
[self.view addSubview: gusterVier];
//允许多指操作
self.view.multipleTouchEnabled=YES;
//[self createTapGuster];
//[self creatLongGesture];
// [self creatPanGesture];
//[self createPinchGesture];
//[self creatRotationGesture];
[self creatSwipeGusture];
#pragma mark -----创建手势-----
//1 点击手势
-(void)createTapGuster
{
UITapGestureRecognizer*tapGuster=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
//允许几根手指同时点击方法
tapGuster.numberOfTouchesRequired=2;
//手指连续点击的触发方法
tapGuster.numberOfTapsRequired=2;
//添加手势方法
[gusterVier addGestureRecognizer:tapGuster];
// [ges]
//
}
//2 长安手势
-(void)creatLongGesture
{
UILongPressGestureRecognizer*longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[gusterVier addGestureRecognizer:longPress];
}
//3 拖动手势
-(void)creatPanGesture
{
UIPanGestureRecognizer * panGusture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
//最少几根手指拖动
panGusture.minimumNumberOfTouches=1;
//最多几根手指拖动
panGusture.maximumNumberOfTouches=2;
[gusterVier addGestureRecognizer:panGusture];
}
//4 捏合手势
-(void)createPinchGesture
{
UIPinchGestureRecognizer*pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
[gusterVier addGestureRecognizer:pinch];
}
//5 旋转手势
-(void)creatRotationGesture
{
UIRotationGestureRecognizer* rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
[gusterVier addGestureRecognizer:rotation];
}
//侧滑
-(void)creatSwipeGusture
{
UISwipeGestureRecognizer*swip=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip:)];
//默认向右滑
/**
UISwipeGestureRecognizerDirectionRight 右滑,
UISwipeGestureRecognizerDirectionLeft 左滑,
UISwipeGestureRecognizerDirectionUp 上滑,
UISwipeGestureRecognizerDirectionDown 下滑
*/
swip.direction=UISwipeGestureRecognizerDirectionDown;
[gusterVier addGestureRecognizer:swip];
//将四个方向 都添加
for (int i=0; i<4; i++)
{
UISwipeGestureRecognizer* swipeGestuer=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip:)];
swipeGestuer.direction = 1 << i;
[gusterVier addGestureRecognizer:swipeGestuer];
}
}
#pragma mark -----手势触发的方法-----
-(void)tap:(UITapGestureRecognizer*)tap
{
self.view.backgroundColor=[UIColor greenColor];
}
//长按点击方法
-(void)longPress:(UILongPressGestureRecognizer*)longPress
{
//动画类的类型
CABasicAnimation* animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//旋转幅度
animation.fromValue=[NSNumber numberWithFloat:-0.2 ];
animation.toValue=[NSNumber numberWithFloat:0.2];
//重复次数
animation.repeatCount=CGFLOAT_MAX;
//是否允许左右摆动
animation.autoreverses=YES;
//添加动画
[gusterVier.layer addAnimation:animation forKey:@"image"];
}
-(void)pan:(UIPanGestureRecognizer*)pan
{
//拖动速度
CGPoint speed=[pan velocityInView:gusterVier];
NSLog(@"speed = %@",NSStringFromCGPoint(speed));
//拖动的位移
CGPoint translation=[pan translationInView:gusterVier ];
NSLog(@"traslation=%@",NSStringFromCGPoint(translation));
//还可以得到在self.view 的点 通过手势
CGPoint center=[pan locationInView:gusterVier];
gusterVier.center=center;
}
-(void)pinch:(UIPinchGestureRecognizer*)pinch
{
//得到捏合的比例
CGFloat scale=pinch.scale;
/**
* 按照此比例形变
*/
gusterVier.transform=CGAffineTransformMakeScale(scale, scale);
}
-(void)rotation:(UIRotationGestureRecognizer*)rotation
{
//得到旋转的弧度
CGFloat angle=rotation.rotation;
gusterVier.transform=CGAffineTransformMakeRotation(angle);
}
//滑动手势
-(void)swip:(UISwipeGestureRecognizer*)swip
{
NSLog(@"侧滑");
if (swip.direction==UISwipeGestureRecognizerDirectionUp)
{
NSLog(@"向上滑");
}else if(swip.direction==UISwipeGestureRecognizerDirectionDown)
{
NSLog(@"向下滑");
}else if(swip.direction==UISwipeGestureRecognizerDirectionLeft)
{
NSLog(@"向左滑");
}else if(swip.direction==UISwipeGestureRecognizerDirectionRight)
{
NSLog(@"向右滑");
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[gusterVier.layer removeAnimationForKey:@"image"];
//停止所有动画 暴力型的
//[gusterVier.layer removeAllAnimations];
}