简体   繁体   English

UIImage.DrawInRect导致错误/通知

[英]UIImage.DrawInRect causes error/notification

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? 我真的不知道为什么当player.drawInRect引起bg.drawInRect不会导致错误? 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. 9月16日22:13:16 imac-admin.lan Letalka_drawrect [804]:CGContextRestoreGState:无效的上下文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; 您需要在drawRect调用之外启动计时器; didMoveToSuperview might be a good place, but it depends on what you're trying to accomplish. didMoveToSuperview可能是个好地方,但这取决于您要完成的工作。 Then, in timerRedraw , call setNeedsDisplay() to trigger a re-draw. 然后,在timerRedraw ,调用setNeedsDisplay()以触​​发重新绘制。

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. 我强烈建议您阅读Apple的iOS绘图和打印指南 ,其中介绍了开始在iOS中进行自定义绘图所需的一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM