2013-12-08 4 views
2

Я пытаюсь нарисовать UIBezierPathShape в iOS7, а затем применить тень. Это прекрасно работает, за исключением того, что, когда я курсирую по пути, инсульт появляется за фигурой. Как я могу это исправить?Тень UIBezierPath: как скрыть тень инсульта?

Код:

- (void)drawDiamondWithCount:(NSUInteger)count inRect:(CGRect)rect { 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    UIGraphicsPushContext(ctx); 
    UIEdgeInsets insets = UIEdgeInsetsMake(cardEdgeInsetTop, cardEdgeInsetRight, cardEdgeInsetBottom, cardEdgeInsetLeft); 
    CGRect insetsRect = UIEdgeInsetsInsetRect(rect, insets); 

    CGFloat shapeHeight = insetsRect.size.height/(double) count; 
    CGRect shapeRect; 
    for (NSUInteger i = 0; i < count; ++i) { 
     // Get the rect for the single shape 
     int numRemainingShapes = count - i - 1; 
     CGFloat remainingBottomSpace = numRemainingShapes * shapeHeight; 
     insets = UIEdgeInsetsMake(i * shapeHeight + shapeEdgeInsets, 0, remainingBottomSpace + shapeEdgeInsets, 0); 
     shapeRect = UIEdgeInsetsInsetRect(insetsRect, insets); 
     UIBezierPath *path = [self getDiamondPath:shapeRect]; 
     [[UIColor redColor] setFill]; 
     [[UIColor blackColor] setStroke]; 
     UIGraphicsPushContext(ctx); 
     CGContextSetShadow(ctx, CGSizeMake(5, 2), 5); 
     [path fill]; 
     UIGraphicsPopContext(); 
     //[path stroke]; 
    } 
    UIGraphicsPopContext(); 
} 

Это дает мне то, что я хочу, минус тактный This gives me what I want, minus the stroke

раскомментировав [path stroke] дает мне это. Я хочу удар, но не хочу видеть его за фигурой.

enter image description here

ответ

2

Я подозреваю, что вместо UIGraphicsPushContext и UIGraphicsPopContext, я думаю, что вы хотите CGContextSaveGState и CGContextRestoreGState:

// create context and configure 

CGContextRef ctx = UIGraphicsGetCurrentContext(); 
[[UIColor redColor] setFill]; 
[[UIColor blackColor] setStroke]; 

// create path 

UIBezierPath *path = ...; 
path.lineJoinStyle = kCGLineJoinMiter; 
path.lineWidth = 2.0; 

// fill the center with shadow 

CGContextSaveGState(ctx); 
CGContextSetShadow(ctx, CGSizeMake(5, 2), 5); 
[path fill]; 
CGContextRestoreGState(ctx); 

// stroke border without shadow 

CGContextSetLineWidth(ctx, 2.0); 
[path stroke]; 

С UIGraphicsPushContext и UIGraphicsPopContext вы получите:

line shadow

С CGContextSaveGState и CGContextRestoreGState вы получите:

enter image description here

+0

Для уточнения для моего будущего я: UIGraphicsPushContext, когда у вас есть новый контекст для замены существующего, CGContextSaveGState, если вы хотите сохранить копию текущего контекста. –

Смежные вопросы