gpt4 book ai didi

ios - 为 UIView 的 bounds 属性设置动画

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:14 29 4
gpt4 key购买 nike

根据可用的 Apple 文档 here UIView 的 bounds 属性可以设置动画。

为了以编程方式旋转我的其中一个 View (在本例中是从纵向到横向),我实现了以下代码:

 float w = controlsView.bounds.size.width;
float h = controlsView.bounds.size.height;

CGAffineTransform T = CGAffineTransformMakeRotation(M_PI/2);

UIViewContentMode oldContentMode = controlsView.contentMode;
controlsView.contentMode = UIViewContentModeRedraw;

[UIView animateWithDuration:1.0 animations:^{
controlsView.bounds = CGRectMake(0, 0, h, w);
controlsView.transform = T;
}];

controlsView.contentMode = oldContentMode;

我为 contentMode 添加了两条指令,因为我在 StackOverflow 的某个地方读到 contentMode 必须设置为那个值,以便 UIView 在bounds 属性被修改(但是,在这种情况下它不会以任何方式影响行为)。

该代码的问题是 UIView 的大小不是通过动画调整的,而是一次调整的。

执行该代码,首先调整 UIView 的大小(没有动画),然后用动画旋转。

如何为调整大小设置动画?

-- 更新--

我尝试结合缩放和旋转变换。 UIView 在动画中旋转和缩放,但最后它的 bounds 属性没有改变:即使它已经旋转到横向(在我的 iPhone 上应该是 568x320),它的宽度和高度保持纵向值 (320x568)。实际上,controlsView 上的 UI 控件变形(纵横比错误)并且看起来被拉伸(stretch)了。

最佳答案

这对我有用 UIViewAnimationOptions.LayoutSubviews (Swift):

UIView.animateWithDuration(1.0,delay:0.0,
options:UIViewAnimationOptions.LayoutSubviews,
animations:{ () -> Void in
myView.transform = myRotationTransform
myView.bounds = myBounds
}, completion: nil)

(没有显式缩放也没有改变 contentMode)

关于ios - 为 UIView 的 bounds 属性设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22116147/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com