箭头锚点无法快速正确显示

阿尔文·瓦格斯(Alvin Varghese)

这就是锚点现在显示的方式这是我用来绘制箭头并为其添加锚点的代码。但是我们可以在不同的方向上添加箭头,因此,锚点放错了位置。有没有人知道我如何将这些锚点正确地放置在箭头的起点和箭头的终点。

//
//  AnnotationArrow.swift
//  
//
//  Created by Alvin Varghese on 10/Mar/15.
//  Copyright (c) 2015![enter image description here][2]. All rights reserved.
//

import UIKit

protocol _Annotation_ARROW_PROTOCOL
{
    func valuesFromArrowSubClass(instance : AnnotationArrow)
}

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

    var delegate : _Annotation_ARROW_PROTOCOL!

    //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

        if self.selectedInBox_Activated_Anchor_Points
        {
            // Starting the code for - ResizableView anchor points

            var color : UIColor = UIColor.greenColor()

            var context : CGContextRef = UIGraphicsGetCurrentContext()
            CGContextSaveGState(context)

            // 1 -  Drawing the bounding box

            CGContextSetFillColorWithColor(context, color.CGColor)
            CGContextSetStrokeColorWithColor(context, color.CGColor)
            CGContextMoveToPoint(context, self.startingPoint.x, self.startingPoint.y)
            CGContextAddLineToPoint(context, self.endingPoint.x, self.endingPoint.y )
            CGContextSetLineWidth(context, 2.0)
            CGContextStrokePath(context)
            CGContextSetStrokeColorWithColor(context, color.CGColor)

            // 2 -  Calculate the bounding boxes for each of the anchor points.

            var startingSectionRect : CGRect  = CGRectMake(self.startingPoint.x, self.startingPoint.y, 20.0, 20.0)

            var endingSectionRect : CGRect = CGRectMake(self.endingPoint.x, self.endingPoint.y, 20.0, 20.0)

            // 3 -  Create the gradient to paint the anchor points.

            var colors : [CGFloat] = [1.0, 1.0]
            var baseSpace : CGColorSpaceRef = CGColorSpaceCreateDeviceRGB()
            var gradient : CGGradientRef = CGGradientCreateWithColorComponents(baseSpace, colors, nil, 2)

            // 4 - Set up the stroke for drawing the border of each of the anchor points.

            CGContextSetLineWidth(context, 2.0)
            CGContextSetShadow(context, CGSizeMake(0.5, 0.5), 1)
            CGContextSetStrokeColorWithColor(context, color.CGColor)

            // 5 - Fill each anchor point using the gradient, then stroke the border.

            var allPoints : [CGRect] = [ startingSectionRect, endingSectionRect]

            for index in 0..<2
            {
                var currentPoint : CGRect = allPoints[index]
                CGContextSaveGState(context)
                CGContextAddEllipseInRect(context, currentPoint)
                CGContextClip(context)

                var startPoint : CGPoint = CGPointMake(CGRectGetMidX(currentPoint), CGRectGetMinY(currentPoint))

                var endPoint : CGPoint = CGPointMake(CGRectGetMidX(currentPoint), CGRectGetMaxY(currentPoint))


                CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0)
                CGContextRestoreGState(context)
                UIColor.greenColor().setFill()

                //  CGContextStrokeEllipseInRect(context, CGRectInset(currentPoint, 1, 1))
                CGContextFillEllipseInRect(context, currentPoint)
            }
            // Restoring state to most recently saved state
            CGContextRestoreGState(context)
        }
    }


    //MARK: hitTest

    override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView?
    {
        if self.arrowPath.containsPoint(point)
        {
            self.selectedInBox_Activated_Anchor_Points  = true
            self.setNeedsDisplay()
            self.delegate.valuesFromArrowSubClass(self)
            return self
        }

        var boundingBox : CGRect = self.arrowPath.bounds

        if ((boundingBox.size.width < 80 || boundingBox.size.height < 80) && CGRectContainsPoint(boundingBox, point))
        {
            self.selectedInBox_Activated_Anchor_Points = true
            self.setNeedsDisplay()
            self.delegate.valuesFromArrowSubClass(self)
            return self
        }

        self.selectedInBox_Activated_Anchor_Points  = false
        self.setNeedsDisplay()
        return nil
    }


    //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)(points.count))
        CGPathCloseSubpath(cgPath)

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

        return bezierPath
    }

    //MARK: UITouch Methods

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
    {
        var touch : UITouch = touches.anyObject() as UITouch


    }

    override func touchesMoved(touches: NSSet, withEvent event: UIEvent)
    {
        var touch : UITouch = touches.allObjects.last as UITouch
        var point = touch.locationInView(self.superview)

    }

    override func touchesEnded(touches: NSSet, withEvent event: UIEvent)
    {
    }

    override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!)
    {
    }
}
苏尔坦

在绘制点时,您考虑的点是矩形:

var startingSectionRect : CGRect  = CGRectMake(self.startingPoint.x, self.startingPoint.y, 20.0, 20.0)

然后,您用椭圆(圆形)填充此矩形。显然这是错误的,因为您感兴趣的点位于中心位置时停留在左上角:

let pointSize = 20.0
var startingSectionRect = CGRectMake(self.startingPoint.x - pointSize / 2.0, self.startingPoint.y - pointSize / 2.0, pointSize, pointSize)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

内联显示<a>锚点

来自分类Dev

如何更新快速布局锚点?

来自分类Dev

dotPlot无法正确显示点颜色

来自分类Dev

CSS显示块无法与锚点链接一起使用

来自分类Dev

显示锚点时停止悬停

来自分类Dev

dynamicjs在单击时显示图像锚点

来自分类Dev

显示锚点时停止悬停

来自分类Dev

HtmlUnit-无法从div获取锚点

来自分类Dev

HTML锚点无法更改网址?

来自分类Dev

HtmlUnit-无法从div获取锚点

来自分类Dev

jQuery滚动锚点无法正常工作

来自分类Dev

使用滑块快速操纵由乘数约束的锚点

来自分类Dev

无法成功托管云锚点并从谷歌云锚点 api 检索云锚点 ID

来自分类Dev

如何让锚点跨度文本显示在 css 或 scss 中的锚点之前

来自分类Dev

Flexslider箭头显示不正确

来自分类Dev

如何正确显示表格的上/下箭头?

来自分类Dev

如何正确显示表格的上/下箭头?

来自分类Dev

无法获得箭头键的正确值

来自分类Dev

无法添加动画以正确放置箭头

来自分类Dev

以编程方式设置锚点时未显示焦点轮廓

来自分类Dev

在CSS锚点上悬停时显示div?

来自分类Dev

基于对应锚点的hasClass('active')显示/隐藏div

来自分类Dev

以编程方式设置锚点时未显示焦点轮廓

来自分类Dev

JavaScript在锚点单击时显示输入类型文件

来自分类Dev

为什么 vaadin-grid 不显示链接(锚点)?

来自分类Dev

仅当用户登录时才显示锚点 *ngIf

来自分类Dev

我正在尝试使用香草JS突出显示单击的锚点并删除以前突出显示的锚点

来自分类Dev

Chrome:无法将:focus CSS应用于锚点

来自分类Dev

Python re无法拆分零宽度锚点吗?