//轻拍
UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeColor:)];
tapGesture.numberOfTouchesRequired=1;
tapGesture.numberOfTapsRequired=2;
[view addGestureRecognizer:tapGesture];
[tapGesture release];
- (void)changeColor:(UITapGestureRecognizer *)tap
{
//NSLog(@"%d",tap.numberOfTouchesRequired);
[self.view viewWithTag:100].backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}
//平移
UIView *view=[self.view viewWithTag:100];
UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[view addGestureRecognizer:panGesture];
[panGesture release];
- (void)pan:(UIPanGestureRecognizer *)pan
{
CGPoint point1=[pan translationInView:pan.view];
//2D仿射变换
//1.不完善的,下次不能从最初的frame开始
//pan.view.transform=CGAffineTransformMakeTranslation(point1.x, point1.y);
//2.无论移动多少次,当下一次再平移的时候都会冲最初的frame开始
pan.view.transform=CGAffineTransformTranslate(pan.view.transform, point1.x,point1.y);
// pan.view.center=CGPointMake(pan.view.center.x+point.x,pan.view.center.y+point.y);
[pan setTranslation:CGPointZero inView:self.view];
// NSLog(@"%@",NSStringFromCGPoint([pan translationInView:pan.view]));
}
//捏合
UIPinchGestureRecognizer *pinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
[view addGestureRecognizer:pinchGesture];
[pinchGesture release];
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
//每次都会从最初的frame开始缩放
//pinch.view.transform=CGAffineTransformMakeScale(pinch.scale, pinch.scale);
pinch.view.transform=CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
pinch.scale=1;//将当前的scale置1;
}
//旋转
UIRotationGestureRecognizer *rotationGesture=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotate:)];
[view addGestureRecognizer:rotationGesture];
[rotationGesture release];
- (void)rotate:(UIRotationGestureRecognizer *)rotate
{
//rotate.view.transform=CGAffineTransformMakeRotation(rotate.rotation);
rotate.view.transform=CGAffineTransformRotate(rotate.view.transform, rotate.rotation);
//rotate.rotation=0;
}
//长按
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[view addGestureRecognizer:longPress];
[longPress release];
- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state==UIGestureRecognizerStateBegan) {
[self.view viewWithTag:100].backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}
}
//清扫
UISwipeGestureRecognizer *swipeGesture1=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip1:)];
swipeGesture1.direction=UISwipeGestureRecognizerDirectionDown;
[view addGestureRecognizer:swipeGesture1];
[swipeGesture1 release];
//给当前的视图添加四个不同方向的清扫
UISwipeGestureRecognizer *swipeGesture1=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip1:)];
swipeGesture1.direction=UISwipeGestureRecognizerDirectionDown;
[view addGestureRecognizer:swipeGesture1];
[swipeGesture1 release];
UISwipeGestureRecognizer *swipeGesture2=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip2:)];
swipeGesture2.direction=UISwipeGestureRecognizerDirectionUp;
[view addGestureRecognizer:swipeGesture2];
[swipeGesture2 release];
UISwipeGestureRecognizer *swipeGesture3=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip3:)];
swipeGesture3.direction=UISwipeGestureRecognizerDirectionRight;
[view addGestureRecognizer:swipeGesture3];
[swipeGesture3 release];
UISwipeGestureRecognizer *swipeGesture4=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip4:)];
swipeGesture4.direction=UISwipeGestureRecognizerDirectionLeft;
[view addGestureRecognizer:swipeGesture4];
[swipeGesture4 release];
- (void)swip1:(UISwipeGestureRecognizer *)swip1
{
NSLog(@"Down");
}
- (void)swip2:(UISwipeGestureRecognizer *)swip2
{
NSLog(@"Up");
}
- (void)swip3:(UISwipeGestureRecognizer *)swip3
{
NSLog(@"Right");
}
- (void)swip4:(UISwipeGestureRecognizer *)swip4
{
NSLog(@"Left");
}