在 swift 中 iPhone 应用程序的 activity 指标中带有旋转的自定义图像

Custom image with rotation in activity indicator for iPhone application in swift

我希望我的公司徽标在我的 iPhone 应用程序中作为 activity 指示器旋转。我正在研究 swift。任何人都可以向我推荐 SVProgressHUD 以外的第三方库,因为它不提供任何自定义图像旋转。

如果有人可以建议自定义代码,我们将不胜感激。

您可以尝试这样的操作:

final class MyActivityIndicator: UIImageView {

    override func startAnimating() {
        isHidden = false
        rotate()
    }

    override func stopAnimating() {
        isHidden = true
        removeRotation()
    }

    private func rotate() {
        let rotation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotation.toValue = NSNumber(value: Double.pi * 2)
        rotation.duration = 1
        rotation.isCumulative = true
        rotation.repeatCount = Float.greatestFiniteMagnitude
        layer.add(rotation, forKey: "rotationAnimation")
    }

    private func removeRotation() {
        layer.removeAnimation(forKey: "rotationAnimation")
    }
}

用法:

let activityIndicator = MyActivityIndicator(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
activityIndicator.image = UIImage(named: "TOU IMAGE")
view.addSubview(activityIndicator)
activityIndicator.startAnimating()