UIView 在动画时光栅化子视图
我正在对一个 UIView 进行动画处理,其中包含用 CGPath 元素绘制的图形。
myView.frame=CGRectMake(0,0,1024,768);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
myView.frame=CGRectMake(0,0,0,768);
[UIView commitAnimations];
第一次运行此代码块时效果很好,但是当我稍后使用 setNeedsDisplay 使用它时,我无法强制 myView 更新。当我执行代码时,动画会按预期发生,但它始终显示我发送的第一个图表。我已确认在更新之前将正确的信息发送到视图。 当我对此进行故障排除时,我将 myView 的目标宽度设置为 80。 这会导致一切正常工作,除了我的 CGPath 绘图似乎是光栅化的(某些图形的视图变得模糊,其他视图看起来有点像素化)。当宽度设置为 0 时,不会发生光栅化。这导致我提出 3 个问题:
1)如果视图的宽度为零,我是否无法强制重绘视图(当宽度为 0 时,它会“消失”吗) ?
2)如何在不栅格化内容的情况下为该视图设置动画?这似乎只发生在我改变视图的尺寸,而不是位置或不透明度时。
3)为什么当我将动画设置为零宽度时,内容不会光栅化?
I am animating a UIView that contains a graph drawn with CGPath elements
myView.frame=CGRectMake(0,0,1024,768);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
myView.frame=CGRectMake(0,0,0,768);
[UIView commitAnimations];
This works fine the first time that I run this block of code, however I am unable to force myView to update when I use it later by using setNeedsDisplay. When I execute the code the animation happens as expected but it always displays the first graph I send it. I've confirmed that the correct information is being sent to the view before I update it.
While I was troubleshooting this I set the destination width of myView to 80.
This causes everything to work properly EXCEPT my CGPath drawing seems to be rasterizing (with some graphs the view gets blurry, others the view looks slightly pixellated). There is NO rasterization that happens when the width is set to 0. This leads me to 3 questions:
1) Am I unable to force a view to redraw if it has a zero width (Does it 'disappear' when it's width is 0)?
2) How do I animate this view without rasterizing the content? This only seems to happen when I change the dimensions of the view, not the position or opacity.
3) Why does the content NOT rasterize when I animate to zero width?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于零宽度视图没有区域,因此它不想绘制自身也就不足为奇了。
当您更改视图的大小时,视图使用其
contentMode
属性来决定如何重绘其内容。contentMode
的默认值为UIViewContentModeScaleToFit
,这意味着它只是缩放以前的内容以适应新的大小。尝试将视图的contentMode
属性设置为UIViewContentModeRedraw
。这告诉它在调整大小时发送自己setNeedsDisplay
。Since a zero-width view has no area, it's not surprising that it doesn't want to draw itself.
When you change the size of a view, the view uses its
contentMode
property to decide how to redraw its contents. The default value ofcontentMode
isUIViewContentModeScaleToFit
, which means that it simply scales its previous contents to fit the new size. Try setting the view'scontentMode
property toUIViewContentModeRedraw
. This tells it to send itselfsetNeedsDisplay
when it is resized.