2016-06-09 36 views
0

我使用下面的代碼繪製trinagle形狀,我想爲我使用bezierpath繪製的此形狀應用陰影。向uibiezierpath顯示shdow

CGContextRef context = UIGraphicsGetCurrentContext(); 
    //creating a new nsshadow 
    NSShadow* shadow = [[NSShadow alloc] init]; 
    [shadow setShadowColor: UIColor.blackColor]; 
    [shadow setShadowOffset: CGSizeMake(3.1, 3.1)]; 
    [shadow setShadowBlurRadius: 5];  

    //view to add a bezier path 
    UIView *triangleView = [[UIView alloc] initWithFrame:CGRectMake(67.0f,33.4f, 40.0f ,20.0f)]; 

    // code for drawing a trinagular shape 
    UIBezierPath* trianglePath = [UIBezierPath bezierPath]; 
    [trianglePath moveToPoint:CGPointMake(0, 0)]; 

    [trianglePath addQuadCurveToPoint:CGPointMake(7.0f, 3.0f) controlPoint:CGPointMake(4.0f, 0.0f)]; 

    [trianglePath addLineToPoint:CGPointMake(17.0f,15.0f)]; 

    [trianglePath addQuadCurveToPoint:CGPointMake(23.0f,15.0f) controlPoint:CGPointMake(20.0f, 20.0f)]; 

    [trianglePath addLineToPoint:CGPointMake(33.0f, 3.0f)]; 

    [trianglePath addQuadCurveToPoint:CGPointMake(40.0, 0.0f) controlPoint:CGPointMake(36.5f, 0.0f)]; 
    [trianglePath addLineToPoint:CGPointMake(0.0f, 0.0f)]; 


    CGContextAddPath(context, trianglePath.CGPath);  
    CGContextSaveGState(context); 
    CGContextSetShadowWithColor(context, shadow.shadowOffset, shadow.shadowBlurRadius, [shadow.shadowColor CGColor]); 
    [UIColor.redColor setFill]; 
    [trianglePath fill]; 
    CGContextRestoreGState(context); 

    [UIColor.blackColor setStroke]; 
    trianglePath.lineWidth = 1; 
    [trianglePath stroke]; 
    CGContextFillPath(context); 

    //CAShapeLayer to set the bezier path 
    CAShapeLayer *triangleMaskLayer = [CAShapeLayer layer]; 
    [triangleMaskLayer setPath:trianglePath.CGPath]; 

    triangleMaskLayer.lineWidth = 0.1f; 
    triangleMaskLayer.fillColor = [[UIColor whiteColor] CGColor]; 

    [triangleView.layer addSublayer:triangleMaskLayer]; 

我想爲這個uibezier路徑設置一個陰影嗎?我嘗試過使用某些方法,但無法弄清楚什麼是錯誤的?

回答

0

嘗試像下面陰影屬性設置爲CAShapeLayer

CAShapeLayer *triangleMaskLayer = [CAShapeLayer layer]; 

    triangleMaskLayer.shadowColor=[UIColor blackColor].CGColor; 
    triangleMaskLayer.shadowOffset=CGSizeMake(3.1, 3.1); 
    triangleMaskLayer.shadowRadius=5.0; 

    [triangleView.layer addSublayer:triangleMaskLayer]; 
0

SWIFT 3 您可以先創建一個陰影圖層,然後您可以添加您shapelayer因爲這shadowlayer的子層。其他人很直接。

let triangleView = UIView(frame: CGRect(x: 67, y: 33, width: 40, height: 20)) 

    let trianglePath = UIBezierPath() 
    trianglePath.move(to: CGPoint(x:0, y:0)) 
    trianglePath.addQuadCurve(to: CGPoint(x:7, y:3), controlPoint: CGPoint(x:4, y:0)) 
    trianglePath.addLine(to: CGPoint(x:17, y:15)) 
    trianglePath.addQuadCurve(to: CGPoint(x:23, y:15), controlPoint: CGPoint(x:20, y:20)) 
    trianglePath.addLine(to: CGPoint(x:33, y:3)) 
    trianglePath.addQuadCurve(to: CGPoint(x:40, y:0), controlPoint: CGPoint(x:36, y:0)) 
    trianglePath.addLine(to: CGPoint(x:0, y:0)) 
    trianglePath.close() 


    //CAShapeLayer to set the bezier path 
    let triangleMaskLayer = CAShapeLayer() 
    triangleMaskLayer.path = trianglePath.cgPath 
    triangleMaskLayer.lineWidth = 1 
    triangleMaskLayer.strokeColor = UIColor.black.cgColor 
    triangleMaskLayer.fillColor = UIColor.red.cgColor 


    let shadowlayer = CALayer() 
    shadowlayer.frame = CGRect(x: 100, y: 100, width: 200, height: 200) 
    shadowlayer.shadowColor = UIColor.black.cgColor 
    shadowlayer.shadowRadius = 5.0 
    shadowlayer.shadowOffset = CGSize(width: 3.1, height: 3.1) 
    shadowlayer.shadowOpacity = 1.0 

    shadowlayer.addSublayer(triangleMaskLayer) 
    triangleView.layer.addSublayer(shadowlayer) 
    self.view .addSubview(triangleView)