【问题标题】:How to achieve animated circular background button pressed effect in UIButton?如何在 UIButton 中实现动画圆形背景按钮按下效果?
【发布时间】:2021-05-22 15:42:51
【问题描述】:

动画

圆形背景按下效果

在 Android 中,我可以通过使用系统选择器以非常简单的方式实现上述效果。

<Button
    android:gravity="center"
    android:textSize="18sp"
    android:minHeight="56dp"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:background="?attr/actionBarItemBackground"
    android:text="1" />

但是,目前,我尝试在 iOS 中实现这样的功能。我正在使用UIButton

默认UIButton 的按下行为不会执行任何精美的动画和最终效果。它只是改变按钮文本的颜色。

我想知道,在 iOS 中,如何实现与我的 Android 相同的动画和最终效果?

【问题讨论】:

    标签: android ios swift


    【解决方案1】:

    你可以试试

    class CustomBu:UIButton {
        override func layoutSubviews() {
            super.layoutSubviews()
            self.layer.cornerRadius = self.frame.height / 2
        }
        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
            self.backgroundColor = .lightGray
            let animation = CABasicAnimation(keyPath: "backgroundColor")
            animation.duration = 0.2
            animation.repeatCount = 2
            animation.autoreverses = true
            animation.fromValue = UIColor.lightGray
            animation.toValue = UIColor.white
            self.layer.add(animation, forKey: "position")
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
                self.backgroundColor = .white
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-20
      • 2018-11-12
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 2019-11-27
      • 2012-03-23
      • 1970-01-01
      • 2021-04-28
      相关资源
      最近更新 更多