2016-05-14 56 views
5

我有下面一段代码圆一个视图只有特定的角落:和圆形边角具体显示在特定边阴影只

- (void)roundOnlySpecifiedCornersInView:(UIView *)view corners:(UIRectCorner)corners 
{ 
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:(corners) cornerRadii:CGSizeMake(4.0, 4.0)]; 
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 

    maskLayer.path = maskPath.CGPath; 
    view.layer.mask = maskLayer; 
} 

这工作完全孤立。现在我也想影子在我的观点,但我特别希望在不同的情况下,申请阴影:除了底部

  • 各方

    • 四面八方
    • 各方除顶部
    • 左/右侧只有

    我遇到的所有技巧都是通过创建视图的插图来实现的。与此相关的问题是,如果您只想保留左侧/右侧的阴影,则会抵消底部和顶部。由于Rect现在不高,因此左侧和右侧的阴影不能覆盖视图的整个高度。而且,用于倒角的掩模层会导致阴影不再出现。

    此示例代码:

    innerView.layer.shadowColor = [[UIColor colorWithWhite:0.0f alpha:0.1f] CGColor]; 
        innerView.layer.shadowOpacity = 1.0f; 
        innerView.layer.shadowOffset = CGSizeMake(0.0f, 0.0f); 
        innerView.layer.shadowRadius = 6.0f; 
    
        CGRect shadowFrame = UIEdgeInsetsInsetRect(innerView.bounds, UIEdgeInsetsMake(9, 0, 9, 0)); 
        CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:shadowFrame].CGPath; 
    
        innerView.layer.shadowPath = shadowPath; 
    

    如何我只能在特定圆边边角特定视图中,并在同一时间显示的影子呢?

    在Swift中的答案也很感谢!

    截图的我想要的东西(这一个是容易的,因为各个角落需要进行四舍五入,所以我可以用.layer.cornerRadius,它在各方面的有阴影): enter image description here

    现在我只想只有2圆角落(左上角和右上角,左下角和右下角)并且只在一些边添加阴影。

  • +0

    你可以显示你有什么/想要的截图吗? – jtbandes

    +0

    http://stackoverflow.com/questions/22679886/how-to-make-a-uiview-with-optional-rounded-corners-and-border/22680538#22680538 –

    +0

    @KiritModi你指的是完全相同的代码我已经有了圆角。 – edwardmp

    回答

    3

    我不确定它是否符合您的要求。代码创建一个带有顶部和底部阴影的图像,以及所有圆角,您可以修改代码以实现所需。你可以使用图像作为你的手机背景(看起来它是UITableViewCell

    让我知道它是否不适合你。

    的图像:

    enter image description here

    // create a shadow image 
    CGSize size = CGSizeMake(ScreenWidth, ScreenWidth); 
    UIGraphicsBeginImageContextWithOptions(size, NO, 0); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    
    UIColor *backgroundColor = [UIColor colorWithRed:246.0/255.0 green:246.0/255.0 blue:246.0/255.0 alpha:1.0]; 
    UIColor *fillColor = [UIColor whiteColor]; 
    CGRect rect = CGRectMake(10, 10, 100, 44); 
    
    // re-draw the background 
    CGContextSetFillColorWithColor(context, backgroundColor.CGColor); 
    CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height)); 
    
    // set top and bottom shadow 
    CGRect rectTop = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 5); 
    CGContextSaveGState(context); 
    CGContextSetShadowWithColor(context, CGSizeMake(0, -5), 5, [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor); 
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
    CGContextFillRect(context, rectTop); 
    CGContextRestoreGState(context); 
    
    CGRect rectBottom = CGRectMake(rect.origin.x, rect.origin.y+rect.size.height-5, rect.size.width, 5); 
    CGContextSaveGState(context); 
    CGContextSetShadowWithColor(context, CGSizeMake(0, 5), 5, [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor); 
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
    CGContextFillRect(context, rectBottom); 
    CGContextRestoreGState(context); 
    
    // re-draw the background 
    CGContextSetFillColorWithColor(context, backgroundColor.CGColor); 
    CGContextFillRect(context, rect); 
    
    CGContextSaveGState(context); 
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(4.0, 4.0)]; 
    [maskPath addClip]; 
    CGContextSetFillColorWithColor(context, fillColor.CGColor); 
    CGContextFillRect(context, rect); 
    CGContextRestoreGState(context); 
    

    您可以修改代码以获得左上阴影:

    enter image description here

    // create a shadow image 
    CGSize size = CGSizeMake(ScreenWidth, ScreenWidth); 
    UIGraphicsBeginImageContextWithOptions(size, NO, 0); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    
    UIColor *backgroundColor = [UIColor colorWithRed:246.0/255.0 green:246.0/255.0 blue:246.0/255.0 alpha:1.0]; 
    UIColor *fillColor = [UIColor whiteColor]; 
    CGRect rect = CGRectMake(10, 10, 100, 44); 
    
    // re-draw the background 
    CGContextSetFillColorWithColor(context, backgroundColor.CGColor); 
    CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height)); 
    
    // set top and left shadow 
    CGRect rectTop = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 5); 
    CGContextSaveGState(context); 
    CGContextSetShadowWithColor(context, CGSizeMake(0, -5), 5, [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor); 
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
    CGContextFillRect(context, rectTop); 
    CGContextRestoreGState(context); 
    
    CGRect rectLeft = CGRectMake(rect.origin.x, rect.origin.y, 5, rect.size.height); 
    CGContextSaveGState(context); 
    CGContextSetShadowWithColor(context, CGSizeMake(-5, 0), 5, [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor); 
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
    CGContextFillRect(context, rectLeft); 
    CGContextRestoreGState(context); 
    
    // re-draw the background 
    CGContextSetFillColorWithColor(context, backgroundColor.CGColor); 
    CGContextFillRect(context, rect); 
    
    CGContextSaveGState(context); 
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(4.0, 4.0)]; 
    [maskPath addClip]; 
    CGContextSetFillColorWithColor(context, fillColor.CGColor); 
    CGContextFillRect(context, rect); 
    CGContextRestoreGState(context); 
    

    HTH

    +0

    感谢您的回答。对于阴影,我只想念左右两边添加阴影,但我想我可以从你的答案中得出它。 – edwardmp

    -1

    特定角落组边角半径

    是指来自:how to set cornerRadius for only top-left and top-right corner of a UIView?

    UIBezierPath *maskPath; 
    maskPath = [UIBezierPathbezierPathWithRoundedRect:_backgroundImageView.bounds 
              byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) 
                cornerRadii:CGSizeMake(3.0, 3.0)]; 
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
    maskLayer.frame = self.bounds; 
    maskLayer.path = maskPath.CGPath; 
    _imageView.layer.mask = maskLayer; 
    [maskLayer release]; 
    
    +0

    您的代码与我在开场白中提供的代码几乎相同(同一个区块) – edwardmp

    1

    在你想用绘画的影子,当然要做到这一点很多,很多情况下。但是,您可以考虑采用以下方法:使用可伸缩图像,并在您需要的两侧放置阴影,例如,类似的东西(这其中有在各方面的影子,虽然):

    enter image description here

    你说就在:“Ewwwww他是使用图像的一个简单的阴影!”我要强调,这将工作在性能方面更好。例如,如果您在UICollectionView中有很多单元格,并且每个单元格都在重新绘制它的阴影,则在滚动过程中它可能会显着减慢您的应用程序的速度,而使用基于图像的阴影则会基本相同。

    我会更进一步,并建议您实际上可以使用可拉伸的图像掩盖视图与圆角,太!现在,这可能看起来有点过头了,但如果你注意到性能的急剧下降,我会给它一个镜头。你基本上需要做的是准备另一个带有透明“中间”部分和与背景颜色相同的圆角的可拉伸图像。喜欢的东西:

    enter image description here

    再次,你downvote做的事情可以用两行代码可以轻松实现这一离奇的方式之前...

    view.layer.cornerRadius = 20 
    view.layer.masksToBounds = true 
    

    ...让我点在这种情况下,当你必须同时显示一大堆被屏蔽的视图的时候,这可以更快地工作:例如,屏蔽UICollectionViewCell s。基本上,如果在UIView的图层上设置蒙版,该蒙版将要重新计算并实时应用,这会在频繁重绘UIView内容时在性能方面造成很大影响 - 滚动时例如,UITableViewUICollectionView

    显然,如果您的背景不是纯色,并且具有实例的渐变效果,则无效。但是,在许多其他情况下,这可能会帮助您实现更平滑的UI性能。在特定情况下的另一个优点是,您可以轻松控制要遮罩哪些角以及阴影应该放在哪里。

    再次,我并不是暗示这是每种情况下去的方式。我所说的是,在很多情况下,这可以显着提高性能:例如,如果屏蔽了同时在屏幕上呈现的太多视图,并且预计布局经常重新绘制。