-1

私はこのリンクのメソッドを使用しています: UILabel の円にテキストを合わせる方法

while (![circle containsPoint:p])

元の方法を使用すると、[UIBezierPath bezierPathWithOvalInRect:r];うまく機能します。しかし、独自の UIBezierPath を描画して置き換えると、機能しません。while ループは永遠に続きます。以下は、(A) カスタム UIBezierPath 描画と (B) デフォルトの UIBezierPath 楕円形のスクリーンショットです: http://i.imgur.com/n74BHjX.png

func drawPath() -> UIBezierPath 
{
    var path = UIBezierPath()
    path.moveToPoint(CGPointMake(126,488))
    path.addCurveToPoint(CGPointMake(196,471), controlPoint1: CGPointMake(156,488), controlPoint2: CGPointMake(175,471))
    path.addCurveToPoint(CGPointMake(266,488), controlPoint1: CGPointMake(216,471), controlPoint2: CGPointMake(235,488))
    path.addCurveToPoint(CGPointMake(362,382), controlPoint1: CGPointMake(296,488), controlPoint2: CGPointMake(338,440))
    path.addCurveToPoint(CGPointMake(304,290), controlPoint1: CGPointMake(327,365), controlPoint2: CGPointMake(304,331))
    path.addCurveToPoint(CGPointMake(349,206), controlPoint1: CGPointMake(304,255), controlPoint2: CGPointMake(322,224))
    path.addCurveToPoint(CGPointMake(265,164), controlPoint1: CGPointMake(327,178), controlPoint2: CGPointMake(295,164))
    path.addCurveToPoint(CGPointMake(196,182), controlPoint1: CGPointMake(219,164), controlPoint2: CGPointMake(218,182))
    path.addCurveToPoint(CGPointMake(126,164), controlPoint1: CGPointMake(173,182), controlPoint2: CGPointMake(172,164))
    path.addCurveToPoint(CGPointMake(12,298), controlPoint1: CGPointMake(73,164), controlPoint2: CGPointMake(12,210))
    path.addCurveToPoint(CGPointMake(126,488), controlPoint1: CGPointMake(12,387), controlPoint2: CGPointMake(81,488))

    return path
}

override func lineFragmentRectForProposedRect(proposedRect: CGRect, atIndex characterIndex: Int, writingDirection baseWritingDirection: NSWritingDirection, remainingRect: UnsafeMutablePointer<CGRect>) -> CGRect 
{
    var result = super.lineFragmentRectForProposedRect(proposedRect, atIndex:characterIndex, writingDirection:baseWritingDirection, remainingRect:remainingRect)
    let r = CGRectMake(0,0,self.size.width,self.size.height)
    let circle = drawPath() //UIBezierPath(ovalInRect: r)
    println("this is self created irregular shape: \(path)")
    println("this is the default working OVALLLL: \(UIBezierPath(ovalInRect: r))")

    while !circle.containsPoint(result.origin) {
        result.origin.x += 0.1
        println(result.origin.x)
    }
     while !circle.containsPoint(CGPointMake(result.maxX, result.origin.y)) {
        result.size.width -= 0.1
    }
    return result
}
4

1 に答える 1

1

あなたの形状を正しく理解していれば、水平方向にスキャンしてオーバーラップを検出しようとしていますが、使用しているパスは平面 y=0 には存在しません。yの値を変更するr.originか、y=0 に到達するパスを使用する必要があります。

于 2015-04-02T13:10:52.557 に答える