【发布时间】:2017-08-17 09:54:52
【问题描述】:
我创建了一个UIButton 如下(带有阴影属性的圆形按钮没什么花哨的):
let button = UIButton(type: .system)
button.backgroundColor = UIColor(hexString: "4267B2")
button.tintColor = .white
button.setImage(#imageLiteral(resourceName: "add").withRenderingMode(.alwaysTemplate), for: .normal)
button.addTarget(self, action: #selector(handleClick), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
button.layer.cornerRadius = 32
button.layer.shadowColor = UIColor.darkGray.cgColor
button.layer.shadowRadius = 6
button.layer.shadowOpacity = 0.6
button.layer.shadowOffset = CGSize.zero
目的是看起来像一个添加按钮,点击它会旋转形成一个十字按钮。
func handleClick() {
button.isSelected = !newPostButton.isSelected
let rotationDirection: CGFloat = button.isSelected ? 1: -1
UIView.animate(withDuration: 0.5) {
self.button.transform = self.button.transform.rotated(by: (rotationDirection * .pi/4))
}
}
我遇到的问题是当动画发生旋转时,旋转效果很好,但为按钮.withRendering: 提供的图像无效,如下所示:
有解决办法吗? (请不要使用代码库)我在网上搜索过,但没有一个有类似的问题。
【问题讨论】: