无法清除Quartz 2D阴影

时间:2014-09-17 05:03:24

标签: objective-c macos cocoa core-graphics quartz-2d

我有一个需要投影的NSScrollview子类。滚动视图根据用户交互显示和消失。当NSScrollView出现时,它会显示阴影,这很棒。然而,当我隐藏视图时,阴影拒绝隐藏/清除。

这是NSScrollView子类的代码:

-(void)drawRect:(NSRect)dirtyRect {
CGContextRef currentContext = [[NSGraphicsContext currentContext] graphicsPort];

NSBezierPath *newClipPath = [NSBezierPath bezierPathWithRect: NSInsetRect( [self bounds], -10, -10)];
[newClipPath setClip];

if (!self.isShadowRendered) {
    DrawShadow(currentContext, self.frame);

    self.isShadowRendered = YES;
} else {
    [NSGraphicsContext saveGraphicsState];
    CGContextClearRect([[NSGraphicsContext currentContext] graphicsPort],self.frame);
    [NSGraphicsContext restoreGraphicsState];
}
}

- (void)setHidden:(BOOL)flag {
  [super setHidden:flag];

   if (!flag) {
       self.isShadowRendered = NO;        
   }
}

void DrawShadow (CGContextRef myContext, CGRect frame) {
CGSize myShadowOffset = CGSizeMake (0,  0);

CGContextSaveGState(myContext);

CGContextSetShadow (myContext, myShadowOffset, 10);

CGContextFillRect (myContext, CGRectMake (0, 0 , frame.size.width, frame.size.height));

CGContextRestoreGState(myContext);
}

如何让阴影与视图一起消失?

0 个答案:

没有答案