【问题标题】:UIButton with shadow rotating without shadow rotatingUIButton 有阴影旋转没有阴影旋转
【发布时间】:2021-02-09 09:13:28
【问题描述】:

目前我有一个从 UIButton 扩展中提取其属性的按钮。

    func mapControllerButtons(image: String, selector: Selector) -> UIButton {
        let button = UIButton()
        button.setImage(UIImage(named: image)!.withRenderingMode(.alwaysOriginal), for: .normal)
        button.layer.shadowColor    = UIColor.black.cgColor
        button.layer.shadowOffset   = CGSize(width: 0.0, height: 3.0)
        button.layer.shadowRadius   = 5
        button.layer.shadowOpacity  = 0.5
        button.layer.masksToBounds  = false
        button.addTarget(self, action: selector, for: .touchUpInside)
        return button
    }
}

这会在button 下方创建一个阴影,但是我还有另一个函数可以旋转按钮,向用户发出信号,表明正在从 API 加载数据。

目前我正在做的是创建带有阴影的button,并用没有阴影的button 覆盖它。无阴影的button 将被点击和旋转。

我有一种直觉,必须有更好的方法来做到这一点,而不是创建两个 buttons 有一个吃内存,这样它就可以成为一个占位符(用于阴影效果)。

如何创建一个可以在阴影保持原位时使用animateWith func 旋转的按钮?

这是我的动画代码块。

    func animateRefreshButton() {
        UIView.animate(withDuration: 1.0, delay: 0.0, options: .repeat, animations: {
            self.refreshButton.transform = CGAffineTransform(rotationAngle: .pi)
        })
    }

【问题讨论】:

    标签: ios swift uibutton uikit


    【解决方案1】:

    您可以尝试旋转按钮的 imageView 而不是整个按钮:

    func animateRefreshButton() {
        UIView.animate(withDuration: 1.0, delay: 0.0, options: .repeat, animations: {
            self.refreshButton.imageView?.transform = CGAffineTransform(rotationAngle: .pi)
        })
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-12
      • 2023-04-01
      • 1970-01-01
      • 2021-11-19
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多