2012-05-01 7 views
1

私はQuartzの描画に経験がありません。マップコールアウトにポインタを描画するのに助けが必要です

吹き出しビューで矩形を描く必要があります。私はそれを描画するいくつかのサンプルコードを発見した。そのため

私の現在のコードは、これは私が長方形の上部にある縁でその三角形の形状を描画する必要がgetting.But午前現在のコールアウトです

- (void)drawRect:(CGRect)rect { 
    CGFloat stroke = 1.0; 
    CGFloat radius = 7.0; 
    CGMutablePathRef path = CGPathCreateMutable(); 
    UIColor *color; 
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGFloat parentX = [self relativeParentXPosition]; 

    //Determine Size 
    rect = self.bounds; 
    rect.size.width -= stroke + 14; 
    rect.size.height -= stroke + 14 + CalloutMapAnnotationViewHeightAboveParent; 
    rect.origin.x += stroke/2.0 + 7; 
    rect.origin.y += stroke/2.0 + 7; 

    //Create Path For Callout Bubble 
    CGPathMoveToPoint(path, NULL, rect.origin.x, rect.origin.y + radius); 
    CGPathAddLineToPoint(path, NULL, rect.origin.x, rect.origin.y + rect.size.height - radius); 
    CGPathAddArc(path, NULL, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, radius, 0.0f, M_PI/2, 1); 
    CGPathAddLineToPoint(path, NULL, parentX - 15, rect.origin.y + rect.size.height); 
    CGPathAddLineToPoint(path, NULL, parentX, rect.origin.y + rect.size.height + 15); 
    CGPathAddLineToPoint(path, NULL, parentX + 15, rect.origin.y + rect.size.height); 
    CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height); 
    CGPathAddArc(path, NULL, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height - radius, radius, M_PI/2, 0.0f, 1); 
    CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y + radius); 
    CGPathAddArc(path, NULL, rect.origin.x + rect.size.width - radius, rect.origin.y + radius, radius, 0.0f, -M_PI/2, 1); 
    CGPathAddLineToPoint(path, NULL, rect.origin.x + radius, rect.origin.y); 
    CGPathAddArc(path, NULL, rect.origin.x + radius, rect.origin.y + radius, radius, -M_PI/2, M_PI, 1); 
    CGPathCloseSubpath(path); 

    //Fill Callout Bubble & Add Shadow 
    color = [[UIColor blackColor] colorWithAlphaComponent:.6]; 
    [color setFill]; 
    CGContextAddPath(context, path); 
    CGContextSaveGState(context); 
    CGContextSetShadowWithColor(context, CGSizeMake (0, self.yShadowOffset), 6, [UIColor colorWithWhite:0 alpha:.5].CGColor); 
    CGContextFillPath(context); 
    CGContextRestoreGState(context); 

    //Stroke Callout Bubble 
    color = [[UIColor darkGrayColor] colorWithAlphaComponent:.9]; 
    [color setStroke]; 
    CGContextSetLineWidth(context, stroke); 
    CGContextSetLineCap(context, kCGLineCapSquare); 
    CGContextAddPath(context, path); 
    CGContextStrokePath(context); 

    //Determine Size for Gloss 
    CGRect glossRect = self.bounds; 
    glossRect.size.width = rect.size.width - stroke; 
    glossRect.size.height = (rect.size.height - stroke)/2; 
    glossRect.origin.x = rect.origin.x + stroke/2; 
    glossRect.origin.y += rect.origin.y + stroke/2; 

    CGFloat glossTopRadius = radius - stroke/2; 
    CGFloat glossBottomRadius = radius/1.5; 

    //Create Path For Gloss 


    CGMutablePathRef glossPath = CGPathCreateMutable(); 
    CGPathMoveToPoint(glossPath, NULL, glossRect.origin.x, glossRect.origin.y + glossTopRadius); 
    CGPathAddLineToPoint(glossPath, NULL, glossRect.origin.x, glossRect.origin.y + glossRect.size.height - glossBottomRadius); 
    CGPathAddArc(glossPath, NULL, glossRect.origin.x + glossBottomRadius, glossRect.origin.y + glossRect.size.height - glossBottomRadius, glossBottomRadius, M_PI, M_PI/2, 1); 
    CGPathAddLineToPoint(glossPath, NULL, glossRect.origin.x + glossRect.size.width - glossBottomRadius, glossRect.origin.y + glossRect.size.height); 
    CGPathAddArc(glossPath, NULL, glossRect.origin.x + glossRect.size.width - glossBottomRadius, glossRect.origin.y + glossRect.size.height - glossBottomRadius, glossBottomRadius, M_PI/2, 0.0f, 1); 
    CGPathAddLineToPoint(glossPath, NULL, glossRect.origin.x + glossRect.size.width, glossRect.origin.y + glossTopRadius); 
    CGPathAddArc(glossPath, NULL, glossRect.origin.x + glossRect.size.width - glossTopRadius, glossRect.origin.y + glossTopRadius, glossTopRadius, 0.0f, -M_PI/2, 1); 
    CGPathAddLineToPoint(glossPath, NULL, glossRect.origin.x + glossTopRadius, glossRect.origin.y); 
    CGPathAddArc(glossPath, NULL, glossRect.origin.x + glossTopRadius, glossRect.origin.y + glossTopRadius, glossTopRadius, -M_PI/2, M_PI, 1); 
    CGPathCloseSubpath(glossPath); 

    //Fill Gloss Path 
    CGContextAddPath(context, glossPath); 
    CGContextClip(context); 
    CGFloat colors[] = 
    { 
     1, 1, 1, .3, 
     1, 1, 1, .1, 
    }; 
    CGFloat locations[] = { 0, 1.0 }; 
    CGGradientRef gradient = CGGradientCreateWithColorComponents(space, colors, locations, 2); 
    CGPoint startPoint = glossRect.origin; 
    CGPoint endPoint = CGPointMake(glossRect.origin.x, glossRect.origin.y + glossRect.size.height); 
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0); 

    //Gradient Stroke Gloss Path  
    CGContextAddPath(context, glossPath); 
    CGContextSetLineWidth(context, 2); 
    CGContextReplacePathWithStrokedPath(context); 
    CGContextClip(context); 
    CGFloat colors2[] = 
    { 
     1, 1, 1, .3, 
     1, 1, 1, .1, 
     1, 1, 1, .0, 
    }; 
    CGFloat locations2[] = { 0, .1, 1.0 }; 
    CGGradientRef gradient2 = CGGradientCreateWithColorComponents(space, colors2, locations2, 3); 
    CGPoint startPoint2 = glossRect.origin; 
    CGPoint endPoint2 = CGPointMake(glossRect.origin.x, glossRect.origin.y + glossRect.size.height); 
    CGContextDrawLinearGradient(context, gradient2, startPoint2, endPoint2, 0); 

    //Cleanup 
    CGPathRelease(path); 
    CGPathRelease(glossPath); 
    CGColorSpaceRelease(space); 
    CGGradientRelease(gradient); 
    CGGradientRelease(gradient2); 
} 

