gpt4 book ai didi

ios - 省略 UIColor.setFill() 错误消息?

转载 作者:行者123 更新时间:2023-11-28 13:20:55 28 4
gpt4 key购买 nike

我有两个并排的函数,它们循环不断地绘制两个 UIBezierPaths,问题是,它们每个都有不同的颜色,所以我经常需要重申 UIColor.blackColor().setFill()UIColor(patternImage: UIImage(named: "normalpaper.jpg")).setFill(),缺点是它使控制台无法读取,因为它会无休止地向您发送警告消息。

<Error>: 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.

因此,这是我的问题,有没有一种方法可以做到这一点,而不会将此警告消息发送到我的控制台?也许是一种使警告消息不会出现的方法? (无法通过搜索找到一个)或者可能是一种省略消息的方式?非常感谢任何输入,感谢阅读 -Zach。

-

如果你需要绘图函数,它们在下面

func drawCircle() {

//Setting the draw color
UIColor.blackColor().setFill()

// Creating the rectangle's size
var drawRect = roundDrawRect(10.0, angle: 7)

//Incrementing the coords
++y
++x

//Drawing the rectangle
drawRect.fill()

}

func eraseCircle() {

//Setting the eraser color
UIColor(patternImage: UIImage(named: "normalpaper.jpg")).setFill()

//Decrementing the coords
eraseX = x - 2
eraseY = y - 2

// Creating the rectangle's size
var eraseRect = roundEraseRect(10.0, angle: 7)

//Drawing the rectangle
eraseRect.fill()

}

下面是完整的 CircleView 类(我对编程还是很陌生,所以它可能效率很低)

//Creating a view capable of painting the circle
class CircleView: UIView {

//Starting X Pos
var x: CGFloat = 100
var eraseX: CGFloat = 100

//Starting Y Pos
var y: CGFloat = 100
var eraseY: CGFloat = 100

//Starting the loop of functions
override func drawRect(rect: CGRect) {

//Creating the looping draw timer
NSTimer.scheduledTimerWithTimeInterval(
0.2,
target: self,
selector: Selector("timerDraw"),
userInfo: nil,
repeats: true)

//Creating the looping erase timer
NSTimer.scheduledTimerWithTimeInterval(
0.3,
target: self,
selector: Selector("timerErase"),
userInfo: nil,
repeats: true)

}

func drawCircle() {

//Setting the draw color
UIColor.blackColor().setFill()

// Creating the rectangle's size
var drawRect = roundDrawRect(10.0, angle: 7)

//Incrementing the coords
++y
++x

//Drawing the rectangle
drawRect.fill()

}

func eraseCircle() {

//Setting the eraser color
UIColor(patternImage: UIImage(named: "normalpaper.jpg")).setFill()

//Decrementing the coords
eraseX = x - 2
eraseY = y - 2

// Creating the rectangle's size
var eraseRect = roundEraseRect(10.0, angle: 7)

//Drawing the rectangle
eraseRect.fill()

}

func timerDraw(){

//DO DRAW LOOP HERE
drawCircle()

}

func timerErase(){

//DO ERASE LOOP HERE
eraseCircle()

}

//Defining the rounded rect erasing (Circle)
func roundEraseRect(radius: CGFloat, angle: CGFloat) -> UIBezierPath {

//Creating the rounded rect (Circle)
var roundedRect = UIBezierPath()

roundedRect.moveToPoint(CGPointMake(eraseX,eraseY))
println(CGPointMake(eraseX,eraseY))

roundedRect.addArcWithCenter(CGPointZero, radius: radius,
startAngle: 0, endAngle: angle ,
clockwise: true)

return roundedRect
}

//Defining the rounded rect drawing (Circle)
func roundDrawRect(radius: CGFloat, angle: CGFloat) -> UIBezierPath {

//Creating the rounded rect (Circle)
var roundedRect = UIBezierPath()

roundedRect.moveToPoint(CGPointMake(x,y))

roundedRect.addArcWithCenter(CGPointZero, radius: radius,
startAngle: 0, endAngle: angle ,
clockwise: true)

return roundedRect
}




}

class ViewController: UIViewController {

最佳答案

Apple 的 Drawing and Printing Guide for iOS解释了所有这些,我强烈建议在进一步使用自定义绘图代码之前阅读它。至少,请阅读有关 iOS Drawing Concepts 的部分.


问题是您在 CircleViewdrawRect 函数中创建并触发了 NSTimer。绘图调用只能在正确的上下文中进行(这实际上是您看到它试图告诉您的错误)。通过在从 NSTimer 调用的函数中进行绘图,您实际上是在 您的 drawRect 函数中进行绘图,并且没有有效的在这种情况下绘制上下文。另外,按照现在的代码,每次系统需要重绘 View 时,您都会启动新的计时器;随着计时器开始重叠,这可能很快就会失控。

但是,只需稍微重新安排一下,我们就可以完成这项工作。

请注意:这不一定是处理圆形动画的正确方法,但它会解决您提出的特定问题关于无效上下文错误。

