您的位置:首页 > 产品设计 > UI/UE

#iOS开发笔记#UIImageView实现拖动,放大/缩小,旋转

2015-01-08 11:54 751 查看
UIImageView是iOS上最基本的图片显示控件,但默认情况下是不支持拖动,放大缩小等功能的。后来在stackoverflow上查找到一种很简单的方法,加上本人自己的理解,步骤整理如下:

1.
开启userInteractionEnabled

首先看苹果官方对UIImageView相关内容地介绍:(UIImageView
Class Reference)

New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of 
UIImageView
,
you must explicitly change the value of the
userInteractionEnabled
 property
to 
YES
 after
initializing the object.

所以首先第一步要set这个属性

[imageView setUserInteractionEnabled:YES];


2. 在当前controller中添加UIGestureRecognizerDelegate协议

@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>

3.
重写gestureRecognizer方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}


4.
定义手势引发的操作方法。UIImageView对象主要应用三种手势操作:UIPinchGestureRecognizer,UIPanGestureRecognizer和UIRotationGestureRecognizer,分别对应(二指)放大缩小,拖动和(二指)旋转的手势操作。三个对应方法如下:

- (void)pinchGestureDetected:(UIPinchGestureRecognizer *)recognizer
{
UIGestureRecognizerState state = [recognizer state];

if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
CGFloat scale = [recognizer scale];
[recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform, scale, scale)];
[recognizer setScale:1.0];
}
}

- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer
{
UIGestureRecognizerState state = [recognizer state];

if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
CGPoint translation = [recognizer translationInView:recognizer.view];
[recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)];
[recognizer setTranslation:CGPointZero inView:recognizer.view];
}
}

- (void)rotationGestureDetected:(UIRotationGestureRecognizer *)recognizer
{
UIGestureRecognizerState state = [recognizer state];

if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
CGFloat rotation = [recognizer rotation];
[recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, rotation)];
[recognizer setRotation:0];
}
}


5. 新建三种recognizer对象,分别调用上一步的三种方法,并绑定到UIImageView对象中

UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureDetected:)];
[pinchGestureRecognizer setDelegate:self];
[imageView addGestureRecognizer:pinchGestureRecognizer];

UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureDetected:)];
[rotationGestureRecognizer setDelegate:self];
[imageView addGestureRecognizer:rotationGestureRecognizer];

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureDetected:)];
[panGestureRecognizer setDelegate:self];
[imageView addGestureRecognizer:panGestureRecognizer];

Done. 经过上述五步,imageView已经能实现放大缩小,拖动和旋转的功能,全部参考代码如下:

MyViewController.h

@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
...

@end


MyViewController.m

@implementation MyViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self enableGestureOnView:self.imageView];

}

#pragma mark - View Gesture Process

- (void)enableGestureOnView:(UIView *)view{
[view setUserInteractionEnabled:YES];
// create and configure the pinch gesture
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRe
4000
cognizer alloc] initWithTarget:self action:@selector(pinchGestureDetected:)];
[pinchGestureRecognizer setDelegate:self];
[view addGestureRecognizer:pinchGestureRecognizer];

// create and configure the rotation gesture
UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureDetected:)];
[rotationGestureRecognizer setDelegate:self];
[view addGestureRecognizer:rotationGestureRecognizer];

// creat and configure the pan gesture
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureDetected:)];
[panGestureRecognizer setDelegate:self];
[view addGestureRecognizer:panGestureRecognizer];

}

- (void)pinchGestureDetected:(UIPinchGestureRecognizer *)recognizer
{
UIGestureRecognizerState state = [recognizer state];

if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
CGFloat scale = [recognizer scale];
[recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform, scale, scale)];
[recognizer setScale:1.0];
}
}

- (void)rotationGestureDetected:(UIRotationGestureRecognizer *)recognizer
{
UIGestureRecognizerState state = [recognizer state];

if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
CGFloat rotation = [recognizer rotation];
[recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, rotation)];
[recognizer setRotation:0];
}
}

- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer
{
UIGestureRecognizerState state = [recognizer state];

if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
CGPoint translation = [recognizer translationInView:recognizer.view];
[recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)];
[recognizer setTranslation:CGPointZero inView:recognizer.view];
}
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}


参考资料:
http://stackoverflow.com/questions/3907397/uigesturerecognizer-on-uiimageview/18622373#18622373 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImageView_Class/index.html

另:iOS中如何限制UIImageView放大缩小的比例?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