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

标签 ios swift uiview

我创建了一个 UIView 的子类,我在其中绘制了一个图形。我将它设为公共(public)类,这样我就可以向它传递新数据并在需要时更新它。

在我升级到 Xcode 6.3/Swift 1.2 之前,一切都运行良好。现在,当该 View 尝试呈现我的应用程序崩溃时。

我得到的错误是:

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() 以根据需要重新呈现 View 。

感谢您的帮助。

最佳答案

当输入数组为

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

关于ios - 升级到 Swift 1.2/Xcode 6.3 后 moveToPoint 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29563862/

相关文章:

ios - 如何停止 MPMoviePlayerViewController 在 moviePlaybackDidFinish 上的自动关闭?

ios - Apple Watch/WatchKit 语音合成器

arrays - 追加到 UserDefaults 数组 (Swift)

ios - swift 中类似 Tinder 的导航

ios - UIView 隐藏一次后不显示

iOS - 如何创建带阴影的 "material design"兼容 UIView?

ios - 如何以编程方式将 UIView 添加到 Swift 中的 UIScrollView?

ios - SCNNode 放置后不服从位置

IOS 解析 - 指针

ios - 从 Swift 的 UIWebView 中的 url 获取查询字符串参数?