gpt4 book ai didi

ios - UIKit 方法中的 "animated" bool 参数是否与设置动画持续时间不同?

转载 作者:行者123 更新时间:2023-12-01 18:57:34 25 4
gpt4 key购买 nike

我正在实现一个方法,该方法具有一个名为 animated 的 bool 参数。类似于许多 UIKit 方法。为了使实现简单,我想写:

- (void)showElement:(BOOL)animated
{
CGFloat duration = animated ? 0.25 : 0;
[UIView animateWithDuration:duration animations:^{
// animation code
} completion:^(BOOL finished) {
// completion code
}];
}

这是正确的还是有必要写出两次 UI 代码?
- (void)showElement:(BOOL)animated
{
if (animated) {
[UIView animateWithDuration:0.25 animations:^{
// animation code
} completion:^(BOOL finished) {
// completion code
}];
} else {
// animation code
// completion code
}
}

最佳答案

我这样写 - 它首先创建 block ,然后将其提供给 UIView 动画方法或自行执行:

- (void) showElement:(BOOL) animated
{
void (^animations)(void) = ^{
// animation code
};

if(animated)
{
[UIView animateWithDuration:0.25 animations:animations];
}
else
{
animations();
}
}

编辑:您也可以对完成 block 执行相同的操作

关于ios - UIKit 方法中的 "animated" bool 参数是否与设置动画持续时间不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25578884/

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