2015-06-05 55 views
0

我想创建一个带阴影的半圆UIView。保留蒙面UIView的阴影

我所做的是:

  1. 创建的UIView。
  2. 设置它的backgroundColor白
  3. 设置CornerRadius于是成了一个圆圈
  4. 使用view.layer.shadow性能

    circleView.backgroundColor = UIColor.whiteColor(); 
    
    circleView.layer.cornerRadius = baseRoundView.bounds.height/2 ; 
    
    circleView.layer.shadowColor = UIColor.blackColor().CGColor; 
    circleView.layer.shadowOffset = CGSize(width: 0, height: -3); 
    circleView.layer.shadowOpacity = 0.1; 
    circleView.layer.shadowRadius = 1; 
    

    添加阴影给它,但这些措施只得到了我一个完整的圆有阴影的UIVIEW。

Full Circle

所以我试图掩饰下半部

var maskRect: CGRect = CGRect(x: 0, y: 0), width: self.circleView.bounds.width, height: self.circleView.bounds.height/2); 
    var path = CGPathCreateWithRect(maskRect, nil); 

    maskLayer.path = path; 

    circleView.layer.mask = maskLayer; 

这些措施运作良好,但我失去了顶部阴影 issing Shadow

我期待什么有

Expectation

任何想法?

+1

阴影的偏移量在y上是-3。所以如果我没有记错的话,它会扩展真正的大小('clipsToBounds'为'false')。所以你也必须在面具上做-3(也可能在高度上+3)。 – Larme

+0

谢谢Larme,那正是我需要的!用您的答案更新了问题 – JayVDiyk

回答

0

作为Larme pointed out in the comment,所有我必须做的是调整在Y maskLayer的位置覆盖阴影。

var maskRect: CGRect = CGRect(x: 0, y: -3), width: self.circleView.bounds.width, height: self.circleView.bounds.height/2 + 3 ); 
-1

我为您建立以下的形象,我希望这是你在找什么

enter image description here

//// Color Declarations 
let color = UIColor(red: 0.800, green: 0.320, blue: 0.320, alpha: 1.000) 

//// Shadow Declarations 
let shadow = NSShadow() 
shadow.shadowColor = UIColor.blackColor().colorWithAlphaComponent(0.6) 
shadow.shadowOffset = CGSizeMake(5.1, 5.1) 
shadow.shadowBlurRadius = 5 

//// Bezier Drawing 
var bezierPath = UIBezierPath() 
bezierPath.moveToPoint(CGPointMake(168, 59)) 
bezierPath.addLineToPoint(CGPointMake(82, 59)) 
bezierPath.addCurveToPoint(CGPointMake(91.82, 31.64), controlPoint1: CGPointMake(82, 48.61), controlPoint2: CGPointMake(85.69, 39.08)) 
bezierPath.addCurveToPoint(CGPointMake(125, 16), controlPoint1: CGPointMake(99.71, 22.09), controlPoint2: CGPointMake(111.64, 16)) 
bezierPath.addCurveToPoint(CGPointMake(168, 59), controlPoint1: CGPointMake(148.75, 16), controlPoint2: CGPointMake(168, 35.25)) 
bezierPath.closePath() 
CGContextSaveGState(context) 
CGContextSetShadowWithColor(context, shadow.shadowOffset, shadow.shadowBlurRadius, (shadow.shadowColor as! UIColor).CGColor) 
color.setFill() 
bezierPath.fill() 
CGContextRestoreGState(context) 

color.setStroke() 
bezierPath.lineWidth = 1 
bezierPath.stroke() 
+0

感谢您花时间写出答案,但是,我想掩盖UIView,而不是绘制半圈。因为我想稍后对它进行一些动画 – JayVDiyk