圆形UIView与阴影? [英] Rounded UIView with Shadow?

查看:81
本文介绍了圆形UIView与阴影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以使用这个链接:
如何我在UIView下绘制阴影?



此链接:
http://iphonedevelopment.blogspot.com/2008/11/creating-transparent-uiviews-rounded.html p>

我来了这个实现:

   - (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);


//确保拐角半径不大于短边的一半
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,但这不影响问题。

解决方案

如何使用Quartz绘制阴影核心?像:

  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


So using this link: How do I draw a shadow under a UIView?

And this link: http://iphonedevelopment.blogspot.com/2008/11/creating-transparent-uiviews-rounded.html

I came upon this implementation:

- (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);

}

However when it draws, the shadow is clipped to the bounds of the view. I have tried setting self.clipsToBounds = NO however this doesn't affect the problem.

解决方案

How about drawing the shadow using Quartz Core instead? Something like:

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

这篇关于圆形UIView与阴影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