如何以编程方式在iphone应用程序中添加简单的默认加载(进度)栏
我在iPhone应用程序中使用http通信。我想在从服务器加载数据时显示进度条。我如何以编程方式完成它 我只需要一个默认的进度条。没有什么特别的,就像在android中我们做的如何以编程方式在iphone应用程序中添加简单的默认加载(进度)栏,iphone,ios,Iphone,Ios,我在iPhone应用程序中使用http通信。我想在从服务器加载数据时显示进度条。我如何以编程方式完成它 我只需要一个默认的进度条。没有什么特别的,就像在android中我们做的ProgressDialog.show(),在iphone中显示进度条是否有一行代码?请尝试下面的代码 float progress; //components UIProgressView *progressBar; progressBar=[[UIProgressView alloc]initWithProgress
ProgressDialog.show()代码>,在iphone中显示进度条是否有一行代码?请尝试下面的代码
float progress;
//components
UIProgressView *progressBar;
progressBar=[[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
[progressBar setFrame:CGRectMake(30.0, 75.0, 200.0, 80.0)];
int prog=progress*100;
progressStr=[NSString stringWithFormat:@"%d%%",prog];
[progressBar setProgress:progress];
我建议使用NSURLConnection
。您需要的方法有:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.resourceData setLength:0];
self.filesize = [NSNumber numberWithLongLong:[response expectedContentLength]];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.resourceData appendData:data];
NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.resourceData length]];
self.progressBar.progress = [resourceLength floatValue] / [self.filesize floatValue];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
self.progressBar.hidden = YES;
}
和头文件:
@property (nonatomic, retain) UIProgressView *progressBar;
@property (nonatomic, retain) NSMutableData *resourceData;
@property (nonatomic, retain) NSNumber *filesize;
希望对您有所帮助UIProgressView
是您正在寻找的课程:
您需要使用setProgress:animated:
方法来更新显示的进度。最有可能的情况是处理从网络接收的数据
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
indicator.center = self.view.center;
[self.view addSubview:indicator];
[indicator bringSubviewToFront:self.view];
[UIApplication sharedApplication].networkActivityIndicatorVisible = TRUE;
当您想显示指示器时,请编写以下代码
[indicator startAnimating];
[indicator stopAnimating];
要隐藏指示器时,请编写以下代码
[indicator startAnimating];
[indicator stopAnimating];
您可以在内置UIProgressView中使用IOS。下面是代码片段:
UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
[self.view addSubview:progressView];
[progressView setProgress:50.0];
您可以使用setFrame:在视图上定位进度条。试试看
它非常简单,有几个进度动画选项,并且能够添加一些定制。它全屏显示。而且它应该适用于任何最新的iOS版本
或者,如果你想要更别致的东西,也可以试试:)它看起来有点像OSX进度条。将@Hiren的答案翻译成Swift
var indicator: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
indicator.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
indicator.center = view.center
self.view.addSubview(indicator)
self.view.bringSubview(toFront: indicator)
UIApplication.shared.isNetworkActivityIndicatorVisible = true
显示指示器
indicator.startAnimating()
indicator.stopAnimating()
停止指示器
indicator.startAnimating()
indicator.stopAnimating()
我也会把我的两分钱放在这里:
我已经创建了这个预加载程序,您也可以在这里进行测试。我知道这是一个较老的问题,但我已经根据上述答案编写了一个全局静态方法,用于从任何角度执行这类操作
主要更新内容包括:
- 支持活动指示器后面的可选不透明覆盖
- 使用活动指示器的默认帧大小
- 使用。白色大指示器样式
在我的AppHelper中。swift:
static func showActivityIndicator(view: UIView, withOpaqueOverlay: Bool) {
// this will be the alignment view for the activity indicator
var superView: UIView = view
// if we want an opaque overlay, do that work first then put the activity indicator within that view; else just use the passed UIView to center it
if withOpaqueOverlay {
let overlay = UIView()
overlay.frame = CGRectMake(0.0, 0.0, view.frame.width, view.frame.height)
overlay.layer.backgroundColor = UIColor.blackColor().CGColor
overlay.alpha = 0.7
overlay.tag = activityIndicatorOverlayViewTag
overlay.center = superView.center
overlay.hidden = false
superView.addSubview(overlay)
superView.bringSubviewToFront(overlay)
// now we'll work on adding the indicator to the overlay (now superView)
superView = overlay
}
let indicator: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)
indicator.center = superView.center
indicator.tag = activityIndicatorViewTag
indicator.hidden = false
superView.addSubview(indicator)
superView.bringSubviewToFront(indicator)
indicator.startAnimating()
// also indicate network activity in the status bar
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
static func hideActivityIndicator(view: UIView) {
// stop the network activity animation in the status bar
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
// remove the activity indicator and optional overlay views
view.viewWithTag(activityIndicatorViewTag)?.removeFromSuperview()
view.viewWithTag(activityIndicatorOverlayViewTag)?.removeFromSuperview()
}
添加属性
@属性(强、非原子)IBUIActivityIndicatorView*指示器
开始设置动画
[自指示器启动激活]
停止设置动画
[自指示停止动画制作] 应用程序委派.h
-(void)showLoader;
-(void)hideLoder;
应用程序代理.m
@implementation AppDelegate
AppDelegate *app;
-(void)showLoader
{
if(loaderView== NULL)
{
loaderView=[[UIView alloc] initWithFrame:self.window.frame];
[loaderView setBackgroundColor:[UIColor blackColor]];
[loaderView setAlpha:0.5];
spinner = [[UIActivityIndicatorView alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[loaderView addSubview:spinner];
spinner.center = CGPointMake(loaderView.frame.size.width/2-10,
loaderView.frame.size.height/2-10);
spinner.hidesWhenStopped = YES;
}
[spinner startAnimating];
[self.window addSubview:loaderView];
[self.window bringSubviewToFront:spinner];
[self.window bringSubviewToFront:loaderView];
}
-(void)hideLoder
{
if (spinner!= NULL) {
[spinner stopAnimating];
}
[loaderView removeFromSuperview];
}
现在将“AppDelegate.h”类导入到任何您想要调用loader.h的地方,您可以像这样调用loader.h类
[应用程序showLoader]
及
[应用程序hideLoder] 为了保持这个问题的完全更新,我将@enrique7mc的答案翻译成了Swift3.0
,这是他从@Hiren的答案翻译过来的
var indicator: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
indicator.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0)
indicator.center = view.center
view.addSubview(indicator)
indicator.bringSubview(toFront: view)
UIApplication.shared.isNetworkActivityIndicatorVisible = true
启动和停止进度条的方式与@enrique7mc指出的相同
indicator.startAnimating()
indicator.stopAnimating()
我想发布一个我创建的解决方案,它将在一个单独的窗口中显示一个进度条,以显示可以列出的结果的进度
附件是下面的图片,它看起来像什么
兼容性:Swift 3
功能:线程编码运行&取消+完成按钮。如果需要,使用updateProgress函数自动显示剩余的进度量
代码非常简单,易于修改,只需一个故事板和一个viewcontroller
使用下面的XML,并将我建议的最终名称保存在项目中的某个地方
ProgressWindow.storyboard
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11542" systemVersion="16B2555" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11524"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Progress-->
<scene sceneID="5gE-ws-FsC">
<objects>
<viewController storyboardIdentifier="progressWindow" title="Progress" id="IB9-Dc-dCV" customClass="ProgressWindowViewController" customModule="WorkOrders" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Lvc-9P-nmJ"/>
<viewControllerLayoutGuide type="bottom" id="xJS-yG-jWM"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="iDk-68-mde">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Please Wait..." textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="9ui-W7-ucD">
<rect key="frame" x="16" y="84" width="343" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<progressView opaque="NO" contentMode="scaleToFill" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="ov7-yH-A5z">
<rect key="frame" x="16" y="113" width="343" height="2"/>
</progressView>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" editable="NO" text="Starting Up..." textAlignment="natural" selectable="NO" translatesAutoresizingMaskIntoConstraints="NO" id="bwA-YT-FcE">
<rect key="frame" x="16" y="123" width="343" height="464"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
<navigationBar contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="RPs-Mo-Cfx">
<rect key="frame" x="-4" y="20" width="383" height="44"/>
<items>
<navigationItem title="Starting Sync..." id="Y87-LY-5o5">
<barButtonItem key="rightBarButtonItem" title="Cancel" id="AD3-in-E6j">
<connections>
<action selector="cancelNavItemButtonActionWithSender:" destination="IB9-Dc-dCV" id="IF1-MG-v2x"/>
</connections>
</barButtonItem>
</navigationItem>
</items>
</navigationBar>
<button opaque="NO" contentMode="scaleToFill" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="6lh-KK-lX1">
<rect key="frame" x="113" y="595" width="150" height="42"/>
<fontDescription key="fontDescription" type="system" pointSize="25"/>
<state key="normal" title="Done"/>
<state key="disabled" title="Please Wait..."/>
<connections>
<action selector="doneButtonActionWithSender:" destination="IB9-Dc-dCV" eventType="touchUpInside" id="KQH-Th-NAC"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="leadingMargin" secondItem="ov7-yH-A5z" secondAttribute="leading" id="9Gm-bd-GY6"/>
<constraint firstItem="xJS-yG-jWM" firstAttribute="top" secondItem="bwA-YT-FcE" secondAttribute="bottom" constant="20" id="CT1-0k-Skt"/>
<constraint firstItem="6lh-KK-lX1" firstAttribute="top" secondItem="bwA-YT-FcE" secondAttribute="bottom" constant="8" symbolic="YES" id="Fb8-eP-lxu"/>
<constraint firstItem="RPs-Mo-Cfx" firstAttribute="leading" secondItem="iDk-68-mde" secondAttribute="leadingMargin" constant="-20" id="JSY-Na-oAF"/>
<constraint firstItem="xJS-yG-jWM" firstAttribute="top" secondItem="6lh-KK-lX1" secondAttribute="bottom" constant="30" id="NHY-fO-W26"/>
<constraint firstAttribute="trailingMargin" secondItem="RPs-Mo-Cfx" secondAttribute="trailing" constant="-20" id="QxH-pj-oOA"/>
<constraint firstItem="RPs-Mo-Cfx" firstAttribute="top" secondItem="Lvc-9P-nmJ" secondAttribute="bottom" id="VIf-63-vaw"/>
<constraint firstAttribute="trailingMargin" secondItem="bwA-YT-FcE" secondAttribute="trailing" id="WxH-hu-ZVQ"/>
<constraint firstAttribute="leadingMargin" secondItem="bwA-YT-FcE" secondAttribute="leading" id="XEd-Ba-ZfL"/>
<constraint firstItem="bwA-YT-FcE" firstAttribute="top" secondItem="ov7-yH-A5z" secondAttribute="bottom" constant="8" id="Xjr-bH-ILB"/>
<constraint firstItem="6lh-KK-lX1" firstAttribute="centerY" secondItem="iDk-68-mde" secondAttribute="centerY" id="ZU1-pD-czP"/>
<constraint firstItem="ov7-yH-A5z" firstAttribute="top" secondItem="9ui-W7-ucD" secondAttribute="bottom" constant="8" symbolic="YES" id="avI-Ab-G29"/>
<constraint firstAttribute="leadingMargin" secondItem="9ui-W7-ucD" secondAttribute="leading" id="dse-zV-g00"/>
<constraint firstItem="6lh-KK-lX1" firstAttribute="centerX" secondItem="iDk-68-mde" secondAttribute="centerX" id="i5Q-oY-DdV"/>
<constraint firstAttribute="trailingMargin" secondItem="9ui-W7-ucD" secondAttribute="trailing" id="jqu-1f-IuA"/>
<constraint firstItem="9ui-W7-ucD" firstAttribute="top" secondItem="RPs-Mo-Cfx" secondAttribute="bottom" constant="20" id="nrH-ey-Zcm"/>
<constraint firstAttribute="trailingMargin" secondItem="ov7-yH-A5z" secondAttribute="trailing" id="qha-Es-6Au"/>
</constraints>
<variation key="default">
<mask key="constraints">
<exclude reference="ZU1-pD-czP"/>
<exclude reference="CT1-0k-Skt"/>
</mask>
</variation>
</view>
<connections>
<outlet property="cancelNavButton" destination="AD3-in-E6j" id="SJc-Bc-N6j"/>
<outlet property="currentProgressLabel" destination="9ui-W7-ucD" id="zij-yQ-MFX"/>
<outlet property="doneButton" destination="6lh-KK-lX1" id="rh2-RF-4ak"/>
<outlet property="navItemLabel" destination="Y87-LY-5o5" id="ijO-a7-TrD"/>
<outlet property="navigationBar" destination="RPs-Mo-Cfx" id="WEq-F4-Pup"/>
<outlet property="theProgressBar" destination="ov7-yH-A5z" id="FUE-9J-iBh"/>
<outlet property="theTextView" destination="bwA-YT-FcE" id="1sR-23-NZH"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="TH6-NB-Eos" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-492" y="1474"/>
</scene>
</scenes>
</document>
3.)要使用,只需将调用它的视图控制器设置为progressWindowDelegate,并提供与popover通信所需的信息
下面的一些示例代码(如果需要):
带YesButton.swift的采样视图控制器
import UIKit
protocol progressWindowDelegate : class{
var titleToGive : String {get}
func codeToRun(progressWindowViewController:ProgressWindowViewController)
var codeToCancel : ()->() {get}
}
class ProgressWindowViewController: UIViewController {
@IBOutlet weak var theTextView: UITextView!
@IBOutlet weak var currentProgressLabel: UILabel!
@IBOutlet weak var theProgressBar: UIProgressView!
@IBOutlet weak var navItemLabel: UINavigationItem!
@IBOutlet weak var doneButton: UIButton!
@IBOutlet weak var cancelNavButton: UIBarButtonItem!
@IBOutlet weak var navigationBar: UINavigationBar!
//For showing network activity
var indicator: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
//Sets delegate
weak var controllerDelegate:progressWindowDelegate? = nil
override func viewDidLoad() {
super.viewDidLoad()
navItemLabel.title = controllerDelegate!.titleToGive
//Run on the main thread first then in background thread.
DispatchQueue.main.async {
DispatchQueue.global(qos: .background).async{
self.controllerDelegate?.codeToRun(progressWindowViewController: self)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func writeToProgressWindow(text:NSMutableAttributedString){
DispatchQueue.main.async(execute: {
print("dispatch_queue_get_main_queue -> writeToProgressWindow()")
self.theTextView.attributedText = text
})
}
func updateNetworkIndicator(active:Bool){
DispatchQueue.main.async(execute: {
if(active){
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}else{
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
})
}
func updateProgress(updatetext:String,amount:Int,left:Int){
DispatchQueue.main.async(execute: {
print("dispatch_queue_get_main_queue -> updatingProgress()")
self.currentProgressLabel.text = updatetext+" : \(amount) / \(left)"
self.theProgressBar.setProgress(Float(amount/left), animated: true) //progress is represented as a percentage of the total
})
}
func updateProgressWindowWeFinished(title:String){
//Enable done button and Disable/Hide Cancel Button. Add Final Messages
DispatchQueue.main.async(execute: {
self.doneButton.isEnabled = true
self.navItemLabel.title = title
self.cancelNavButton.isEnabled = false
self.cancelNavButton.tintColor = UIColor.clear
})
}
@IBAction func cancelNavItemButtonAction(sender: UIBarButtonItem) {
//Run on the main thread first then in background thread.
DispatchQueue.main.async {
DispatchQueue.global(qos: .background).sync{
print("dispatch_queue_priority_default")
self.controllerDelegate?.codeToCancel()
}
self.dismiss(animated: true, completion: nil)//closes the window.
}
}
@IBAction func doneButtonAction(sender: UIButton) {
self.dismiss(animated: true, completion: nil)//closes the window.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
class SyncViewController: UIViewController, progressWindowDelegate {
var codeToCancel = {print("code to cancel")}
var titleToGive = "Starting Sync..."
func codeToRun(progressWindowViewController:ProgressWindowViewController) {
print("code to run")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func yesButtonAction(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "ProgressWindow", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "progressWindow") as! ProgressWindowViewController
controller.controllerDelegate = self
self.present(controller, animated: true, completion: nil)
}
@IBAction func noButtonAction(_ sender: UIButton) {
tabBarController?.selectedIndex = 1 //Send them to the list then.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
您在codeToRun()中放入的任何内容都将作为后台线程运行。要访问任何UI,codeToRun都可以访问progressWindowViewController,该控制器已预配置为连接到UI,由您控制这些元素
@IBOutlet weak var theTextView: UITextView!
@IBOutlet weak var currentProgressLabel: UILabel!
@IBOutlet weak var theProgressBar: UIProgressView!
@IBOutlet weak var navItemLabel: UINavigationItem!
@IBOutlet weak var doneButton: UIButton!
@IBOutlet weak var cancelNavButton: UIBarButtonItem!
@IBOutlet weak var navigationBar: UINavigationBar!
每当我在做同步、上传或耗时的网络任务时,我都会在我的所有项目中使用它
这也显示了活动指示器,以表明您正在尝试做某事。所有这些都可以定制,因为它很容易查看和理解它是如何工作的。希望这能帮助你们中的一些人节省一些时间,快速打开一个进度类型窗口 Swift 3版@enrique7mc解决方案
var indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
indicator.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
indicator.center = view.center
view.addSubview(indicator)
indicator.bringSubview(toFront: view)
UIApplication.shared.isNetworkActivityIndicatorVisible = true
indicator.startAnimating()
indicator.stopAnimating()
@海伦的答案翻译成Swift4.0
let indicator: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorView.Style.gray)
indicator.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0)
indicator.center = view.center
view.addSubview(indicator)
indicator.bringSubviewToFront(view)
UIApplication.shared.isNetworkActivityIndicatorVisible = true
indicator.startAnimating()
indicator.stopAnimating()
Swift 5.x版本@enrique7mc的Swift代码:
var indicator: UIActivityIndicatorView = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.gray)
indicator.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0)
indicator.center = view.center
view.addSubview(indicator)
indicator.bringSubviewToFront(view)
UIApplication.shared.isNetworkActivityIndicatorVisible = true
从
indicator.startAnimating()
停下来
indicator.stopAnimating()
这会在屏幕中央和状态栏中添加一个小的活动指示器(旋转圆圈,苹果的)。切换到不同的ViewController
后,第一个将被清除,而状态栏中的一个不会清除-您必须手动禁用它。SwiftUI版本
非常粗糙和简单,不是网络化的基本进度指标。在SwiftUI中默认情况下仍然不支持,UIViewRepresentable
。喜欢根据你的心意修改它
struct ActivityIndicatorView: UIViewRepresentable {
let large: Bool
@State var enabled: Bool
func makeUIView(context: Context) -> UIActivityIndicatorView {
let view = UIActivityIndicatorView(style: large ? .large : .medium)
return view
}
func updateUIView(_ view: UIActivityIndicatorView, context: Context) {
if enabled {
view.startAnimating()
} else {
view.stopAnimating()
}
}
}
Swift 5.x版本
导入UIKit
class SKLoader: NSObject {
static let sharedInstance = SKLoader()
let indicator: UIActivityIndicatorView? = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.medium)
let screen = UIScreen.main.bounds
var appDelegate: SceneDelegate {
guard let sceneDelegate = UIApplication.shared.connectedScenes
.first!.delegate as? SceneDelegate else {
fatalError("sceneDelegate is not UIApplication.shared.delegate")
}
return sceneDelegate
}
var rootController:UIViewController? {
guard let viewController = appDelegate.window?.rootViewController else {
fatalError("There is no root controller")
}
return viewController
}
func show() {
indicator?.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0)
indicator?.frame.origin.x = (screen.width/2 - 20)
indicator?.frame.origin.y = (screen.height/2 - 20)
rootController?.view.addSubview(indicator!)
indicator?.startAnimating()
}
func hide() {
DispatchQueue.main.async {
self.indicator?.stopAnimating()
self.indicator?.removeFromSuperview()
}
}
}
示例用法:
//显示
SKLoader.sharedInstance.show()
//隐藏
SKLoader.sharedInstance.hide()
=======阿奎尔先生尝试发布代码。您只需将progressbar子视图添加到您的视图中即可。在swift中尝试此用于iOS的HUD库。我必须将代码从同步http请求更改为异步http请求,但我想,我应该已经完成了。感谢帮助人们看到这个伟大的答案,不要忘记在第一行的开头添加UIActivityindicator视图,而不是UIActivityindicator视图,也不要忘记停止网络活动指示器[UIApplication sharedApplication]。NetworkActivityIndicator visible=FALSE代码>我还必须添加[indicator setHidden:NO]
您不需要设置网络活动指标
,它用于显示指标i