iPhone应用程序的活动指示器中带有旋转功能的自定义图像

Er.*_*har 0 uiactivityindicatorview ios swift

我希望我的公司徽标可以显示在活动指示器中,并且可以在我的iPhone应用程序中旋转。我正在努力。谁能向我建议一些除SVProgressHUD之外的第三方库,因为这没有提供任何自定义图像旋转。

如果有人可以建议一个自定义代码,那么也将不胜感激。

Naz*_*vyi 5

您可以尝试如下操作:

class MyIndicator: UIView {

  let imageView = UIImageView()

    init(frame: CGRect, image: UIImage) {
        super.init(frame: frame)

        imageView.frame = bounds
        imageView.image = image
        imageView.contentMode = .scaleAspectFit
        imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        addSubview(imageView)
    }

    required init(coder: NSCoder) {
        fatalError()
    }

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

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

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

    private func removeRotation() {
         self.imageView.layer.removeAnimation(forKey: "rotationAnimation")
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

let ind = MyIndicator(frame: CGRect(x: 0, y: 0, width: 100, height: 100), image: UIImage(named: "YOUR IMAGE NAME HERE"))
view.addSubview(ind)
ind.startAnimating()
Run Code Online (Sandbox Code Playgroud)