使用swift

时间:2015-08-11 09:22:01

标签: ios swift uiview

我创建了一个活动窗口函数,它有3个输入,

  • msg:String - 应在活动窗口中显示的消息
  • 指标:Bool - 如果活动窗口应显示
  • view:UIView - 应该从View Controller中获取帧大小等的uiview

一切正常,除了需要删除子视图的部分。如果在主视图控制器上运行相同的功能。它工作正常。就在我把它移到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()
    }

}

1 个答案:

答案 0 :(得分:1)

将变量移出函数,如下所示

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()
}

}