this is the current callout i am getting

です。

それをこれらの2つの2行を間柱ことにより、私は

CGPathAddLineToPoint(path, NULL, parentX - 15, rect.origin.y + rect.size.height); 
     CGPathAddLineToPoint(path, NULL, parentX, rect.origin.y + rect.size.height + 15); 

を変更する必要があるが、私は、石英グラフィックに劣っています。

私の必要な吹き出しが三角形enter image description here

画像

以下のようなものですが吹き出しビューのアップに必要です。

クォーツのグラフィックに関する知識をお持ちの方にお手伝いください。

+0

2質問:1冒頭にのみ表示される吹き出し三角形を探していますか、またはいくつかの基準によって4つの可能な位置のいずれかに表示したいですか? 2.「self relativeParentXPosition」;とは何ですか?それは何を返しますか? –

答えて

1

吹き出しが上になるように探している場合は、これを試してみてください。コールアウトバブルのパスを次のように置き換えます。

//Determine Size 
    rect = self.bounds; 
    rect.size.width -= stroke + 14; 
    rect.size.height -= stroke + 14 + CalloutMapAnnotationViewHeightAboveParent; 
    rect.origin.x += stroke/2.0 + 7; 
    rect.origin.y += stroke/2.0 + 7 + 14;  

//Create Path For Callout Bubble 
     CGPathMoveToPoint(path, NULL, rect.origin.x, rect.origin.y + radius); 
     CGPathAddLineToPoint(path, NULL, rect.origin.x, rect.origin.y + rect.size.height - radius); //Left line 
     CGPathAddArc(path, NULL, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, radius, 0.0f, M_PI/2, 1); //Bottom left arc 
    // CGPathAddLineToPoint(path, NULL, parentX - 15, rect.origin.y + rect.size.height); //bottom to callout line 
    // CGPathAddLineToPoint(path, NULL, parentX, rect.origin.y + rect.size.height + 15); //callout left 
    // CGPathAddLineToPoint(path, NULL, parentX + 15, rect.origin.y + rect.size.height); //callout right 
     CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height); //bottom to right line 
     CGPathAddArc(path, NULL, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height - radius, radius, M_PI/2, 0.0f, 1); //bottom right 
     CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y + radius); //right line 
     CGPathAddArc(path, NULL, rect.origin.x + rect.size.width - radius, rect.origin.y + radius, radius, 0.0f, -M_PI/2, 1); //top right 
     CGPathAddLineToPoint(path, NULL, parentX + 15, rect.origin.y); //top right to right callout 
     CGPathAddLineToPoint(path, NULL, parentX, rect.origin.y - 15); //callout right 
     CGPathAddLineToPoint(path, NULL, parentX - 15, rect.origin.y); //callout left 
     CGPathAddLineToPoint(path, NULL, rect.origin.x + radius, rect.origin.y); //top left callout to top left 
     CGPathAddArc(path, NULL, rect.origin.x + radius, rect.origin.y + radius, radius, -M_PI/2, M_PI, 1); //top left 
     CGPathCloseSubpath(path); //Path closed, no line draw 

