22

円を作ろうとしていますが、UIImageViewうまくいきます。以下は私がそれを作るために使用する方法です:

[self.pic.layer setMasksToBounds:YES];
[self.pic.layer setCornerRadius:50.0];

に影を追加したいと思いUIImageViewます。以下のコードは画像ビューに影を追加しますが、画像ビューは正方形に戻ります。誰かがこの問題を解決するための指針を教えてもらえますか? 以下は、影を追加するために使用するコードです。

self.pic.layer.shadowColor = [UIColor purpleColor].CGColor;
self.pic.layer.shadowOffset = CGSizeMake(0, 1);
self.pic.layer.shadowOpacity = 1;
self.pic.layer.shadowRadius = 1.0;
self.pic.clipsToBounds = NO;
4

6 に答える 6

51

CALayershadowPathプロパティを使用してUIBezierPath、角丸四角形を追加します

self.pic.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.pic.frame cornerRadius:50.0].CGPath;

編集

あなたが言ったように、画像ビューは正方形に戻るため、正方形のような画像ビューの場合、この手法は直接機能しません。理由:のサブビューであるclipsToBounds = NO角の半径のクリッピングを削除する影を表示するように設定しました。imageViewcontainer

回避策:
イメージビューをコンテナー ビューに追加してから、レイヤー シャドウをこのコンテナーに適用します。以下は私が試したコードです。

[self.imageView.layer setCornerRadius:60.0];
[self.imageView.layer setMasksToBounds:YES];
self.imageView.clipsToBounds = YES;

self.container.backgroundColor = [UIColor clearColor];
self.container.layer.shadowColor = [UIColor blackColor].CGColor;
self.container.layer.shadowOffset = CGSizeMake(5,15);
self.container.layer.shadowOpacity = 0.5;
self.container.layer.shadowRadius = 2.0;
self.container.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.container.bounds cornerRadius:100.0].CGPath;

結果として得られる効果は、スクリーンショットに示すとおりです。

ここに画像の説明を入力

于 2013-08-23T13:12:41.980 に答える
5

コンテナなしで背景ビューがある場合、ここに私の 2 セントがあります

Swift 2.2 拡張機能として

    image?.applyCircleShadow(5, shadowOpacity: 1)
extension UIView {
    func applyCircleShadow(shadowRadius: CGFloat = 2,
                           shadowOpacity: Float = 0.3,
                           shadowColor: CGColor = UIColor.blackColor().CGColor,
                           shadowOffset: CGSize = CGSize.zero) {
        layer.cornerRadius = frame.size.height / 2
        layer.masksToBounds = false
        layer.shadowColor = shadowColor
        layer.shadowOffset = shadowOffset
        layer.shadowRadius = shadowRadius
        layer.shadowOpacity = shadowOpacity
    }
}
extension UIImageView {
    override func applyCircleShadow(shadowRadius: CGFloat = 2,
                                    shadowOpacity: Float = 0.3,
                                    shadowColor: CGColor = UIColor.blackColor().CGColor,
                                    shadowOffset: CGSize = CGSize.zero) {

        // Use UIImageView.hashvalue as background view tag (should be unique)
        let background: UIView = superview?.viewWithTag(hashValue) ?? UIView()
        background.frame = frame
        background.backgroundColor = backgroundColor
        background.tag = hashValue
        background.applyCircleShadow(shadowRadius, shadowOpacity: shadowOpacity, shadowColor: shadowColor, shadowOffset: shadowOffset)
        layer.cornerRadius = background.layer.cornerRadius
        layer.masksToBounds = true
        superview?.insertSubview(background, belowSubview: self)
    }
}
于 2016-09-02T08:55:02.237 に答える