Swift开发iPhone应用:求可自定义旋转Logo的活动指示器方案(非SVProgressHUD)
用公司Logo实现自定义旋转活动指示器的方案(Swift)
一、推荐的第三方库
除了SVProgressHUD,这两个库完美支持自定义旋转Logo的需求:
MBProgressHUD:这是iOS开发中非常流行的 HUD 库,灵活性极高,可以轻松替换默认指示器为你的公司Logo,并添加旋转动画。
使用示例:import MBProgressHUD // 显示自定义旋转Logo func showCustomLogoHUD() { let hud = MBProgressHUD.showAdded(to: view, animated: true) hud.mode = .customView // 创建Logo图片视图 let logoImageView = UIImageView(image: UIImage(named: "company-logo")) hud.customView = logoImageView // 添加旋转动画 let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z") rotationAnimation.toValue = NSNumber(value: Double.pi * 2) rotationAnimation.duration = 1.0 rotationAnimation.isCumulative = true rotationAnimation.repeatCount = Float.infinity logoImageView.layer.add(rotationAnimation, forKey: "rotationAnimation") hud.label.text = "加载中..." } // 隐藏HUD func hideCustomLogoHUD() { MBProgressHUD.hide(for: view, animated: true) }PKHUD:同样是轻量且高度可定制的HUD库,支持自定义内容视图,实现Logo旋转也很简单。
使用示例:import PKHUD // 显示自定义旋转Logo func showCustomLogoPKHUD() { let logoImageView = UIImageView(image: UIImage(named: "company-logo")) // 添加旋转动画 let rotation = CABasicAnimation(keyPath: "transform.rotation") rotation.fromValue = 0 rotation.toValue = 2 * Double.pi rotation.duration = 1.0 rotation.repeatCount = .infinity logoImageView.layer.add(rotation, forKey: "spin") HUD.show(.customView(view: logoImageView)) } // 隐藏HUD func hideCustomLogoPKHUD() { HUD.hide() }
二、纯自定义实现(无需第三方库)
如果不想依赖第三方库,自己实现一个也很简单,下面是完整的自定义旋转指示器代码:
import UIKit class CustomSpinnerView: UIView { private let logoImageView: UIImageView = { let imageView = UIImageView() imageView.image = UIImage(named: "company-logo") imageView.contentMode = .scaleAspectFit imageView.translatesAutoresizingMaskIntoConstraints = false return imageView }() override init(frame: CGRect) { super.init(frame: frame) setupView() } required init?(coder: NSCoder) { super.init(coder: coder) setupView() } private func setupView() { backgroundColor = UIColor.black.withAlphaComponent(0.5) layer.cornerRadius = 10 clipsToBounds = true addSubview(logoImageView) NSLayoutConstraint.activate([ logoImageView.centerXAnchor.constraint(equalTo: centerXAnchor), logoImageView.centerYAnchor.constraint(equalTo: centerYAnchor), logoImageView.widthAnchor.constraint(equalToConstant: 60), logoImageView.heightAnchor.constraint(equalToConstant: 60) ]) } // 开始旋转动画 func startAnimating() { let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z") rotationAnimation.toValue = NSNumber(value: Double.pi * 2) rotationAnimation.duration = 1.0 rotationAnimation.isCumulative = true rotationAnimation.repeatCount = Float.infinity logoImageView.layer.add(rotationAnimation, forKey: "rotationAnimation") } // 停止旋转动画 func stopAnimating() { logoImageView.layer.removeAnimation(forKey: "rotationAnimation") } } // 在ViewController中调用的示例 extension UIViewController { func showCustomSpinner() { let spinner = CustomSpinnerView(frame: view.bounds) spinner.tag = 999 view.addSubview(spinner) spinner.startAnimating() } func hideCustomSpinner() { if let spinner = view.viewWithTag(999) as? CustomSpinnerView { spinner.stopAnimating() spinner.removeFromSuperview() } } }
使用的时候,直接在你的ViewController里调用:
// 显示指示器 showCustomSpinner() // 模拟异步任务后隐藏 DispatchQueue.main.asyncAfter(deadline: .now() + 3) { self.hideCustomSpinner() }
这样不管是用第三方库还是自己实现,都能完美实现用公司Logo作为旋转活动指示器的需求啦!
内容的提问来源于stack exchange,提问作者Er. Vihar
相关产品推荐
相关产品推荐