これはうまくいくはずです(テストしていないと思います)。しかし、光沢のパスは、使用されているコールアウト矩形に依存しているので、他のすべてがうまく動作するはずです。

コアグラフィックスには、紙面に描いたようなパスが描かれています。 CGPathMoveToPointには、鉛筆を置くことが含まれます。それ以降は線や円弧を連続して描きます。あなたがする必要があるのは、線/曲線が描画されている点を読み取ることだけです。円弧は曲線に変換されますが、円、π/ 2、π/ 4が単位円上のどこにあるかを知る必要があります。これは、アークが描かれている場所を示します。しかし、一般的に、(少なくともこの場合は)前の行が描画された実際のコーナーにアークが対応していると仮定することができます。どのパス要素がボックスのどの部分に対応しているかを示すコメントを追加しました。私はまた、それを削除するのではなく、一番下の吹き出しをコメントアウトしました。それをコメント解除して、もう一度下の吹き出しを描画することができますが、吹き出しをコメントアウトして矩形を元に戻す(私は14だけ下に移動した)ことを覚えておく必要があります。また、コールアウト(15)の大きさのように、「マジックナンバー」を使用して回避を検討し、代わりにあなたのファイルにこのようなものを置く:これを行うと、コードにCalloutSizeを挿入

#define CalloutSize 15.0f

ではなく15できるようになります使用されている各インスタンスを検索することなく簡単にサイズを変更することができます。

+0

Aaron Haymanさん、ありがとうございました。これは私に多くの手助けをします。 –

+0

問題ないです。 :) –

関連する問題