gpt4 book ai didi

iOS 自定义 View 不会立即显示图像

转载 作者:行者123 更新时间:2023-11-30 14:06:33 25 4
gpt4 key购买 nike

我在 Swift 中制作了一个简单的自定义 View ,它有一个背景 ImageView ,前面有一张照片和一些标签。然后,我将自定义 View 添加到 Storyboard中,它显示得很好,我可以看到背景图像和标签。

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

这是我的自定义 View 的代码

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
}

}

然后,我将自定义 View 添加到 Storyboard中,并使用 PerformSegueWithIdentifier("dashboardSegue", sender: nil) 以编程方式导航到场景。我刚刚注意到不仅客户 View ,而且场景中的两个进度条也没有显示。

最佳答案

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

关于iOS 自定义 View 不会立即显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32321968/

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