2015-09-01 44 views
0

我在Swift中做了一个简单的自定义视图,其中有一张带有照片和一些标签的背景图像视图。然后,我将自定义视图添加到故事板,并且显示效果良好,可以看到背景图片和标签。iOS自定义视图不立即显示图像

但是,当我在我的设备上运行我的应用程序时,图像视图不会立即显示,如果我导航到另一个视图,然后返回,它将显示。我只有一个计时器,它在我的场景中安排了一些后台任务。我在这里错过了什么吗?

这里是我的自定义视图的代码

import UIKit 

@IBDesignable class GaugeView: UIImageView { 

    @IBOutlet weak var speedLabel: UILabel! 

    let circlePathLayer = CAShapeLayer() 
    var circleRadius: CGFloat = 50.0 

    var speed: CGFloat { 
     get { 
      return circlePathLayer.strokeEnd 
     } 
     set { 
      speedLabel.text = String(format: "%.2f", newValue) 

      if newValue < 20.0 { 
       circlePathLayer.strokeColor = UIColor.blueColor().CGColor 
      } else if newValue >= 25.0 { 
       circlePathLayer.strokeColor = UIColor.redColor().CGColor 
      } else { 
       circlePathLayer.strokeColor = UIColor.yellowColor().CGColor 
      } 

      if (newValue > 50) { 
       circlePathLayer.strokeEnd = 1 
      } else if (newValue < 0) { 
       circlePathLayer.strokeEnd = 0 
      } else { 
       circlePathLayer.strokeEnd = newValue/49.9 
      } 
     } 
    } 

    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 

     xibSetup() 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 

     xibSetup() 
    } 

    override func layoutSubviews() { 
     super.layoutSubviews() 

     circleRadius = bounds.width/2 - 2.6 
     circlePathLayer.frame = bounds 
     circlePathLayer.path = circlePath().CGPath 
    } 

    // Our custom view from the XIB file 
    var view: UIView! 
    let nibName = "GaugeView" 

    func xibSetup() { 
     view = loadViewFromNib() 
     // use bounds not frame or it'll be offset 
     view.frame = bounds 
     // Make the view stretch with containing view 
     view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight 
     // Adding custom subview on top of our view (over any custom drawing > see note below) 
     addSubview(view) 
     // Configure speed circle layer 
     configure() 
    } 

    func configure() { 
     speed = 0.0 
     circlePathLayer.frame = bounds 
     circlePathLayer.lineWidth = 6 
     circlePathLayer.fillColor = UIColor.clearColor().CGColor 
     circlePathLayer.strokeColor = UIColor.blueColor().CGColor 
     layer.addSublayer(circlePathLayer) 
     backgroundColor = UIColor.whiteColor() 
    } 

    func circleFrame() -> CGRect { 
     var circleFrame = CGRect(x: 0, y: 0, width: 2*circleRadius, height: 2*circleRadius) 
     circleFrame.origin.x = CGRectGetMidX(circlePathLayer.bounds) - CGRectGetMidX(circleFrame) 
     circleFrame.origin.y = CGRectGetMidY(circlePathLayer.bounds) - CGRectGetMidY(circleFrame) 
     return circleFrame 
    } 

    func circlePath() -> UIBezierPath { 
     let center: CGPoint = CGPointMake(CGRectGetMidX(circlePathLayer.bounds), CGRectGetMidY(circlePathLayer.bounds)) 
     let start = CGFloat(1.33 * M_PI_2) 
     let end = CGFloat(1.64 * M_2_PI) 
     return UIBezierPath(arcCenter: center, radius: circleRadius, startAngle: start, endAngle: end, clockwise: true) 
    } 

    func loadViewFromNib() -> UIView { 
     let bundle = NSBundle(forClass: self.dynamicType) 
     let nib = UINib(nibName: nibName, bundle: bundle) 
     let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView 

     return view 
    } 

} 

然后,我添加了自定义视图我的故事板并导航到现场编程与performSegueWithIdentifier(“dashboardSegue”,发件人:无)。我只注意到不仅客户视图,而且现场的两个进度条也不显示。

+1

我们可以看到一些代码吗? – rocky

+0

不知道为什么,但我删除了scence导航代码performSegueWithIdentifier(“dashboardSegue”,发件人:无),使用segue直接显示视图和我的第二个视图显示完全没有延迟。 –

回答

0

最好显示你的代码,但我猜你的计时器卡在主UI线程。当你导航到另一个视图时,你的timer.invalidate()被调用。尝试删除NSTimer以查看它是否正常工作。

+0

这可能是原因,我会尝试在新线程中启动计时器,看看会发生什么。如果这不起作用,我会回来更多的细节。谢谢。 –

+0

我将计时器添加到runloop,但不起作用。我注释掉了viewDidLoad和viewWillApper中的所有代码,但仍然无效。如果我将场景设置为初始视图,那么它显示得非常快。任何关于差异的想法? –

+0

如果是这样,我想你的观点可能不会从故事板被引用。你可以尝试以编程方式添加你的视图吗?或者,如果你可以在github上分享你的代码。 –