Create a simple app, and can't do it without an error
I don't really know why bg.drawInRect doesn't cause an error when player.drawInRect causing? And the problem is that image doesn't drawing/redrawing when we change coordinates
Sep 16 22:13:16 imac-admin.lan Letalka_drawrect[804] : CGContextRestoreGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
You need to start your timer outside your drawRect
call; didMoveToSuperview
might be a good place, but it depends on what you're trying to accomplish. Then, in timerRedraw
, call setNeedsDisplay()
to trigger a re-draw.
var timer: NSTimer?
override func didMoveToSuperview() {
// Stop the timer if it was already running
if timer != nil {
timer.invalidate()
timer = nil
}
// Only start the timer if the view is actually part of a view hierarchy
if superview != nil {
timer = NSTimer.scheduledTimerWithTimeInterval(interval, target: self, selector: "timerRedraw", userInfo: nil, repeats: true)
}
}
func timerRedraw() {
x += 5
y += 6
setNeedsDisplay()
}
I highly recommend reading Apple's Drawing and Printing Guide for iOS, it explains everything you need to know to get started with doing custom drawing in iOS.