ios - 在捏合时,即使在平移到不同位置后, ImageView 也会移回原始帧的原点
<p><ol>
<li><p>当操作正在进行时使用 UIPinchGestureRecognizer 缩放 ImageView 时,图像“尝试”保持在初始 frame.origin。 </p></li>
<li><p>即使在平移手势将图像移动到另一个位置之后,捏合也会立即将其移回初始位置。 </p></li>
<li><p>这个公然复制的 Apple Touches 示例应用程序的所有代码,用于演示手势。</p></li>
</ol>
<p>下面是 pinch 的代码,我在 github 上推送了一个示例应用程序来展示这种行为:<a href="https://github.com/atokubi/TestImageManipulation" rel="noreferrer noopener nofollow">https://github.com/atokubi/TestImageManipulation</a> </p>
<p>提前感谢您的帮助。 </p>
<pre><code>- (IBAction)scaleItem:(UIPinchGestureRecognizer *)gestureRecognizer {
;
if ( == UIGestureRecognizerStateBegan || == UIGestureRecognizerStateChanged) {
.transform = CGAffineTransformScale([ transform], , );
;
}
}
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
UIView *piece = gestureRecognizer.view;
CGPoint locationInView = ;
CGPoint locationInSuperview = ;
piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
piece.center = locationInSuperview;
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>如果你想正确地重新居中图像,你可以使用这个方法:</p>
<pre><code>// To re-center the image after zooming in and zooming out
- (void)centerViewContents
{
CGSize boundsSize = self.bounds.size;
CGRect contentsFrame = self.imageView.frame;
if (contentsFrame.size.width < boundsSize.width)
{
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
}
else
{
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height)
{
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
}
else
{
contentsFrame.origin.y = 0.0f;
}
self.imageView.frame = contentsFrame;
}
</code></pre>
<p>在你的“adjustAnchorPointForGestureRecognizer”中添加这个方法,方法如下:</p>
<pre><code>- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
UIView *piece = gestureRecognizer.view;
CGPoint locationInView = ;
CGPoint locationInSuperview = ;
piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
piece.center = locationInSuperview;
;
}
}
</code></pre>
<p>我下载了你的代码并实现了这个。它有效。</p>
<p>如果有帮助请告诉我。</p></p>
<p style="font-size: 20px;">关于ios - 在捏合时,即使在平移到不同位置后, ImageView 也会移回原始帧的原点,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/21764280/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/21764280/
</a>
</p>
页:
[1]