我已经创建了一个UIView的子类,在其中我画了一个图。我把它变成了一个公共类,这样我就可以传递新的数据,并在需要时进行更新。
在我升级到Xcode 6.3 /SWIFT1.2之前,这一切都很完美。现在,当该视图试图呈现我的应用程序崩溃时。
我得到的错误是:
断言失败:(CGFloatIsValid(x) & CGFloatIsValid(y)),函数void CGPathMoveToPoint(CGMutablePathRef,const CGAffineTransform *,CGFloat,CGFloat),文件路径/CGPath.cc,第254行。
下面是我的课程代码:
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()来根据需要重新呈现视图。
谢谢你的帮助。
发布于 2015-04-10 19:15:58
当输入数组是
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.这是一个特殊情况,在初始数组中有所有的零元素。
发布于 2015-04-10 17:01:58
我认为默认的graphPoints数组被忽略了,所以当视图第一次加载时它没有值(尽管它确实显示它将0传递到columnYPoint函数,所以谁知道呢)。作为一项工作,我刚刚测试了y.isNAN,如果它被设置为等于0。在我做完之后一切都成功了。
编辑:只是检查一下,看看graphPoints的默认数组是否被忽略了,它没有被忽略,我认为它只是不喜欢它是以0的值传递的。如果有人知道为什么会这样,我很想听听。
https://stackoverflow.com/questions/29563862
复制相似问题