2016-11-17 19 views
2

誰かが私に内側の影を与えない理由を教えてくれたり、内側の影を与える解決策を私に教えてもらえたら、本当にありがたいです。Swiftラウンドでインナーシャドウを作成する

丸みを帯びたUIViewに内側のシャドウを作成する必要があります。私は多くの答えを経験してきましたし、これを通常の四角形のUIViewで取得する方法を見つけましたが、丸みを帯びたビューで動作する解決策は見つかりませんでした。代わりに、以下に示すような解決策がわかりますが、実装する際には必要な内側の影を作成しないでください。 ここに私の画面があるが、それは私がに影を追加したい、外青と内側の黄色のビュー間の白図である。

outer

私はサブクラス化したビュー、ここに私のドローRECTコードです:

let innerShadow = CALayer() 
    // Shadow path (1pt ring around bounds) 
    let path = UIBezierPath(rect: innerShadow.bounds.insetBy(dx: -1, dy: -1)) 
    let cutout = UIBezierPath(rect: innerShadow.bounds).reversing() 
    path.append(cutout) 
    innerShadow.shadowPath = path.cgPath 
    innerShadow.masksToBounds = true 
    // Shadow properties 
    innerShadow.shadowColor = UIColor.darkGray.cgColor 
    innerShadow.shadowOffset = CGSize(width: 0.0, height: 7.0) 
    innerShadow.shadowOpacity = 1 
    innerShadow.shadowRadius = 5 
    // Add 
    self.layer.addSublayer(innerShadow) 

    // Make view round 
    self.layer.cornerRadius = self.frame.size.width/2 
    self.layer.masksToBounds = true 

多くのご協力ありがとうございました。ご質問がある場合はお知らせください。

答えて

0

だけ

は、上記青ビュー

let maskLayer = CAShapeLayer() 

    // based on the image the ring is 1/6 its diameter 
    let radius = self.bounds.width * 1.0/6.0 
    let path = UIBezierPath(rect: self.bounds) 
    let holeCenter = CGPoint(x: center.x - (radius * 2), y: center.y - (radius * 2)) 

    path.addArc(withCenter: holeCenter, radius: radius, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true) 

    maskLayer.path = path.cgPath 
    maskLayer.fillRule = kCAFillRuleEvenOdd 

    blueView.layer.mask = maskLayer 

の中心から円をマスク昨日これを見つけたはあなたに青いリングを提供します。

次は私たちの影青いビューと同じになるように、そのフレームを設定し

var blackView = UIView() 

として機能するblackViewを作成します。 blackView

let maskLayer = CAShapeLayer() 

    // This is the most important part, the mask shadow allows some of the black view to bleed from under the blue view and give a shadow 
    maskLayer.shadowOffset = CGSize(width: shadowX, height: shadowY) 
    maskLayer.shadowRadius = shadowRadius 

    let radius = self.bounds.width * 2.0/6.0 
    let path = UIBezierPath(rect: self.bounds) 
    let holeCenter = CGPoint(x: center.x - (radius * 2), y: center.y - (radius * 2)) 
    path.addArc(withCenter: holeCenter, radius: radius, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true) 

    maskLayer.path = path.cgPath 
    maskLayer.fillRule = kCAFillRuleEvenOdd 

    blackView.layer.mask = maskLayer 
から同様の穴を切り取り

blackView.frame = blueView.frame 
blackView.clipToBounds = true 

関連する問題