2017-08-17 22 views
0
图像渲染效果

我创建了一个UIButton如下(有阴影属性没有什么太花哨圆形按钮):查看变换旋转抵消iOS中

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:图像被废止,如下图所示:

InitiallyFinally

有没有解决? (请不要使用代码库)我通过网络搜索,但没有发现类似的问题。

回答

1

您可以将您的按钮类型设置为.custom而不是.system。这应该可以解决你的问题。

+0

谢谢,你节省了很多时间。 –

+0

不客气:) –