  1. drawRect 中取出所有内容,并将其替换为对 eraseCircledrawCircle 的调用。

  2. 采用必须递增 xy 以及 eraseXeraseY 的逻辑从 drawCircleeraseCircle 中取出,并将其放入 timerDrawtimerErase 中。

  3. 不要直接在timerDrawtimerErase 中调用绘图代码,而是通过调用setNeedsDisplay() 告诉 View 系统您需要重绘 View 。 .这会将您的 View 标记为需要重绘,并且 View 系统会尽快自动再次调用您的 drawRect 函数。

  4. 通过覆盖 didMoveToSuperview 并在那里启动它们,让您的计时器再次工作;如果它们已经在运行,您还应该添加停止它们的逻辑。

第 1 步和第 3 步是消除错误的关键部分。

像这样:

//Creating a view capable of painting the circle
class CircleView: UIView {
// Timers
var drawTimer: NSTimer?
var eraseTimer: NSTimer?

//Starting X Pos
var x: CGFloat = 100
var eraseX: CGFloat = 100

//Starting Y Pos
var y: CGFloat = 100
var eraseY: CGFloat = 100

override func drawRect(rect: CGRect) {
eraseCircle()
drawCircle()
}

override func didMoveToSuperview() {
// If we have active timers, stop them
if var drawTimer = self.drawTimer {
// This stops the timer
drawTimer.invalidate()
self.drawTimer = nil
}

if var eraseTimer = self.eraseTimer {
// This stops the timer
eraseTimer.invalidate()
self.eraseTimer = nil
}

// If we're actually part of the view hierarchy, start the timers
if self.superview != nil {
//Creating the looping draw timer
self.drawTimer = NSTimer.scheduledTimerWithTimeInterval(
0.2,
target: self,
selector: Selector("timerDraw"),
userInfo: nil,
repeats: true)

//Creating the looping erase timer
self.eraseTimer = NSTimer.scheduledTimerWithTimeInterval(
0.3,
target: self,
selector: Selector("timerErase"),
userInfo: nil,
repeats: true)
}
}

func drawCircle() {
//Setting the draw color
UIColor.blackColor().setFill()

// Creating the rectangle's size
var drawRect = roundDrawRect(10.0, angle: 7)

//Drawing the rectangle
drawRect.fill()

}

func eraseCircle() {
//Setting the eraser color
UIColor(patternImage: UIImage(named: "normalpaper.jpg")).setFill()

// Creating the rectangle's size
var eraseRect = roundEraseRect(10.0, angle: 7)

//Drawing the rectangle
eraseRect.fill()

}

func timerDraw(){
//Incrementing the coords
++y
++x

self.setNeedsDisplay()
}

func timerErase(){
//Decrementing the coords
eraseX = x - 2
eraseY = y - 2

self.setNeedsDisplay()
}

//Defining the rounded rect erasing (Circle)
func roundEraseRect(radius: CGFloat, angle: CGFloat) -> UIBezierPath {

//Creating the rounded rect (Circle)
var roundedRect = UIBezierPath()

roundedRect.moveToPoint(CGPointMake(eraseX,eraseY))
println(CGPointMake(eraseX,eraseY))

roundedRect.addArcWithCenter(CGPointZero, radius: radius,
startAngle: 0, endAngle: angle ,
clockwise: true)

return roundedRect
}

//Defining the rounded rect drawing (Circle)
func roundDrawRect(radius: CGFloat, angle: CGFloat) -> UIBezierPath {

//Creating the rounded rect (Circle)
var roundedRect = UIBezierPath()

roundedRect.moveToPoint(CGPointMake(x,y))

roundedRect.addArcWithCenter(CGPointZero, radius: radius,
startAngle: 0, endAngle: angle ,
clockwise: true)

return roundedRect
}
}

至于实现您正在尝试的动画的最佳方式,您可以考虑只绘制一次圆圈,然后在您的 UIViewController 中在计时器上移动整个 View 。或者,可能更好,使用 Core Animation .或者,如果您的最终产品将是一款游戏(甚至是类似游戏),也许可以看看 Sprite Kit .

关于ios - 省略 UIColor.setFill() 错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25840455/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com