增加子视图尺寸时,UIScrollView会缩放子视图

时间:2013-06-17 07:22:19

标签: ios cocoa-touch uiscrollview catiledlayer

我有一个UIScrollView,它包含一个使用CATiledLayer从UIView派生的视图。基本上,在我的ViewController viewDidLoad中:

_tiledView = [[TiledView alloc] initWithFrame:rect tileSize:_tileSize];

_scrollView = [[ScrollingView alloc] initWithFrame:rect];
_scrollView.contentSize = _tiledView.frame.size;
_scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
_scrollView.scrollEnabled = YES;
_scrollView.delegate = self;

[_scrollView addSubview:_tiledView];

最初,_tiledView是一个4x4网格的256x256磁贴。我正在尝试在运行时增加_tiledView的维度。在构建_tiledView时,我只需通过将平铺数乘以其大小来计算视图的大小。然后我设置_tiledView.frame_tiledView.bounds的大小,例如:

CGRect frame = self.frame;
frame.origin = CGPointZero;
frame.size = CGSizeMake(tileSize.width*4, tileSize.height*4);
self.frame = frame;
self.bounds = frame;

现在,当我点击界面中的一个按钮时,我想要完成的第一步就是将_tiledView的尺寸增加一个256x256平铺到右边和底部。这就是我的尝试:

- (void)addTiles:(id)sender
{
    CGRect rect = _tiledView.frame;
    rect.size.width += _tileSize.width;
    rect.size.height += _tileSize.height;
    _tiledView.frame = rect;
    _tiledView.bounds = rect;
    CGSize size = _scrollView.contentSize;
    size.width += _tileSize.width;
    size.height += _tileSize.height;
    _scrollView.contentSize = size;
    [_scrollView setNeedsLayout];
}

但是,这不能按预期工作。会发生的事情是_tiledView变得更大,好像它已经放大了一样 - 与开头的瓷砖数量相同,即4x4。我检查了_scrollView.contentsScaleFactor属性,然后显示1.0。我假设_scrollView在技术上没有缩放内容。

我期待_tileView保持在界面中的当前位置,但添加9个新图块,即右边4个,底部4个,右下角1个。相反,最初的16块瓷砖变得更大,以填充可能由25块瓷砖填充的空间。

我错过了什么?我究竟做错了什么?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果有人发现它有用。在进一步挖掘之后,我意识到我的contentMode默认为ScaleToFill。所以我把它设置为:

_tiledView.contentMode = UIViewContentModeRedraw;

初始化。并调整了addTiles,如下所示:

CGRect rect = _tiledView.frame;
rect.size.width += _tileSize.width;
rect.size.height += _tileSize.height;
_tiledView.frame = rect;
_tiledView.bounds = rect;
_scrollView.contentSize = rect.size;
[_tiledView setNeedsDisplay];

并且,这有我正在寻找的效果。

相关问题