gpt4 book ai didi

ios - UIView动画,如何让它流畅?

转载 作者:行者123 更新时间:2023-11-29 02:43:38 25 4
gpt4 key购买 nike

我尝试实现简单的动画来更改 View 位置我使用以下代码:

[UIView animateWithDuration:0.4 animations: ^{
self.greenIm.frame = CGRectMake(self.roundView.frame.origin.x, self.roundView.frame.origin.y - 110, 60, 60);
self.greenIm.layer.cornerRadius = self.greenIm.frame.size.height / 2;
}

但我所看到的是,我的圆形 View self.greenIm 在没有四舍五入的情况下转到了新位置,并且只有在最后才保持圆形,看起来很糟糕。

稍后我想更改另一个 View 的大小,但我需要相同的中心:

[UIView animateWithDuration:0.4 animations: ^{
self.cup.frame = CGRectMake(self.cup.frame.origin.x, self.cupImage.frame.origin.y, self.cup.frame.size.width - 20, self.cup.frame.size.height - 20);
self.cup.center = self.centerPoint;
}

这里我看到相同的,第一个 View 更改大小然后居中。

如何解决?我是如何理解 UIView 动画动画的所有属性不是一次性的。请帮忙。

最佳答案

对于您的第一个问题,Apple 文档说您可以为属性设置动画,这些属性是可动画的。

A block object containing the changes to commit to the views. This is where you programmatically change any animatable properties of the views in your view hierarchy. This block takes no parameters and has no return value. This parameter must not be NULL.

如果您查看有关 UIView 属性的文档,您会发现 layer 不可设置动画。您需要使用 Core Animation 来实现这一点。使用下面的代码。我为此示例代码使用了 UILabel。看看这是否有帮助。

  CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
[anim setFromValue:[NSValue valueWithNonretainedObject:[NSNumber numberWithDouble:10.0]]];
lblView.layer.cornerRadius = 30.0;
[anim setDuration:6];
[lblView.layer addAnimation:anim forKey:@"move"];

以同样的方式,您可以为 position 属性创建另一个动画对象,并根据需要更改帧。将该动画添加到,两个动画将并行开始。

关于ios - UIView动画,如何让它流畅?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25418554/

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