手势处理
点击
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
[self.view addGestureRecognizer:tap];
}
-(void)tap:(UITapGestureRecognizer*)param{
CGPoint point = [param locationInView:self.view];
if(CGRectContainsPoint(self.myLabel.frame, point)){
self.myLabel.backgroundColor = [UIColor yellowColor];
}else{
self.myLabel.backgroundColor = [UIColor redColor];
}
}
捏合、旋转
- (void)viewDidLoad {
[super viewDidLoad];
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
[self.view addGestureRecognizer:pinch];
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotate:)];
[self.view addGestureRecognizer:rotation];
}
-(void)pinch:(UIPinchGestureRecognizer*)param{
CGFloat scale = param.scale;
self.myImage.transform = CGAffineTransformMakeScale(scale, scale);
}
-(void)rotate:(UIRotationGestureRecognizer*)param{
CGFloat r = param.rotation;
self.myImage.transform = CGAffineTransformMakeRotation(r);
}
滑动
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];
拖动
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[self.view addGestureRecognizer:pan];
长按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[self.view addGestureRecognizer:longPress];