I created a UIView in storyboard and i linked to a custom UIView class called "XXX". Where in drawRect i have written code to fill color in XXX. From my view controller i'm trying to animate the UIView. Filling color in XXX(UIView) is happening but no animation. here is my code.
class XXX: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
override func drawRect(rect: CGRect) {
var context: CGContextRef = UIGraphicsGetCurrentContext()!
CGContextClearRect(context, rect)
let color = UIColor.greenColor().CGColor
CGContextSetFillColorWithColor(context, color)
CGContextAddRect(context, rect)
CGContextFillPath(context)
}
myviewcontroller.swift
@IBOutlet var xxxView: XXX!
override func viewDidLoad() {
super.viewDidLoad()
XXX.animateWithDuration(1.0, delay: 0, options:
UIViewAnimationOptions.Repeat, animations: { () -> Void in
XXX.setAnimationDuration(1.0)
self.xxxView = XXX (frame: CGRectMake(0, 0, 100, 100))
},completion: nil);
}
Some where i'm making mistake in my view controller animation. couldn't figure it out. So, How to animate a custom UIView? or How to animate the content of drawrect?
Note: drawRect is been called and rect is filled with color but without animation. My earlier approach was just create a UIView in controller and increase the width with animation. But, it was not accepted by team. some thing like below and works fine.
var xxxView = UIView()
xxxView.frame = CGRectMake(0, 10, startingPoint, 20)
xxxView.backgroundColor = UIColor.greenColor()
UIView.animateWithDuration(1.0, delay: 0, options:
UIViewAnimationOptions.Repeat, animations: { () -> Void in
UIView.setAnimationDuration(3.0)
let totalWidth: CGFloat = 50
let animationWidth: CGFloat = totalWidth - startingPoint
var frame : CGRect = xxxView.frame;
frame.size.width = (startingPoint + animationWidth)
xxxView.frame = frame;
},completion: nil);
Thanks!