xcode iOS 中 ImageView 上的阴影效果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27222595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:17:13  来源:igfitidea点击:

Shadow effects on ImageView in iOS

iosobjective-cxcodeuiimageviewshadow

提问by Shikhar varshney

I am trying to provide a shadow effect to my Imageview just like in this image.

我正在尝试为我的 Imageview 提供阴影效果,就像在这张图片中一样。

enter image description here

在此处输入图片说明

but the problem which I am facing it is that the shadow is actually visible from the bottom of the Imageview.

但我面临的问题是从 Imageview 的底部实际上可以看到阴影。

Here is the code which I have done to add the shadow. The color and all is still not matching with this one.

这是我为添加阴影所做的代码。颜色和所有仍然与这个不匹配。

          CAGradientLayer *shadow = [CAGradientLayer layer];
          shadow.frame = CGRectMake(-100, 70, perspectiveView.frame.size.width, 
           perspectiveView.frame.size.height - 20);
          shadow.startPoint = CGPointMake(1.0, 0.5);
           shadow.endPoint = CGPointMake(0, 0.5);
           shadow.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0.0 
           alpha:0.4f] CGColor], (id)[[UIColor clearColor] CGColor], nil];

          shadow.opacity = 1.0f;
          [perspectiveView.layer addSublayer:shadow];

Please provide inputs. Also if the approach is wrong, feel free to guide me to any other approach. Thanks in advance.

请提供输入。此外,如果方法错误,请随时指导我使用任何其他方法。提前致谢。

Also can anyone suggest how to provide a 3D border just like in the image which provides a slight width to the image view?

也有人可以建议如何提供 3D 边框,就像在图像中为图像视图提供轻微宽度的图像一样?

回答by GenieWanted

Perhaps, adding a shadow to the layer seems to work. You may want to try something like this:

也许,向图层添加阴影似乎有效。你可能想尝试这样的事情:

//  perspectiveView.transform = CGAffineTransformMakeRotation(-50.0f);   
    perspectiveView.layer.shadowOffset = CGSizeMake(10, 10);
    perspectiveView.layer.shadowRadius = 5.0;
    perspectiveView.layer.shadowOpacity = 0.6;

    perspectiveView.layer.masksToBounds = NO;

You will need to playaround with these values to match your requirements. Hope this helps.

您将需要使用这些值来满足您的要求。希望这可以帮助。

Here is my output:

这是我的输出:

enter image description here

在此处输入图片说明

回答by Neenu

Try this,

尝试这个,

- (void)setShadow {
    [perspectiveView.layer setShadowOffset:CGSizeMake(-5.0, 5.0)];
    [perspectiveView.layer setShadowRadius:5.0];
    [perspectiveView.layer setShadowOpacity:1.0];

}

回答by Anurag Bhakuni

you can use CALayerclass , the layer can manage your visual aspects like background color, border, shadow and with it you can also manage the geometry of it content like size , transform etc .

您可以使用CALayerclass ,图层可以管理您的视觉方面,如背景颜色、边框、阴影,并且您还可以管理它的几何内容,如大小、变换等。

visual effect(shadowing)

视觉效果(阴影)

self.yourView.layer.shadowOffset = CGSizeMake(0, 3);
self.yourView.layer.shadowRadius = 5.0;
self.yourView.layer.shadowColor = [UIColor blackColor].CGColor;
self.yourView.layer.shadowOpacity = 1; 

and you also used it for like self.yourView.layer.frame(Geometry)

你也用它来做self.yourView.layer.frame(几何)

more info https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CALayer_class/index.html

更多信息https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CALayer_class/index.html

回答by Lalji

Below code work i have checked properly.

下面的代码工作我已经正确检查。

but Shikhar varshneycan u check setShadow method call and perspectiveView is not nil.

但是Shikhar varshney可以检查 setShadow 方法调用并且透视视图不是零。

- (void)setShadow {
    perspectiveView.layer.shadowColor = [UIColor colorWithWhite:0.0 
       alpha:0.4f] CGColor;
    perspectiveView.layer.shadowOffset = CGSizeMake(0, 1);
    perspectiveView.layer.shadowOpacity = 1;
    perspectiveView.layer.shadowRadius = 1.0;
    perspectiveView.clipsToBounds = NO;
}