我创建了一个类,它绘制一个条形图,当设置一个条形图时,该条形图指示一个百分比值。如何设置条的动画,使其从左向右增长?
动画功能,我已经没有工作,因为它希望它。。。
class EasyLineView : UIView {
var fillRect = CGRect.init(x: 0, y: 0, width: 0, height: 0)
var percentValue: CGFloat = 0 {
didSet {
self.layoutIfNeeded()
}
}
var orientation: BarOrientation = .vertical
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.black
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func draw(_ rect: CGRect) {
print("drawing with \(rect.size.width)")
let r = self.bounds // the view's bounds
let ctx = UIGraphicsGetCurrentContext() // get the current context
ctx!.setFillColor(UIColor.yellow.cgColor) // set fill color to the given color if it's provided, else use clearColor
if (self.orientation == .vertical){
fillRect = CGRect.init(x: 0, y: 0, width: r.size.width, height: percentValue*r.size.height)
} else {
fillRect = CGRect.init(x: 0, y: 0, width: percentValue*r.size.width, height: r.size.height)
}
ctx!.fill(fillRect)
}
// does not work
func animate(){
UIView.animate(withDuration: 5, delay: 0, animations: {
self.layoutIfNeeded()
})
}
}