需要添加协议
// 添加所有的手势
- (void) addGestureRecognizerToVi
ew:(UIView *)view
{
// 旋转手势
UIRotationGestureRecogni
zer *rotationGestureRecognize
r = [[UIRotationGestureRecogni
zer alloc] initWithTarget:self action:@selector(rotateView:)];
[view addGestureRecognizer:rotationGestureRecognize
r];
// 缩放手势
UIPinchGestureRecognizer
*pinchGestureRecognizer = [[UIPinchGestureRecognizer
alloc] initWithTarget:self action:@selector(pinchView:)];
[view addGestureRecognizer:pinchGestureRecognizer];
// 移动手势
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
[view addGestureRecognizer:panGestureRecognizer];
}
// 处理旋转手势
- (void) rotateView:(UIRotationGestureRecogni
zer *)rotationGestureRecognize
r
{
UIView *view = rotationGestureRecognize
r.view;
if (rotationGestureRecognize
r.state == UIGestureRecognizerState
Began || rotationGestureRecognize
r.state == UIGestureRecognizerState
Changed) {
view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognize
r.rotation);
[rotationGestureRecognize
r setRotation:0];
}
}
// 处理缩放手势
- (void) pinchView:(UIPinchGestureRecognizer
*)pinchGestureRecognizer
{
UIView *view = pinchGestureRecognizer.view;
if (pinchGestureRecognizer.state == UIGestureRecognizerState
Began || pinchGestureRecognizer.state == UIGestureRecognizerState
Changed) {
view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
pinchGestureRecognizer.scale = 1;
}
}
// 处理拖拉手势
- (void) panView:(UIPanGestureRecognizer *)panGestureRecognizer
{
UIView *view = panGestureRecognizer.view;
if (panGestureRecognizer.state == UIGestureRecognizerState
Began || panGestureRecognizer.state == UIGestureRecognizerState
Changed) {
CGPoint translation = [panGestureRecognizer translationInView:view.superview];
[view setCenter:(CGPoint){view.center.x + translation.x, view.center.y + translation.y}];
[panGestureRecognizer setTranslation:CGPointZero inView:view.superview];
}
}
//如果处理的是图片,别忘了
[imageView setUserInteractionEnable
d:YES];
[imageView setMultipleTouchEnabled:YES];