В последнее время я спотыкался с Swift и нашел концепцию, которая, как мне кажется, я поняла, но, похоже, не может заставить ее работать нормально.Изменение переменной UIView из ViewController в Swift
В моем приложении у меня есть ViewController с представлением, которое я добавил в IB. Я установил Custom Class моего представления как «RingView», который живет в моем файле RingView.swift (для контекста RingView рисует кольцо).
Это технически нормально работает, так как мое кольцо отображается соответствующим образом при запуске приложения. То, что я пытаюсь сделать, - установить ширину линии кольца и цвет хода программно от моего файла ViewController.swift. В то время как я не получаю никаких ошибок при создании, несмотря на то, что, например, при настройке lineWidth на 10, ширина строки всегда равна 1 при построении.
RingView.swift
class RingView: UIView {
var lineWidth: CGFloat = 1
var strokeColor = UIColor(red: 147.0/255.0, green: 184.0/255.0, blue: 255.0/255.0, alpha: 1.0).CGColor
let circlePathLayer = CAShapeLayer()
let circleRadius: CGFloat = 105.0
override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configure()
}
func configure() {
circlePathLayer.frame = bounds
circlePathLayer.lineWidth = lineWidth
circlePathLayer.fillColor = UIColor.clearColor().CGColor
circlePathLayer.strokeColor = strokeColor
layer.addSublayer(circlePathLayer)
backgroundColor = UIColor.clearColor()
}
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 {
return UIBezierPath(ovalInRect: circleFrame())
}
override func layoutSubviews() {
super.layoutSubviews()
circlePathLayer.frame = bounds
circlePathLayer.path = circlePath().CGPath
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet var titleLabel: UILabel!
@IBOutlet weak var RingViewInner: RingView!
var detailItem: Minion? {
didSet {
// Update the view.
self.configureView()
}
}
func configureView() {
// Update the user interface for the detail item.
if let detail = self.detailItem {
//Set the label for the title name
if let label = titleLabel {
label.text = detail.title!
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
RingViewInner?.strokeColor = UIColor(red: 255.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 0.0).CGColor
RingViewInner?.lineWidth = 10
RingViewInner.setNeedsDisplay()
//Show the configured context of data
self.configureView()
}
}
Любые мысли будут очень высокую оценку. Спасибо!
Возможно, вы захотите переопределить 'drawRect()' в своем пользовательском классе просмотра, чтобы сделать рисунок вашего представления. Не знаю, удастся ли вам решить проблему или нет, но это может помочь. – trevorj