菜鸟教程小白 发表于 2022-12-13 12:39:37

iphone - UIPinchGestureRecognizer 用于在 xcode 中缩放和平移图像


                                            <p><p>我创建了一个应用程序,其中有一个包含 ImageView 的 View 。
下面使用 UIPinchGestureRecognizer 缩放图像的代码正在工作。
这里 innerview 是我的 View 的名称。</p>

<pre><code> - (void)viewDidLoad
    {
;

//Zoom Photo
UIPinchGestureRecognizer *pinchGesture = [ initWithTarget:self action:@selector(handlePinch:)];
;
;
mCurrentScale = 0;
mLastScale = 0;
}

-(void)handlePinch:(UIPinchGestureRecognizer*)sender {

NSLog(@&#34;latscale = %f&#34;,mLastScale);

mCurrentScale += - mLastScale;
mLastScale = ;

if (sender.state == UIGestureRecognizerStateEnded)
{
    mLastScale = 1.0;
}

CGAffineTransform currentTransform = CGAffineTransformIdentity;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform,mCurrentScale, mCurrentScale);
innerview.transform = newTransform;

}
</code></pre>

<p>但是如何使用 ScrollView 来正确查看图像呢?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在 ScrollView 中嵌入你的 innerView
完成捏合后,您可以获取新图像的大小,然后设置其内容大小</p>

<pre><code>-(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:@&#34;transform.scale&#34;] 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];   
}
</code></pre>

<p>希望对你有帮助:) </p></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - UIPinchGestureRecognizer 用于在 xcode 中缩放和平移图像,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/9954582/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/9954582/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - UIPinchGestureRecognizer 用于在 xcode 中缩放和平移图像