升级到 Swift 1.2 / Xcode 6.3 后 moveToPoint 失败
moveToPoint fails after upgrading to Swift 1.2 / Xcode 6.3
我创建了一个 UIView 的子 class,我在其中绘制了一个图形。我把它变成了 public class 这样我就可以传递新数据并在需要时更新它。
在我升级到 Xcode 6.3 / Swift 1.2 之前,一切都运行良好。现在,当该视图试图呈现我的应用程序崩溃时。
我得到的错误是:
Assertion failed: (CGFloatIsValid(x) && CGFloatIsValid(y)), function void CGPathMoveToPoint(CGMutablePathRef, const
CGAffineTransform *, CGFloat, CGFloat), file Paths/CGPath.cc, line
254.
这是我的 class:
代码
import UIKit
public class GraphView: UIView {
//Data from parent VC
var graphPoints = [0, 0, 0, 0, 0, 0, 0]
var keyColor = BabyMasterStyleKit.bathsBase
override public func drawRect(rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
let colorSpace = CGColorSpaceCreateDeviceRGB()
let width = rect.width
let height = rect.height
//calculate the x point
let margin:CGFloat = 15.0
var columnXPoint = { (column:Int) -> CGFloat in
//Calculate gap between points
let spacer = (width - margin*2 - 4) /
CGFloat((self.graphPoints.count - 1))
var x:CGFloat = CGFloat(column) * spacer
x += margin + 2
return x
}
// calculate the y point
let topBorder:CGFloat = 15
let bottomBorder:CGFloat = 15
let graphHeight = height - topBorder - bottomBorder
let maxValue = maxElement(graphPoints)
var columnYPoint = { (graphPoint:Int) -> CGFloat in
var y:CGFloat = CGFloat(graphPoint) /
CGFloat(maxValue) * graphHeight
y = graphHeight + topBorder - y // Flip the graph
return y
}
//Draw horizontal graph lines on the top of everything
var linePath = UIBezierPath()
//top line
linePath.moveToPoint(CGPoint(x:0, y: topBorder))
linePath.addLineToPoint(CGPoint(x: width,
y:topBorder))
//center line
linePath.moveToPoint(CGPoint(x:0,
y: graphHeight/2 + topBorder))
linePath.addLineToPoint(CGPoint(x:width,
y:graphHeight/2 + topBorder))
let color = UIColor.lightGrayColor()
color.setStroke()
linePath.lineWidth = 0.5
linePath.stroke()
// draw the line graph
keyColor.setFill()
keyColor.setStroke()
// set up the points line
var graphPath = UIBezierPath()
// go to start of line
graphPath.moveToPoint(CGPoint(x:columnXPoint(0), y:columnYPoint(graphPoints[0])))
// add points for each item in the graph points array
// at the correct (x, y) for the point
for i in 1..<graphPoints.count {
let nextPoint = CGPoint(x:columnXPoint(i),
y:columnYPoint(graphPoints[i]))
graphPath.addLineToPoint(nextPoint)
}
graphPath.stroke()
//Draw the circles on top of graph stroke
for i in 0..<graphPoints.count {
var point = CGPoint(x:columnXPoint(i), y:columnYPoint(graphPoints[i]))
point.x -= 5.0/2
point.y -= 5.0/2
let circle = UIBezierPath(ovalInRect:
CGRect(origin: point,
size: CGSize(width: 6.0, height: 6.0)))
circle.fill()
}
// add left and bottom borders
UIColor.lightGrayColor().setStroke()
var borderPath = UIBezierPath()
borderPath.moveToPoint(CGPoint(x:0, y:0))
borderPath.addLineToPoint(CGPoint(x:0, y:height))
borderPath.addLineToPoint(CGPoint(x:width, y:height))
borderPath.stroke()
}
}
应用程序在这一行失败:
graphPath.moveToPoint(CGPoint(x:columnXPoint(0), y:columnYPoint(graphPoints[0])))
数组graphPoints
是我传入的数据。我默认设置为全0。在我的 ViewController 中,我有一个函数传入一组真实数据(7 个不同的数字),然后执行 setNeedsDislay()
以根据需要重新呈现视图。
感谢您的帮助。
我认为默认的 graphPoints 数组被忽略了,所以当视图第一次加载时它没有值(尽管它确实表明它正在将 0 传递给 columnYPoint 函数,所以谁知道呢)。作为解决方法,我刚刚测试了 y.isNAN,如果它被设置为等于 0。在我这样做之后,一切正常。
编辑:刚刚检查过是否实际上忽略了 graphPoints 的默认数组,但事实并非如此。我认为它只是不喜欢它以 0 值传递。如果有人知道为什么会这样,我很想听听。
当输入数组为
var graphPoints = [0, 0, 0, 0, 0, 0, 0]
然后
let maxValue = maxElement(graphPoints)
使maxValue
为0,因此columnYpoint
中的行
var y:CGFloat = CGFloat(graphPoint) / CGFloat(maxValue) * graphHeight
除以零并使 y
成为 NaN
。这是初始数组中所有元素为零的特例。
我创建了一个 UIView 的子 class,我在其中绘制了一个图形。我把它变成了 public class 这样我就可以传递新数据并在需要时更新它。
在我升级到 Xcode 6.3 / Swift 1.2 之前,一切都运行良好。现在,当该视图试图呈现我的应用程序崩溃时。
我得到的错误是:
Assertion failed: (CGFloatIsValid(x) && CGFloatIsValid(y)), function void CGPathMoveToPoint(CGMutablePathRef, const CGAffineTransform *, CGFloat, CGFloat), file Paths/CGPath.cc, line 254.
这是我的 class:
代码import UIKit
public class GraphView: UIView {
//Data from parent VC
var graphPoints = [0, 0, 0, 0, 0, 0, 0]
var keyColor = BabyMasterStyleKit.bathsBase
override public func drawRect(rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
let colorSpace = CGColorSpaceCreateDeviceRGB()
let width = rect.width
let height = rect.height
//calculate the x point
let margin:CGFloat = 15.0
var columnXPoint = { (column:Int) -> CGFloat in
//Calculate gap between points
let spacer = (width - margin*2 - 4) /
CGFloat((self.graphPoints.count - 1))
var x:CGFloat = CGFloat(column) * spacer
x += margin + 2
return x
}
// calculate the y point
let topBorder:CGFloat = 15
let bottomBorder:CGFloat = 15
let graphHeight = height - topBorder - bottomBorder
let maxValue = maxElement(graphPoints)
var columnYPoint = { (graphPoint:Int) -> CGFloat in
var y:CGFloat = CGFloat(graphPoint) /
CGFloat(maxValue) * graphHeight
y = graphHeight + topBorder - y // Flip the graph
return y
}
//Draw horizontal graph lines on the top of everything
var linePath = UIBezierPath()
//top line
linePath.moveToPoint(CGPoint(x:0, y: topBorder))
linePath.addLineToPoint(CGPoint(x: width,
y:topBorder))
//center line
linePath.moveToPoint(CGPoint(x:0,
y: graphHeight/2 + topBorder))
linePath.addLineToPoint(CGPoint(x:width,
y:graphHeight/2 + topBorder))
let color = UIColor.lightGrayColor()
color.setStroke()
linePath.lineWidth = 0.5
linePath.stroke()
// draw the line graph
keyColor.setFill()
keyColor.setStroke()
// set up the points line
var graphPath = UIBezierPath()
// go to start of line
graphPath.moveToPoint(CGPoint(x:columnXPoint(0), y:columnYPoint(graphPoints[0])))
// add points for each item in the graph points array
// at the correct (x, y) for the point
for i in 1..<graphPoints.count {
let nextPoint = CGPoint(x:columnXPoint(i),
y:columnYPoint(graphPoints[i]))
graphPath.addLineToPoint(nextPoint)
}
graphPath.stroke()
//Draw the circles on top of graph stroke
for i in 0..<graphPoints.count {
var point = CGPoint(x:columnXPoint(i), y:columnYPoint(graphPoints[i]))
point.x -= 5.0/2
point.y -= 5.0/2
let circle = UIBezierPath(ovalInRect:
CGRect(origin: point,
size: CGSize(width: 6.0, height: 6.0)))
circle.fill()
}
// add left and bottom borders
UIColor.lightGrayColor().setStroke()
var borderPath = UIBezierPath()
borderPath.moveToPoint(CGPoint(x:0, y:0))
borderPath.addLineToPoint(CGPoint(x:0, y:height))
borderPath.addLineToPoint(CGPoint(x:width, y:height))
borderPath.stroke()
}
}
应用程序在这一行失败:
graphPath.moveToPoint(CGPoint(x:columnXPoint(0), y:columnYPoint(graphPoints[0])))
数组graphPoints
是我传入的数据。我默认设置为全0。在我的 ViewController 中,我有一个函数传入一组真实数据(7 个不同的数字),然后执行 setNeedsDislay()
以根据需要重新呈现视图。
感谢您的帮助。
我认为默认的 graphPoints 数组被忽略了,所以当视图第一次加载时它没有值(尽管它确实表明它正在将 0 传递给 columnYPoint 函数,所以谁知道呢)。作为解决方法,我刚刚测试了 y.isNAN,如果它被设置为等于 0。在我这样做之后,一切正常。
编辑:刚刚检查过是否实际上忽略了 graphPoints 的默认数组,但事实并非如此。我认为它只是不喜欢它以 0 值传递。如果有人知道为什么会这样,我很想听听。
当输入数组为
var graphPoints = [0, 0, 0, 0, 0, 0, 0]
然后
let maxValue = maxElement(graphPoints)
使maxValue
为0,因此columnYpoint
var y:CGFloat = CGFloat(graphPoint) / CGFloat(maxValue) * graphHeight
除以零并使 y
成为 NaN
。这是初始数组中所有元素为零的特例。