用阴影包围UIView?

时间:2009-08-04 01:38:14

标签: iphone objective-c cocoa-touch uikit core-graphics

所以使用这个链接: How do I draw a shadow under a UIView?

这个链接: http://iphonedevelopment.blogspot.com/2008/11/creating-transparent-uiviews-rounded.html

我遇到了这个实现:

- (void)drawRect:(CGRect)rect {

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSaveGState(context);
CGContextSetShadow(context, CGSizeMake(-15.0f, 20.0f), 5.0f);

CGContextSetLineWidth(context, strokeWidth);
CGContextSetStrokeColorWithColor(context, self.strokeColor.CGColor);
CGContextSetFillColorWithColor(context, self.rectColor.CGColor);

CGRect rrect = self.bounds;

CGFloat radius = cornerRadius;
CGFloat width = CGRectGetWidth(rrect);
CGFloat height = CGRectGetHeight(rrect);


// Make sure corner radius isn't larger than half the shorter side
if (radius > width/2.0)
    radius = width/2.0;
if (radius > height/2.0)
    radius = height/2.0;    

CGFloat minx = CGRectGetMinX(rrect);
CGFloat midx = CGRectGetMidX(rrect);
CGFloat maxx = CGRectGetMaxX(rrect);
CGFloat miny = CGRectGetMinY(rrect);
CGFloat midy = CGRectGetMidY(rrect);
CGFloat maxy = CGRectGetMaxY(rrect);
CGContextMoveToPoint(context, minx, midy);
CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);

CGContextRestoreGState(context);

}

然而,当它绘制时,阴影被剪切到视图的边界。我试过设置self.clipsToBounds = NO但是这不会影响问题。

1 个答案:

答案 0 :(得分:3)

如何使用Quartz Core绘制阴影呢?类似的东西:

view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOffset = CGSizeMake(0, 0);
view.layer.shadowRadius = 4;
view.layer.shadowOpacity = 1;
view.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:view.frame cornerRadius:11].CGPath; // make sure you set that for better performance