我创建了一个应用程序,其中有一个包含 ImageView 的 View 。 下面使用 UIPinchGestureRecognizer 缩放图像的代码正在工作。 这里 innerview 是我的 View 的名称。
- (void)viewDidLoad
{
[super viewDidLoad];
//Zoom Photo
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self actionselector(handlePinch];
[innerview addGestureRecognizer:pinchGesture];
[pinchGesture release];
mCurrentScale = 0;
mLastScale = 0;
}
-(void)handlePinchUIPinchGestureRecognizer*)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 来正确查看图像呢?
在 ScrollView 中嵌入你的 innerView 完成捏合后,您可以获取新图像的大小,然后设置其内容大小
-(void)scaleid)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];
}
希望对你有帮助
关于iphone - UIPinchGestureRecognizer 用于在 xcode 中缩放和平移图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9954582/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |