OStack程序员社区-中国程序员成长平台

标题: ios - Objective C - 无法缩放嵌入到 UIScrollView 中的 UIImageView [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:05
标题: ios - Objective C - 无法缩放嵌入到 UIScrollView 中的 UIImageView

我是这里的新手。很高兴认识大家。

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

http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content

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

这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

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

    //------------------------------
    //Initialize the image view of the home screen
    _mapView = (TM_MapView *)[[[NSBundle mainBundle]loadNibNamed"MapView" owner:self options:nil]objectAtIndex:0];

    // set the content size to be the size our our whole frame
    mapScrollView.contentSize = _mapView.imageView.image.size;
    [mapScrollView setScrollEnabled:YES];

    //set the selector for the buttons in the map view
    [self setButtonsSelector];

    // now add our scroll view to the main view
    [mapScrollView addSubview:_mapView];
    //------------------------------

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

}

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

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        [self zoomOnStart];
    }
}

-(void)zoomOnStart
{
    NSLog(@"Zooming on start");
    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(@"rectToZoomTo: %@", NSStringFromCGRect(rectToZoomTo));

    [self.mapScrollView zoomToRect:rectToZoomTo animated:YES];

}

#pragma mark - Delegates
-(UIView*)viewForZoomingInScrollViewUIScrollView*)scrollView
{
    NSLog(@"Scroll View viewForZoomingInScrollView");
    return _mapView;
}

-(void)scrollViewDidEndZoomingUIScrollView *)scrollView withViewUIView *)view atScaleCGFloat)scale
{
    NSLog(@"Scroll View End Zooming!");
}

-(void)scrollViewDidZoomUIScrollView *)scrollView
{
    NSLog(@"Scroll View Zoom changed to: %f",  scrollView.zoomScale);
}

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

2014-10-09 17:24:18.507 TrueMuzeiOS[13268:60b] _mapView.imageView.frame: {{0, 0}, {1024, 768}}
2014-10-09 17:24:18.511 TrueMuzeiOS[13268:60b] _mapView.imageView.image.size: {1024, 768}
2014-10-09 17:24:18.513 TrueMuzeiOS[13268:60b] mapScrollView.contentSize: {1024, 768}
2014-10-09 17:24:18.519 TrueMuzeiOS[13268:60b] Zooming on start
2014-10-09 17:24:18.521 TrueMuzeiOS[13268:60b] rectToZoomTo: {{320, 128}, {384, 512}}

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

那么,你们能帮帮我吗?恕我直言,我已正确执行了这些步骤。所以我现在迷路了。提前非常感谢。



Best Answer-推荐答案


你是否设置了 UIScrollView 的委托(delegate)属性?

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

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

mapScrollView.delegate = self;

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

https://developer.apple.com/library/ios/documentation/uikit/reference/uiscrollview_class/index.html#//apple_ref/occ/instp/UIScrollView/delegate

关于ios - Objective C - 无法缩放嵌入到 UIScrollView 中的 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26276433/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4