How to draw a “speech bubble” on an iPhone?

后端 未结 11 1190
礼貌的吻别
礼貌的吻别 2020-11-29 16:34

I\'m trying to get a \"speech bubble\" effect similar to the one in Mac OS X when you right click on something in the dock. Here\'s what I have now:

11条回答
  •  难免孤独
    2020-11-29 17:04

    See the triangle on the pop up menu in the image below, thats drawn with Core Graphics funcs and is completely scalable.

    alt text

    Done like this to do an equilateral triangle (old-school function names, sorry):

    #define triH(v) (v * 0.866)    
    
    func(CGContextRef inContext, CGRect arrowRect, CustomPushButtonData* controlData) {
    // Draw the triangle
    float   arrowXstart, arrowYstart;
    float   arrowXpos, arrowYpos, arrowHpos; 
    
    if (controlData->controlEnabled && controlData->controlActive) {
    
        CGContextSetRGBFillColor(inContext, 0., 0., 0., 1.);
    
    } else {
    
        CGContextSetRGBFillColor(inContext, 0., 0., 0., 0.5);
    
    }
    
    arrowHpos = triH(arrowRect.size.height);
    
    // Point C
    
    CGContextBeginPath(inContext);
    
    arrowXstart = arrowXpos = (arrowRect.origin.x + ((float)(arrowRect.size.width / 2.) - (arrowSize / 2.)));
    
    arrowYstart = arrowYpos = (arrowRect.origin.y + (float)((arrowRect.size.height / 2.) - (float)(arrowHpos / 2.)));
    
    CGContextMoveToPoint(inContext, arrowXpos, arrowYpos);
    
    // Point A
    
    arrowXpos += arrowSize;
    
    CGContextAddLineToPoint(inContext, arrowXpos, arrowYpos);
    
    // Point B
    
    arrowYpos += arrowHpos;
    
    arrowXpos -= (float)(arrowSize / 2.0);
    
    CGContextAddLineToPoint(inContext, arrowXpos, arrowYpos);
    
    // Point C
    CGContextAddLineToPoint(inContext, arrowXstart, arrowYstart);
    
    CGContextClosePath(inContext);
    
    CGContextFillPath(inContext);
    

    }

    Note that the triH(x) func is an optimized formula for calculating the height of an equitlateral triangle e.g. h = 1/2 * sqrt(3) * x . Since 1/2 * sqrt(3) never changes, I optimized it into that define.

提交回复
热议问题