gpt4 book ai didi

ios - 使用 swift 删除为 activityIndi​​cator 创建的 subview

转载 作者:可可西里 更新时间:2023-11-01 02:19:29 25 4
gpt4 key购买 nike

我已经创建了一个接受 3 个输入的事件窗口函数,

  • msg:String - 应在事件窗口中显示的消息
  • indicator:Bool - 事件窗口是否应该显示
  • view:UIView - 应该从调用它的 View Controller 获取帧大小等的 uiview

一切正常,除了需要删除 subview 的部分。如果在主视图 Controller 上运行相同的功能。它工作正常。就在我将它移动到 NSObject 时,它没有。请帮忙

class UIDesignFunction: NSObject {

func progressBarDisplayer(msg:String, indicator:Bool, view:UIView) {

var activityIndicator = UIActivityIndicatorView()
var strLabel = UILabel()
var msgFrame = UIView()

if indicator{

//println(msg)
strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
strLabel.text = msg
strLabel.textColor = UIColor.whiteColor()
msgFrame = UIView(frame: CGRect(x: view.frame.midX - 90, y: view.frame.midY - 25 , width: 180, height: 50))
msgFrame.layer.cornerRadius = 15
msgFrame.backgroundColor = UIColor(white: 0, alpha: 0.7)

activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
activityIndicator.startAnimating()
msgFrame.addSubview(activityIndicator)
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
msgFrame.addSubview(strLabel)
view.addSubview(msgFrame)
}else{
UIApplication.sharedApplication().endIgnoringInteractionEvents()
msgFrame.removeFromSuperview()
}

}

最佳答案

如下所示将变量移出函数

class UIDesignFunction: NSObject {

var activityIndicator = UIActivityIndicatorView()
var strLabel = UILabel()
var msgFrame = UIView()

func progressBarDisplayer(msg:String, indicator:Bool, view:UIView) {

if indicator{

//println(msg)
strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
strLabel.text = msg
strLabel.textColor = UIColor.whiteColor()
msgFrame = UIView(frame: CGRect(x: view.frame.midX - 90, y: view.frame.midY - 25 , width: 180, height: 50))
msgFrame.layer.cornerRadius = 15
msgFrame.backgroundColor = UIColor(white: 0, alpha: 0.7)

activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
activityIndicator.startAnimating()
msgFrame.addSubview(activityIndicator)
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
msgFrame.addSubview(strLabel)
view.addSubview(msgFrame)
}else{
UIApplication.sharedApplication().endIgnoringInteractionEvents()
msgFrame.removeFromSuperview()
}

}

关于ios - 使用 swift 删除为 activityIndi​​cator 创建的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31937888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com