简体   繁体   English

特殊投影:Swift 2

[英]Special drop shadow: Swift 2

In the image below, I have two types of drop shadows. 在下图中,我有两种阴影。

In swift, I've used this function: 快速地,我使用了以下功能:

func applyShadow(view: UIView){
    view.layer.shadowColor = UIColor.blackColor().CGColor
    view.layer.shadowOffset = CGSizeMake(0, 1)
    view.layer.shadowOpacity = 0.5
    view.layer.shadowRadius = 10.0
    view.clipsToBounds = false
    view.layer.masksToBounds = false
}

to apply the shadow to the image in the image view. 将阴影应用于图像视图中的图像。

How can I modify the applyShadow function to make the iOS drop-shadow match the effect of the web one? 如何修改applyShadow函数以使iOS阴影与Web的效果匹配?

I'm using .png files, but if I had to convert the file format, It wouldn't be too much more work. 我正在使用.png文件,但是如果我不得不转换文件格式,那就不会做太多的工作了。

在此处输入图片说明

Here just think of view.layer.shadowOffset as the fourth quadrant of 2 dimensional coordinate system. 在这里,只需将view.layer.shadowOffset视为二维坐标系的第四象限。 So if the value for that is CGSizeMake(0, 0) , the shadow will spread in all the sides equally. 因此,如果该值为CGSizeMake(0,0) ,则阴影将平均分布在所有侧面。

Now view.layer.shadowRadius is just the radius or reach of the shadow. 现在view.layer.shadowRadius只是阴影的半径或范围。 By changing its value, the shadow length will change. 通过更改其值,阴影长度将更改。

view.layer.shadowOpacity will change the darkness of the shadow. view.layer.shadowOpacity将更改阴影的暗度。 Its value change from 0 to 1. 其值从0变为1。

Suppose we set the shadowOffset as CGSizeMake(5, 0) and shadowRadius as something less than 5 , then the shadow will move to the right side of the object and there will not be any shadows in other sides. 假设我们将shadowOffset设置为CGSizeMake(5,0)并将shadowRadius设置为小于5的值 ,则阴影将移动到对象的右侧,而在其他侧面将没有任何阴影。 Suppose the value is CGSizeMake(0, 5) , the shadow will be at the bottom of the object, ie it will move 5 points down and there will not be any shadows in other sides. 假设值为CGSizeMake(0,5) ,则阴影将在对象的底部,即,它将向下移动5个点,并且在另一侧没有任何阴影。 So in that way if value is CGSizeMake(5, 5) , the shadow will move bottom-left direction 5 points. 因此,如果value为CGSizeMake(5,5) ,则阴影将向左下方向移动5个点。

I think you got a better understanding of this. 我认为您对此有了更好的了解。 So when comes back to your problem, I think this will work: 因此,当您遇到问题时,我认为这可以解决问题:

func applyShadow(view: UIView){
    view.layer.shadowColor = UIColor.blackColor().CGColor
    view.layer.shadowOffset = CGSizeMake(8, 12)
    view.layer.shadowOpacity = 0.4
    view.layer.shadowRadius = 5.0
}

Something like this should work: 这样的事情应该起作用:

func applyShadow(view: UIView){
    let offset = CGSize(width: 150, height: 150)
    let path = UIBezierPath()
    path.move(to: CGPoint(x: 0, y: 0))
    path.addLine(to: CGPoint(x: view.frame.size.width, y: 0))
    path.addLine(to: CGPoint(x: view.frame.size.width + offset.width, y: view.frame.size.height))
    path.addLine(to: CGPoint(x: view.frame.size.width + offset.width, y: view.frame.size.height + offset.height))
    path.addLine(to: CGPoint(x: view.frame.size.width, y: view.frame.size.height + offset.height))
    path.addLine(to: CGPoint(x: 0, y: view.frame.size.height))
    path.addLine(to: CGPoint(x: 0, y: 0))

    view.layer.shadowColor = UIColor.black.cgColor
    view.layer.shadowPath = path.cgPath
    view.layer.shadowOpacity = 0.2
    view.layer.shadowRadius = 2.0
    view.layer.masksToBounds = false
}

You might need to fiddle around with offset , shadowOpacity , and shadowRadius a little bit more to get as close as possible, but these values are pretty close when I debug locally. 您可能需要花更多的时间使用offsetshadowOpacityshadowRadius来获得尽可能接近的值,但是当我在本地调试时,这些值非常接近。

Also, you don't need to do view.clipsToBounds and view.layer.masksToBounds at the same time. 另外,您无需同时执行view.clipsToBoundsview.layer.masksToBounds Those are pretty much interchangeable and if you set one, the other should be updated automatically. 这些几乎可以互换,如果您设置了一个,则另一个应自动更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM