升级到Swift 1.2 / Xcode 6.3后,moveToPoint失败

时间:2015-04-10 14:20:40

标签: ios swift uiview

我已经创建了一个UIView的子类,我在其中绘制了一个图形。我把它变成了一个公共类,所以我可以传递新数据并在需要时更新它。

在我升级到Xcode 6.3 / Swift 1.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()重新渲染视图。

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

当输入数组是

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。这是在初始数组中包含所有零元素的特殊情况。

答案 1 :(得分:0)

我认为默认的graphPoints数组被忽略了,所以当视图第一次加载时它没有值(尽管它确实显示它正在向0中传递一个0到函数中,所以谁知道)。作为一个解决方法,我刚刚测试了y.isNAN,如果它被设置为等于0.我做了之后一切正常。

编辑:只是检查一下是否实际上是否忽略了graphPoints的默认数组,而事实并非如此。我认为它只是不喜欢它以0值传递。如果有人知道为什么会这样,我很乐意听到它。