【问题标题】:View transform rotation nullify image rendering effects in iOSiOS中视图变换旋转使图像渲染效果无效
【发布时间】: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: 提供的图像无效,如下所示:

有解决办法吗? (请不要使用代码库)我在网上搜索过,但没有一个有类似的问题。

【问题讨论】:

    标签: ios swift animation view


    【解决方案1】:

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

    【讨论】:

    • 谢谢,您节省了很多时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    相关资源
    最近更新 更多