2013-08-23 50 views
12

我正在努力创造一个圈子UIImageView,它的工作原理。下面是我用做它的方式:如何将阴影添加到圆UIImageView或UIView?

[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; 

回答

34

使用CALayershadowPath property与圆角的矩形

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

编辑

添加UIBezierPath对于方十岁上下图像视图这种技术不能直接工作,因为如你所说,图像视图回到了正方形。原因:您设置clipsToBounds = NO以显示删除角落半径裁剪的阴影,其中imageViewcontainer的子视图。

解决方法:
将您的imageview添加到容器视图中,然后将图层阴影应用于此容器。以下是我试过的代码。

[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; 

所得到的效果如截图所示,

enter image description here

希望帮助!

+1

我已将您建议的代码行添加到我的代码中,但它不起作用。你能详细说明如何利用上面的代码吗?谢谢。 – Newbie

+0

@Newbie检查我的编辑。 – Amar

+0

@Newbie有帮助吗? – Amar

-4
yourImageView.layer.masksToBounds = NO; 
yourImageView.layer.shadowOffset = CGSizeMake(5, 0); 
yourImageView.layer.shadowRadius = 5; 
yourImageView.layer.shadowOpacity = 0.5; 
yourImageView.layer.shadowPath = [UIBezierPath bezierPathWithRect:yourImageView.bounds].CGPath; 

试试这个代码...

+0

它不工作:( – Newbie

2

如果没有一个容器,但与此背景视图是我的2美分

作为一个迅速扩展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) 
    } 
} 
+0

伟大的解决方案,但我不知道为什么它不工作的情况下7+ –

2

在任何人寻找斯威夫特3或4工作解决方案:

let imageSize: CGFloat = 64.0 

    // Create a container which has a shadow 
    let imageCotainer = UIView(frame: CGRect(x: 0, y: 0, width: imageSize, height: imageSize)) 
    imageCotainer.clipsToBounds = false 
    imageCotainer.layer.shadowColor = UIColor.black.cgColor 
    imageCotainer.layer.shadowOpacity = 0.2 
    imageCotainer.layer.shadowOffset = CGSize(width: 0, height: 1) 
    imageCotainer.layer.shadowRadius = 2 

    // Create an image view that will be inserted into the container view 
    let imageView = UIImageView(frame: imageCotainer.bounds) 
    imageView.image = yourImage 
    imageView.clipsToBounds = true 
    let cornerRadius = imageView.frame.height/2 
    imageView.layer.cornerRadius = cornerRadius 

    // Draw a shadow 
    imageCotainer.layer.shadowPath = UIBezierPath(roundedRect: imageCotainer.bounds, cornerRadius: cornerRadius).cgPath 
    // Add image into container 
    imageCotainer.addSubview(imageView) 

有时你也需要设置容器的内部图像的限制,但它可能在某些情况下,没有它工作了。但如果不是,请添加:

// Set constraints for the image inside the container view 
    imageView.translatesAutoresizingMaskIntoConstraints = false 
    imageView.topAnchor.constraint(equalTo: imageCotainer.topAnchor).isActive = true 
    imageView.leftAnchor.constraint(equalTo: imageCotainer.leftAnchor).isActive = true 
    imageView.rightAnchor.constraint(equalTo: imageCotainer.rightAnchor).isActive = true 
    imageView.bottomAnchor.constraint(equalTo: imageCotainer.bottomAnchor).isActive = true 
    imageView.heightAnchor.constraint(equalToConstant: imageSize).isActive = true 
    imageView.widthAnchor.constraint(equalToConstant: imageSize).isActive = true