本文介绍了UIPinchGestureRecognizer用于在xcode中缩放和平移图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个应用程序,其中有一个包含图像视图的视图。
下面使用UIPinchGestureRecognizer缩放图像的代码正常工作。
这里innerview是我的观点的名称。
I have created one application in which there is one view containing an image view.Below code for zooming an image using UIPinchGestureRecognizer is working.Here innerview is the name of my view.
- (void)viewDidLoad
{
[super viewDidLoad];
//Zoom Photo
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
[innerview addGestureRecognizer:pinchGesture];
[pinchGesture release];
mCurrentScale = 0;
mLastScale = 0;
}
-(void)handlePinch:(UIPinchGestureRecognizer*)sender {
NSLog(@"latscale = %f",mLastScale);
mCurrentScale += [sender scale] - mLastScale;
mLastScale = [sender scale];
if (sender.state == UIGestureRecognizerStateEnded)
{
mLastScale = 1.0;
}
CGAffineTransform currentTransform = CGAffineTransformIdentity;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform,mCurrentScale, mCurrentScale);
innerview.transform = newTransform;
}
但如何在此使用ScrollView正确查看图像? ?
But how to use ScrollView in this to view image properly??
推荐答案
在滚动视图中嵌入内部视图
完成捏合后,可以获得新图像的大小然后设置其内容大小
Embed your innerView in Scroll ViewOnce you are done with pinching, you can get size of new image and then set its content size
-(void)scale:(id)sender {
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastScale = 1.0;
CGSize zoomViewSize = CGSizeApplyAffineTransform(imgScrollView.frame.size, [(UIPinchGestureRecognizer *)sender view].transform);
CGPoint centerPoint = CGPointApplyAffineTransform(imgScrollView.frame.origin, [(UIPinchGestureRecognizer *)sender view].transform);
CGRect r = imgScrollView.bounds;
CGSize scrollViewSize = imgScrollView.frame.size;
imgScrollView.contentSize = zoomViewSize;
imgScrollView.contentOffset = centerPoint;
}
CGFloat currentScale = [[[(UIPinchGestureRecognizer*)sender view].layer valueForKeyPath:@"transform.scale"] floatValue];
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
scale = MIN(scale, imgScrollView.maximumZoomScale / currentScale);
scale = MAX(scale, imgScrollView.minimumZoomScale / currentScale);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}
希望这可以帮到你:)
Hope this help you :)
这篇关于UIPinchGestureRecognizer用于在xcode中缩放和平移图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!