欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

【IOS】使用手势对UIImageView进行缩放、旋转和移动

程序员文章站 2022-06-15 23:16:48
因为项目需要,需要对图片进行一些操作。本着拿来主义的原则~上网找了方法。 代码: view plaincopy //添加所有的手势 -(void)addGestureR...

因为项目需要,需要对图片进行一些操作。本着拿来主义的原则~上网找了方法。

代码:

view plaincopy

//添加所有的手势

-(void)addGestureRecognizerToView:(UIView*)view

{

//旋转手势

UIRotationGestureRecognizer*rotationGestureRecognizer=[[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotateView:)];

[viewaddGestureRecognizer:rotationGestureRecognizer];

//缩放手势

UIPinchGestureRecognizer*pinchGestureRecognizer=[[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinchView:)];

[viewaddGestureRecognizer:pinchGestureRecognizer];

//移动手势

UIPanGestureRecognizer*panGestureRecognizer=[[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panView:)];

[viewaddGestureRecognizer:panGestureRecognizer];

}

//处理旋转手势

-(void)rotateView:(UIRotationGestureRecognizer*)rotationGestureRecognizer

{

UIView*view=rotationGestureRecognizer.view;

if(rotationGestureRecognizer.state==UIGestureRecognizerStateBegan||rotationGestureRecognizer.state==UIGestureRecognizerStateChanged){

view.transform=CGAffineTransformRotate(view.transform,rotationGestureRecognizer.rotation);

[rotationGestureRecognizersetRotation:0];

}

}

//处理缩放手势

-(void)pinchView:(UIPinchGestureRecognizer*)pinchGestureRecognizer

{

UIView*view=pinchGestureRecognizer.view;

if(pinchGestureRecognizer.state==UIGestureRecognizerStateBegan||pinchGestureRecognizer.state==UIGestureRecognizerStateChanged){

view.transform=CGAffineTransformScale(view.transform,pinchGestureRecognizer.scale,pinchGestureRecognizer.scale);

pinchGestureRecognizer.scale=1;

}

}

//处理拖拉手势

-(void)panView:(UIPanGestureRecognizer*)panGestureRecognizer

{

UIView*view=panGestureRecognizer.view;

if(panGestureRecognizer.state==UIGestureRecognizerStateBegan||panGestureRecognizer.state==UIGestureRecognizerStateChanged){

CGPointtranslation=[panGestureRecognizertranslationInView:view.superview];

[viewsetCenter:(CGPoint){view.center.x+translation.x,view.center.y+translation.y}];

[panGestureRecognizersetTranslation:CGPointZeroinView:view.superview];

}

}

这样只需要简单调用

view plaincopy

[selfaddGestureRecognizerToView:view];

//如果处理的是图片,别忘了

[imageViewsetUserInteractionEnabled:YES];

[imageViewsetMultipleTouchEnabled:YES];

大功告成。view plaincopy

@interfaceYourViewController:UIViewController

{

CGFloatlastScale;

CGRectoldFrame;//保存图片原来的大小

CGRectlargeFrame;//确定图片放大最大的程度

}

然后在viewDidLoad里面加上

view plaincopy

-(void)viewDidLoad

{

[superviewDidLoad];

showImgView=[[UIImageViewalloc]initWithFrame:CGRectMake(0,0,320,480)];

[showImgViewsetMultipleTouchEnabled:YES];

[showImgViewsetUserInteractionEnabled:YES];

[showImgViewsetImage:[UIImageimageNamed:@"1.jpg"]];

oldFrame=showImgView.frame;

largeFrame=CGRectMake(0-screenSize.width,0-screenSize.height,3*oldFrame.size.width,3*oldFrame.size.height);

[selfaddGestureRecognizerToView:showImgView];

[self.viewaddSubview:showImgView];

这样就实现了

但是,这样是不够的。

因为里边的缩放和移动等没有做相应的判断。

因为代码很简洁,所以扩展也非常方便。

我修改了缩放的代码,增加了限制,其他的类似

view plaincopy

//处理缩放手势

-(void)pinchView:(UIPinchGestureRecognizer*)pinchGestureRecognizer

{

UIView*view=pinchGestureRecognizer.view;

if(pinchGestureRecognizer.state==UIGestureRecognizerStateBegan||pinchGestureRecognizer.state==UIGestureRecognizerStateChanged){

view.transform=CGAffineTransformScale(view.transform,pinchGestureRecognizer.scale,pinchGestureRecognizer.scale);

if(showImgView.frame.size.width showImgView.frame=oldFrame;

//让图片无法缩得比原图小

}

if(showImgView.frame.size.width>3*oldFrame.size.width){

showImgView.frame=largeFrame;

}

pinchGestureRecognizer.scale=1;

}

}

这样就好了。保证了图片的最大和最小比例。