UIButton与阴影旋转而不阴影旋转



目前我有一个按钮,可以从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 animateRefreshButton() {
UIView.animate(withDuration: 1.0, delay: 0.0, options: .repeat, animations: {
self.refreshButton.transform = CGAffineTransform(rotationAngle: .pi)
})
}

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

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

最新更新