快速箭头画 - 缺少一个三角形

时间:2015-03-21 06:11:42

标签: ios iphone swift object

我在视图上画了一个箭头,但三角形的一部分丢失了。我不知道我的代码中缺少什么。有关更多细节,我在这里添加了一个图像。 enter image description here

import UIKit

class AnnotationArrow: UIView
{
       //MARK: Global Variables

    var startingPoint : CGPoint = CGPoint()
    var endingPoint : CGPoint = CGPoint()
    var arrowLength : CGFloat = CGFloat()
    var arrowPath : UIBezierPath = UIBezierPath()
    var selectedInBox_Activated_Anchor_Points = false

    //MARK: For resizing

    var kUserResizableViewDefaultMinWidth = 40.0
    var kUserResizableViewDefaultMinHeight  = 40.0
    var kUserResizableViewInteractiveBorderSize = 10.0

    //MARK: initFrame

    override init(frame: CGRect)
    {
        super.init(frame: frame)
    }

    //MARK: initCoder

    required init(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)
    }

    func passingValues(startingPointValue : CGPoint, endingPointValue : CGPoint)
    {
        self.startingPoint = startingPointValue
        self.endingPoint = endingPointValue

        var xDistance : CGFloat = self.endingPoint.x - self.startingPoint.x
        var yDistance : CGFloat = self.endingPoint.y - self.startingPoint.y

        self.arrowLength = sqrt((xDistance * xDistance) + (yDistance * yDistance))
    }

      //MARK: drawRect

    override func drawRect(rect: CGRect)
    {
        var tailWidth : CGFloat = max(4.0, self.arrowLength * 0.07)
        var headLength : CGFloat = max(self.arrowLength / 3.0, 10.0)
        var headWidth : CGFloat = headLength * 0.9
        var strokeWidth : CGFloat = max(1.0, tailWidth * 0.25)

        self.layer.shadowRadius = max(4.0, tailWidth)

        self.arrowPath = self.bezierPathWithArrowFromPoint(self.startingPoint, endPoint: self.endingPoint, tailWidth: tailWidth, headWidth: headWidth, headLength: headLength)

        self.arrowPath.fill()
        self.arrowPath.stroke()
        self.arrowPath.lineWidth = strokeWidth
        self.layer.shadowPath  = self.arrowPath.CGPath
    }
 //MARK: Creating Path

    func bezierPathWithArrowFromPoint(startingPoint : CGPoint, endPoint : CGPoint, tailWidth : CGFloat, headWidth : CGFloat, headLength : CGFloat) -> UIBezierPath
    {

        var length = hypotf( Float(endPoint.x) - Float(startingPoint.x) , Float(endPoint.y) - Float(startingPoint.y))

        var tailLength : CGFloat = CGFloat(length) - headLength

        var points = [CGPointMake(0, tailWidth / 2), CGPointMake(tailLength, tailWidth / 2), CGPointMake(tailLength, headWidth / 2), CGPointMake(CGFloat(length), 0), CGPointMake(tailLength, (-headWidth) / 2), CGPointMake(tailLength, (-tailWidth) / 2 ), CGPointMake(0, (-tailWidth) / 2)]


        var cosine : CGFloat = (endPoint.x - startingPoint.x) / CGFloat(length)
        var sine : CGFloat = (endPoint.y - startingPoint.y) / CGFloat(length)
        var transform : CGAffineTransform = CGAffineTransform(a: cosine, b: sine, c: -sine, d: cosine, tx: startingPoint.x, ty: startingPoint.y)

        var cgPath : CGMutablePathRef = CGPathCreateMutable()
        CGPathAddLines(cgPath, &transform, points, UInt(sizeofValue(points)) / 1)
        CGPathCloseSubpath(cgPath)

        var bezierPath : UIBezierPath = UIBezierPath(CGPath: cgPath)
        bezierPath.lineCapStyle = kCGLineCapRound
        bezierPath.lineJoinStyle = kCGLineJoinRound

        return bezierPath
    }

如果有人知道遗失了什么,请在这里帮助我。

0 个答案:

没有答案