手势 Gesture
-
(void)createLabel {
label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
label.backgroundColor = [UIColor magentaColor];
label.text = @"How old are you?";
label.numberOfLines = 0;
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentCenter;
label.userInteractionEnabled = YES;[self.view addSubview:label];
} -
(void)createRotation {
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)];
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGesture:)];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGesture:)];
swipeGesture.numberOfTouchesRequired = 1;
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;[label addGestureRecognizer:swipeGesture];
[label addGestureRecognizer:pan];
[label addGestureRecognizer:pinch];
[label addGestureRecognizer:rotation];
} -
(void)rotationGesture:(UIRotationGestureRecognizer *)rotation {
label.transform = CGAffineTransformRotate(label.transform, rotation.rotation);
// 复位
rotation.rotation = 0;
}
-(void)pinchGesture:(UIPinchGestureRecognizer *)pinch {
label.transform = CGAffineTransformScale(label.transform, pinch.scale, pinch.scale);
// 复位
pinch.scale = 1;
}
-
(void)panGesture:(UIPanGestureRecognizer *)pan{
//获取偏移量
// 返回的是相对于最原始的手指的偏移量
CGPoint transP = [pan translationInView:label];
// 移动图片控件
label.transform = CGAffineTransformTranslate(label.transform, transP.x, transP.y);
// 复位,表示相对上一次
[pan setTranslation:CGPointZero inView:label];
} -
(void)swipeGesture:(UISwipeGestureRecognizer *)swipe{
NSLog(@"执行的swipe");
swipe.direction = UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;
}