CMPopTipView icon indicating copy to clipboard operation
CMPopTipView copied to clipboard

PopTip without pointing somewhere

Open SjoerdPerfors opened this issue 12 years ago • 2 comments

This can come handy when you want to show a poptip to a mkmapview or a global one for a screen. So my goal was to get a poptip without the arrow.

To archive this I added to the enum:

typedef enum {
    PointDirectionAny = 0,
    PointDirectionUp,
    PointDirectionDown,
    PointDirectionGoneButDown,
    PointDirectionGoneButUp
} PointDirection;

Then in - (void)drawRect:(CGRect)rect {

for example the PointDirectionDown:

else {
    if(_pointDirection == PointDirectionGoneButDown)
        CGPathMoveToPoint(bubblePath, NULL, _targetPoint.x+_sidePadding, _targetPoint.y - _pointerSize);
    else
        CGPathMoveToPoint(bubblePath, NULL, _targetPoint.x+_sidePadding, _targetPoint.y);

SjoerdPerfors avatar Nov 22 '13 08:11 SjoerdPerfors

Hi SjoerdPerfors.

I can't get it to work with the code you provided. Can you please provide the complete code that handles not having the pointers?

Thanks! Raphael

rpungin avatar Apr 01 '14 20:04 rpungin

Sorry Raphael, I forgot the following.

Change in method

  • (void)presentPointingAtView:(UIView *)targetView inView:(UIView *)containerView animated:(BOOL)animated

the following: if (_pointDirection == PointDirectionDown || _pointDirection == PointDirectionGoneButDown) see snipper below

if (targetRelativeOrigin.y+targetView.bounds.size.height < containerRelativeOrigin.y) {
    pointerY = 0.0;
    _pointDirection = PointDirectionUp;
}
else if (targetRelativeOrigin.y > containerRelativeOrigin.y+containerView.bounds.size.height) {
    pointerY = containerView.bounds.size.height;
    _pointDirection = PointDirectionDown;
}
else {
    _pointDirection = _preferredPointDirection;
    CGPoint targetOriginInContainer = [targetView convertPoint:CGPointMake(0.0, 0.0) toView:containerView];
    CGFloat sizeBelow = containerView.bounds.size.height - targetOriginInContainer.y;
    if (_pointDirection == PointDirectionAny) {
        if (sizeBelow > targetOriginInContainer.y) {
            pointerY = targetOriginInContainer.y + targetView.bounds.size.height;
            _pointDirection = PointDirectionUp;
        }
        else {
            pointerY = targetOriginInContainer.y;
            _pointDirection = PointDirectionDown;
        }
    }
    else {
        if (_pointDirection == PointDirectionDown || _pointDirection == PointDirectionGoneButDown) {
            pointerY = targetOriginInContainer.y;
        }
        else {
            pointerY = targetOriginInContainer.y + targetView.bounds.size.height;
        }
    }
}

My top lines of the drawRect method looks like this (I didnt implement the PointDirectionGoneButUp):

- (void)drawRect:(CGRect)rect {

    CGRect bubbleRect = [self bubbleFrame];

    CGContextRef c = UIGraphicsGetCurrentContext(); 

    CGContextSetRGBStrokeColor(c, 0.0, 0.0, 0.0, 1.0);  // black
    CGContextSetLineWidth(c, self.borderWidth);

    CGMutablePathRef bubblePath = CGPathCreateMutable();

    if (_pointDirection == PointDirectionUp) {
        CGPathMoveToPoint(bubblePath, NULL, _targetPoint.x+_sidePadding, _targetPoint.y);
        CGPathAddLineToPoint(bubblePath, NULL, _targetPoint.x+_sidePadding+_pointerSize, _targetPoint.y+_pointerSize);

        CGPathAddArcToPoint(bubblePath, NULL,
                            bubbleRect.origin.x+bubbleRect.size.width, bubbleRect.origin.y,
                            bubbleRect.origin.x+bubbleRect.size.width, bubbleRect.origin.y+_cornerRadius,
                            _cornerRadius);
        CGPathAddArcToPoint(bubblePath, NULL,
                            bubbleRect.origin.x+bubbleRect.size.width, bubbleRect.origin.y+bubbleRect.size.height,
                            bubbleRect.origin.x+bubbleRect.size.width-_cornerRadius, bubbleRect.origin.y+bubbleRect.size.height,
                            _cornerRadius);
        CGPathAddArcToPoint(bubblePath, NULL,
                            bubbleRect.origin.x, bubbleRect.origin.y+bubbleRect.size.height,
                            bubbleRect.origin.x, bubbleRect.origin.y+bubbleRect.size.height-_cornerRadius,
                            _cornerRadius);
        CGPathAddArcToPoint(bubblePath, NULL,
                            bubbleRect.origin.x, bubbleRect.origin.y,
                            bubbleRect.origin.x+_cornerRadius, bubbleRect.origin.y,
                            _cornerRadius);
        CGPathAddLineToPoint(bubblePath, NULL, _targetPoint.x+_sidePadding-_pointerSize, _targetPoint.y+_pointerSize);
    }
    else {
        if(_pointDirection == PointDirectionGoneButDown)
            CGPathMoveToPoint(bubblePath, NULL, _targetPoint.x+_sidePadding, _targetPoint.y - _pointerSize);
        else
            CGPathMoveToPoint(bubblePath, NULL, _targetPoint.x+_sidePadding, _targetPoint.y);
        CGPathAddLineToPoint(bubblePath, NULL, _targetPoint.x+_sidePadding-_pointerSize, _targetPoint.y-_pointerSize);

        CGPathAddArcToPoint(bubblePath, NULL,
                            bubbleRect.origin.x, bubbleRect.origin.y+bubbleRect.size.height,
                            bubbleRect.origin.x, bubbleRect.origin.y+bubbleRect.size.height-_cornerRadius,
                            _cornerRadius);
        CGPathAddArcToPoint(bubblePath, NULL,
                            bubbleRect.origin.x, bubbleRect.origin.y,
                            bubbleRect.origin.x+_cornerRadius, bubbleRect.origin.y,
                            _cornerRadius);
        CGPathAddArcToPoint(bubblePath, NULL,
                            bubbleRect.origin.x+bubbleRect.size.width, bubbleRect.origin.y,
                            bubbleRect.origin.x+bubbleRect.size.width, bubbleRect.origin.y+_cornerRadius,
                            _cornerRadius);
        CGPathAddArcToPoint(bubblePath, NULL,
                            bubbleRect.origin.x+bubbleRect.size.width, bubbleRect.origin.y+bubbleRect.size.height,
                            bubbleRect.origin.x+bubbleRect.size.width-_cornerRadius, bubbleRect.origin.y+bubbleRect.size.height,
                            _cornerRadius);
        CGPathAddLineToPoint(bubblePath, NULL, _targetPoint.x+_sidePadding+_pointerSize, _targetPoint.y-_pointerSize);
    }

SjoerdPerfors avatar Apr 02 '14 07:04 SjoerdPerfors