0
我正在嘗試在程序中在使用Open Source software
創建的循環加載圖像中添加一個label
。我面臨的問題是我不知道如何標籤,使其包含在圓圈內。我試圖將CAShapeLayer
與UILabel
進行比較,但這不會起作用,因爲CAShapeLayer
不是可從Storyboard
獲得的類型。有沒有我應該遵循的建議修復?以編程方式爲UILabel添加一個圓形加載動畫的約束
let circlePathLayer = CAShapeLayer()
var circleRadius: CGFloat = 100.0
var circleCenter: CGPoint = CGPoint()
func displayStatus() {
let statusLabel = UILabel(frame: CGRectMake(0, 0, 200, 21))
statusLabel.center = CGPointMake(160, 284)
statusLabel.textAlignment = NSTextAlignment.Center
statusLabel.textColor = UIColor.whiteColor()
statusLabel.text = "(Up)loading"
let horizontalConstraint = NSLayoutConstraint(item: statusLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: circlePathLayer, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0) //ERROR
self.addConstraint(horizontalConstraint)
let verticalConstraint = NSLayoutConstraint(item: statusLabel, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: circlePathLayer, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0) //ERROR
self.addConstraint(verticalConstraint)
self.addSubview(statusLabel)
}
做喲你有這個Swift版本嗎? –
用Swift Code更新。 –