UIActivityIndicatorView
时间: 2024-04-24 16:26:07 浏览: 22
UIActivityIndicatorView 是一个 iOS 上的控件,用于在界面上显示一个旋转的指示器,表示正在进行某个耗时操作。它通常用于在加载数据或执行任务时向用户提供一个视觉提示。
你可以将 UIActivityIndicatorView 添加到视图层次结构中,并根据需要设置样式、颜色和大小等属性。然后,通过调用 startAnimating() 方法来启动指示器的旋转动画,调用 stopAnimating() 方法来停止动画。
UIActivityIndicatorView 提供了几种不同的样式供你选择,包括白色大、白色小、灰色等。你还可以自定义指示器的颜色和大小。
这是一个使用 UIActivityIndicatorView 的示例代码:
```swift
let activityIndicatorView = UIActivityIndicatorView(style: .large)
activityIndicatorView.color = .gray
activityIndicatorView.center = view.center
view.addSubview(activityIndicatorView)
// 启动动画
activityIndicatorView.startAnimating()
// 执行耗时操作...
// 停止动画
activityIndicatorView.stopAnimating()
```
希望这能回答你的问题!如果还有其他问题,请随时提问。
相关问题
自定义 UIActivityIndicatorView
要自定义 UIActivityIndicatorView,您需要遵循以下步骤:
1. 创建一个新的 UIView,并将其大小设置为您想要的指示器的大小。
2. 在 UIView 中添加一个 CAShapeLayer,并设置其填充颜色和线宽。
3. 使用 CABasicAnimation 创建旋转动画,并将其添加到 CAShapeLayer 上。
4. 将 UIView 添加到您的视图层次结构中,并在需要时启动动画。
下面是一些示例代码,演示如何创建一个自定义的 UIActivityIndicatorView:
```swift
class CustomActivityIndicatorView: UIView {
private let shapeLayer = CAShapeLayer()
override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
configure()
}
private func configure() {
shapeLayer.fillColor = UIColor.white.cgColor
shapeLayer.strokeColor = UIColor.gray.cgColor
shapeLayer.lineWidth = 3.0
shapeLayer.lineCap = .round
shapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: bounds.midX, y: bounds.midY), radius: bounds.width / 2 - shapeLayer.lineWidth / 2, startAngle: 0, endAngle: .pi * 2, clockwise: true).cgPath
layer.addSublayer(shapeLayer)
}
func startAnimating() {
let animation = CABasicAnimation(keyPath: "transform.rotation.z")
animation.fromValue = 0
animation.toValue = CGFloat.pi * 2
animation.duration = 1
animation.repeatCount = .infinity
shapeLayer.add(animation, forKey: "rotation")
}
func stopAnimating() {
shapeLayer.removeAnimation(forKey: "rotation")
}
}
```
在您的视图控制器中,可以像这样使用它:
```swift
let activityIndicator = CustomActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
activityIndicator.center = view.center
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
// 在一段时间后停止动画
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
activityIndicator.stopAnimating()
}
```
ios loading
iOS loading通常采用活动指示器(UIActivityIndicatorView)来展示。可以通过以下步骤来添加一个活动指示器:
1. 在需要展示loading的页面上,添加一个UIView作为容器,设置其背景颜色和透明度,以便让loading更加明显。
2. 在该UIView中添加一个UIActivityIndicatorView。
3. 在需要展示loading时,将UIView和UIActivityIndicatorView设置为可见,停止时隐藏即可。
示例代码:
```swift
// 创建UIView作为容器
let containerView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
containerView.backgroundColor = UIColor.black.withAlphaComponent(0.5) // 设置背景颜色和透明度
view.addSubview(containerView)
// 创建UIActivityIndicatorView并添加到容器中
let activityIndicator = UIActivityIndicatorView(style: .whiteLarge)
activityIndicator.center = containerView.center
containerView.addSubview(activityIndicator)
// 启动loading
activityIndicator.startAnimating()
containerView.isHidden = false
// 停止loading
activityIndicator.stopAnimating()
containerView.isHidden = true
```
另外,也可以使用第三方库来实现loading效果,比如MBProgressHUD、SVProgressHUD等。这些库会提供更加丰富的loading样式和动画效果。
相关推荐
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)