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

ios - Objective C - 无法缩放嵌入到 UIScrollView 中的 UIImageView


                                            <p><p>我是这里的新手。很高兴认识大家。</p>

<p>我正在尝试实现 Ray Wenderlich 对此处描述的 UIScrollView 的缩放:</p>

<p> <a href="http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content" rel="noreferrer noopener nofollow">http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content</a> </p>

<p>但它似乎对我不起作用。我想做的是:</p>

<ul>
<li>从 nib 创建 UIImageView。</li>
<li>将 UIImageView 添加到 UIScrollView。</li>
<li>如果应用是从 iPad 上执行的,则在 View 开始时以 2 的比例从图像的中心将其放大。 </li>
</ul>

<p>这是我的代码:</p>

<pre><code>- (void)viewDidLoad
{
    ;
    // Do any additional setup after loading the view.

    //Hide the status bar
    [ setStatusBarHidden:YES];

    //------------------------------
    //Initialize the image view of the home screen
    _mapView = (TM_MapView *)[[loadNibNamed:@&#34;MapView&#34; owner:self options:nil]objectAtIndex:0];

    // set the content size to be the size our our whole frame
    mapScrollView.contentSize = _mapView.imageView.image.size;
    ;

    //set the selector for the buttons in the map view
    ;

    // now add our scroll view to the main view
    ;
    //------------------------------

    NSLog(@&#34;_mapView.imageView.frame: %@&#34;, NSStringFromCGRect( _mapView.imageView.frame));
    NSLog(@&#34;_mapView.imageView.image.size: %@&#34;, NSStringFromCGSize( _mapView.imageView.image.size));
NSLog(@&#34;mapScrollView.contentSize: %@&#34;, NSStringFromCGSize( mapScrollView.contentSize));

}

-(void)viewWillAppear:(BOOL)animated
{
    mapScrollView.minimumZoomScale = 1.0f;
    mapScrollView.maximumZoomScale = 2.5f;
    mapScrollView.zoomScale = 1.0f;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
      ;
    }
}

-(void)zoomOnStart
{
    NSLog(@&#34;Zooming on start&#34;);
    CGPoint zoomPoint = CGPointMake(512.0f, 384.0f);

    CGFloat newZoomScale = 2.0f;

    CGSize scrollViewSize = self.mapScrollView.bounds.size;

    CGFloat w = scrollViewSize.width / newZoomScale;
    CGFloat h = scrollViewSize.height / newZoomScale;
    CGFloat x = zoomPoint.x - (w/2.0f);
    CGFloat y = zoomPoint.y - (h/2.0f);

    CGRect rectToZoomTo = CGRectMake(x, y, w, h);
    //CGRect rectToZoomTo = CGRectMake(300, 300, 200, 100);

    NSLog(@&#34;rectToZoomTo: %@&#34;, NSStringFromCGRect(rectToZoomTo));

    ;

}

#pragma mark - Delegates
-(UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView
{
    NSLog(@&#34;Scroll View viewForZoomingInScrollView&#34;);
    return _mapView;
}

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    NSLog(@&#34;Scroll View End Zooming!&#34;);
}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    NSLog(@&#34;Scroll View Zoom changed to: %f&#34;,scrollView.zoomScale);
}
</code></pre>

<p>如您所见,我记录了一些帧和代理,以查看缩放的输入并查看我们是否正在缩放。这是控制台日志:</p>

<pre><code>2014-10-09 17:24:18.507 TrueMuzeiOS _mapView.imageView.frame: {{0, 0}, {1024, 768}}
2014-10-09 17:24:18.511 TrueMuzeiOS _mapView.imageView.image.size: {1024, 768}
2014-10-09 17:24:18.513 TrueMuzeiOS mapScrollView.contentSize: {1024, 768}
2014-10-09 17:24:18.519 TrueMuzeiOS Zooming on start
2014-10-09 17:24:18.521 TrueMuzeiOS rectToZoomTo: {{320, 128}, {384, 512}}
</code></pre>

<p>如您所见,<code>viewForZoomingInScrollView:</code>、<code>scrollViewDidEndZooming:withView:atScale:</code> 和 <code>scrollViewDidZoom:</code> 内的日志没有被调用,这意味着我们根本没有缩放(CMIIW)。我已经添加了 UIScrollViewDelegate,所以它应该可以工作。</p>

<p>那么,你们能帮帮我吗?恕我直言,我已正确执行了这些步骤。所以我现在迷路了。提前非常感谢。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你是否设置了 UIScrollView 的委托(delegate)属性?</p>

<p>我没有在您发布的代码中看到这一点,尽管您可以在 Interface Builder 上完成。</p>

<p>如果是这种情况,这会让你的方法被调用:</p>

<pre><code>mapScrollView.delegate = self;
</code></pre>

<p>查看委托(delegate)属性部分的 UIScrollView 文档以获取更多信息。</p>

<p> <a href="https://developer.apple.com/library/ios/documentation/uikit/reference/uiscrollview_class/index.html#//apple_ref/occ/instp/UIScrollView/delegate" rel="noreferrer noopener nofollow">https://developer.apple.com/library/ios/documentation/uikit/reference/uiscrollview_class/index.html#//apple_ref/occ/instp/UIScrollView/delegate</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Objective C - 无法缩放嵌入到 UIScrollView 中的 UIImageView,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/26276433/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/26276433/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Objective C - 无法缩放嵌入到 UIScrollView 中的 UIImageView