ios - UIButton 文本动画不动画

标签 ios swift uibutton fadein animatewithduration

在按钮的 touchUpInside 上,它运行一系列代码(所有数据相关,主函数中的代码都没有修改 View 对象)代码所做的最后一件事是调用我粘贴在下面的动画函数.问题是,动画看起来不尽如人意。

想要的动画:

  1. footerImg 应该淡出
  2. footerBar 文本应该淡入
  3. footerBar 文本应该淡出
  4. footerImg 应该淡入

文本和图像在屏幕上占据相同的位置,所以视觉上应该是一个替换另一个,然后又回到原来的状态。 View 不相互嵌套,它们共享同一个容器。

在模拟器中看到的实际动画:

-footerImg 永远不会出现 -footerBar 文本在点击触发器后立即出现,没有动画 -footerBar 文本淡出 -footerImg 淡入

footerBar 是一个 UIButton

-开始状态,在 Action 开始之前,按钮是空的且清晰(用户不可见)

-以防万一,这不是触发功能的按钮

-有一个清晰的背景,所以它的文字应该是唯一动画进/出的东西

footerImg 是一个 UIImageView

-只是一个图标图片,没什么特别的

func animateFeedback() {
    UIView.animateWithDuration(0.25, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0, options: nil, animations: {
            self.footerImg.alpha = 0
            self.footerBar.alpha = 0

    },
        completion: { finished in
            self.footerBar.setTitle("Item added", forState: UIControlState.Normal)
        }
    )

    UIView.animateWithDuration(1.5, delay: 0.5, options: nil, animations: {
            self.footerBar.alpha = 1
        }
        , completion: nil
    )
    UIView.animateWithDuration(0.75, delay: 1.25, options: nil, animations: {
            self.footerBar.alpha = 0
        }
        , completion: { finished in
            self.footerBar.setTitle("", forState: UIControlState.Normal)
        }
    )
    UIView.animateWithDuration(0.25, delay: 1.5, options: nil, animations: {
            self.footerImg.alpha = 1
        }
        , completion: nil
    )
}

最佳答案

对于序列动画,您应该将动画放在完成 block 中。例如

页脚栏文本应淡入

页脚栏文本应该淡出

试试这个:

        self.footerBar.alpha = 0;

        UIView.animateWithDuration(1.5, delay: 0.5, options: nil, animations: {
            self.footerBar.alpha = 1
        }
        , completion: { finished in
            UIView.animateWithDuration(0.75, delay: 1.25, options: nil, animations: {
               self.footerBar.alpha = 0
            }
            , completion: { finished in
               self.footerBar.setTitle("", forState: UIControlState.Normal)
            }
            )
       }
       )

其他人也一样

关于ios - UIButton 文本动画不动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32115215/

相关文章:

objective-c - UIScrollView + UIImage 和缩放 : not fluid

swift - 在 Swift 4 中传递 JSON

ios - UIButton 的子类无法调用 buttonType 初始值设定项

iOS:保存日期选择器的设置

ios - UITableviewcontroller 中的 UITableview - 重新加载时未调用 cellforrowatindexpath

ios - 拖放以在列表 SwiftUI 中移动行

ios - 确定 Objective C 中哪个 UIButton 触发了事件

ios - 自动应用 cap insets 的 UIButton 子类

ios - 从sqlite数据库获取数据-iOS

swift - 为什么下面的协议(protocol)有这个必需的功能?