[英]moveToPoint fails after upgrading to Swift 1.2 / Xcode 6.3
I've created a sub class of a UIView in which I draw a graph. 我创建了一个UIView的子类,在其中绘制了图形。 I made it a public class so I can pass it new data and have it update when needed. 我将其设置为公共类,以便可以将其传递给新数据并在需要时进行更新。
It all worked perfectly until I upgraded to Xcode 6.3 / Swift 1.2. 在我升级到Xcode 6.3 / Swift 1.2之前,一切都运行良好。 Now when that view attempts to render my app crashes. 现在,当该视图尝试呈现我的应用程序时崩溃。
The error I get is: 我得到的错误是:
Assertion failed: (CGFloatIsValid(x) && CGFloatIsValid(y)), function void CGPathMoveToPoint(CGMutablePathRef, const CGAffineTransform *, CGFloat, CGFloat), file Paths/CGPath.cc, line 254. 断言失败:(CGFloatIsValid(x)&& CGFloatIsValid(y)),函数void CGPathMoveToPoint(CGMutablePathRef,const CGAffineTransform *,CGFloat,CGFloat),文件Paths / CGPath.cc,第254行。
Here is my code for the 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()
}
}
The app fails at this line: 该应用程序在此行失败:
graphPath.moveToPoint(CGPoint(x:columnXPoint(0), y:columnYPoint(graphPoints[0])))
The array graphPoints
is the data I am passing in. I have it set to all 0s as a default. 数组graphPoints
是我要传入的数据。我将其默认设置为全0。 In my ViewController I have a function that passes in an array of real data (7 different numbers) and then does a setNeedsDislay()
to re-render the view as needed. 在我的ViewController中,我有一个传递实数据数组(7个不同数字)的函数,然后执行setNeedsDislay()
以根据需要重新呈现视图。
Thanks for your help. 谢谢你的帮助。
When the input array is 当输入数组是
var graphPoints = [0, 0, 0, 0, 0, 0, 0]
then 然后
let maxValue = maxElement(graphPoints)
makes maxValue
0, so that the line in columnYpoint
使maxValue
0,以便columnYpoint
中的行
var y:CGFloat = CGFloat(graphPoint) / CGFloat(maxValue) * graphHeight
does a divide-by-zero and makes y
a NaN
. 进行零除并让y
为NaN
。 It's a special case of having all zero elements in the initial array. 在初始数组中具有所有零元素是一种特殊情况。
I think the default graphPoints array was being ignored, so when the view first loaded it didn't have values (though it did show that it was passing a 0 into the columnYPoint function, so who knows). 我认为默认的graphPoints数组被忽略了,所以当视图第一次加载时它没有值(尽管它确实表明它正在将0传递给columnYPoint函数,所以谁知道)。 As a work around I just tested for y.isNAN and if it was set it to equal 0. After I did that everything worked. 作为一项变通方法,我只是测试了y.isNAN并将其设置为等于0。之后,一切正常。
edit: Just checked to see if in fact the default array for graphPoints was getting ignored, and it wasn't. 编辑:只是检查以查看是否实际上是否忽略了graphPoints的默认数组,而实际上并没有。 I think that it just doesn't like that it was being passed in a 0 value. 我认为它只是不喜欢它以0值传递。 If anyone has an idea as to why this would be, I'd love to hear about it. 如果有人知道为什么会这样,我很想听听。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.