UIView 动画无法正常工作

发布于 2024-10-02 14:51:35 字数 1385 浏览 0 评论 0原文

我有一个动画,可以“抬起”按钮并放下阴影。例如,我就是这样做的。

button1shadow.alpha = 0;
button1shadow.hidden = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
CGRect frame11 =searchByName.frame;
frame11.origin.y -=20;
frame11.origin.x +=20;
searchByName.frame = frame11;
searchByNameLabel.frame = frame11;
CGRect frame21 = button1shadow.frame;
frame21.origin.y +=10;
frame21.origin.x -=10;
button1shadow.frame = frame21;
searchByName.alpha      = 1;
button1shadow.alpha     = 0.1;
[UIView commitAnimations]; 

如果我自己运行它,效果就很好。

然后我把这段代码放在上面的代码后面。

button1shadow.hidden = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:2.0];
[UIView setAnimationDuration:1.5];
CGRect frame11 =searchByName.frame;
frame11.origin.y +=20;
frame11.origin.x -=20;
searchByName.frame = frame11;
searchByNameLabel.frame = frame11;
CGRect frame21 = button1shadow.frame;
frame21.origin.y -=10;
frame21.origin.x +=10;
button1shadow.frame = frame21;
searchByName.alpha      = 1;
button1shadow.alpha     = 0.1;
[UIView commitAnimations]; 

两者都可以单独工作,但是当我将它们组合在一起时,第一个动画不会显示,但按钮会跳转到我分配的坐标而不是动画。有什么想法吗?

编辑: 我还尝试使用以下方法调用第二个动画:

[NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(lowerButtons) userInfo:nil repeats:NO];

但是,它似乎甚至没有调用“lowerButtons”。

I have an animation that "lifts" up a button and drops a shadow. Here's how I do it, for example.

button1shadow.alpha = 0;
button1shadow.hidden = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
CGRect frame11 =searchByName.frame;
frame11.origin.y -=20;
frame11.origin.x +=20;
searchByName.frame = frame11;
searchByNameLabel.frame = frame11;
CGRect frame21 = button1shadow.frame;
frame21.origin.y +=10;
frame21.origin.x -=10;
button1shadow.frame = frame21;
searchByName.alpha      = 1;
button1shadow.alpha     = 0.1;
[UIView commitAnimations]; 

This works just fine if I run it by itself.

Then I put this code right after the code above.

button1shadow.hidden = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:2.0];
[UIView setAnimationDuration:1.5];
CGRect frame11 =searchByName.frame;
frame11.origin.y +=20;
frame11.origin.x -=20;
searchByName.frame = frame11;
searchByNameLabel.frame = frame11;
CGRect frame21 = button1shadow.frame;
frame21.origin.y -=10;
frame21.origin.x +=10;
button1shadow.frame = frame21;
searchByName.alpha      = 1;
button1shadow.alpha     = 0.1;
[UIView commitAnimations]; 

Both work fine seperately, but when I combine them right after each other the first animation is not displayed, but the button jumps up to the coordinates I assigned instead of animating. Any ideas?

Edit:
I also tried to call the second animation by using:

[NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(lowerButtons) userInfo:nil repeats:NO];

However, it didn't seem to even call "lowerButtons".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笛声青案梦长安 2024-10-09 14:51:35

您正在设置动画延迟,所以这应该可以工作,但是您是否尝试过在委托回调中执行第二个动画?查看 UIView+setAnimationDelegate:+setAnimationDidStopSelector: 方法。

You’re setting an animation delay, so this should work, but have you tried doing the second animation in a delegate callback? Check out UIView’s +setAnimationDelegate: and +setAnimationDidStopSelector: methods.

云仙小弟 2024-10-09 14:51:35

当您提交动画时,UIView(或CALayer)上的属性会立即设置。例如,如果您将视图向上移动 100 像素,一旦您提交了该动画,视图的框架就会显示它是 100 像素,即使它看起来还不是这样。如果您想将动画一个接一个地排列起来,则需要直接使用 CAAnimation,或者在 animationDidStop: 委托回调中设置第二个动画第一个。要注册委托回调,请参阅+[UIView setAnimationDelegate:]

或者,如果第二个动画只是第一个动画的反转,您可以尝试使用 +[UIView setAnimationRepeatAutoreverses:]+[UIView setAnimationRepeatCount:] 方法。

When you commit an animation, the properties on the UIView (or CALayer) are set straight away. E.g. if you move a view up by 100px, as soon as you've committed that animation, the view's frame will show that it is 100px, even though it doesn't look like it (yet). If you want to queue animations up one after the other, you need to look at using CAAnimations directly, or setting up the second animation in the animationDidStop: delegate callback from the first one. To register for the delegate callback, see +[UIView setAnimationDelegate:].

Alternatively, if the second animation is simply the first one in reverse, you can try using the +[UIView setAnimationRepeatAutoreverses:] and +[UIView setAnimationRepeatCount:] methods.

新雨望断虹 2024-10-09 14:51:35

我认为它的工作原理是,如果您使用给定的 AnimationID 启动一个动画,然后运行另一个具有相同 ID 的动画,则第二个动画将“覆盖”第一个动画。

因此,答案是在每个 beginAnimation 块中使用两个不同的animationID。

我不太确定你传递“nil”的事实如何影响这个逻辑。只有一种方法可以找出答案...

I think the way it works is that if you started one animation with a given AnimationID, then ran another animation with the same ID, the second would "override" the first.

Thus, the answer would be to use two different animationIDs in each beginAnimation block.

I'm not quite sure how the fact that you are passing "nil" affects this logic. Only one way to find out...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文