所有4面的UIButton阴影 [英] UIButton shadow on all 4 sides

查看:167
本文介绍了所有4面的UIButton阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为UIButton生成阴影。

I am trying to generate shadow for a UIButton.

以下是我正在使用的内容。

Below is what I am using.

myButton.layer.shadowColor = [UIColor blackColor].CGColor;
myButton.layer.shadowOpacity = 0.5;
myButton.layer.shadowRadius = 1;
myButton.layer.shadowOffset = CGSizeMake(4, 4);
myButton.layer.masksToBounds = NO;

但它在右下方生成阴影。

But its generating shadow on right and bottom.

有没有办法让我可以在所有4个方面都有阴影?

Is there way where I can have shadow on all 4 sides?

作为另一种解决方案,我这样做是通过在按钮后面放置带阴影的图像,但是我不想那样。

As an another solution, I am do this by putting image with shadow behind the button, but I don't want to go that way.

有没有办法以编程方式完成这项工作?

Is there any way to get this done programmatically?

如下所示。

推荐答案

因为您通过 {4,4} 阴影来抵消阴影出现在按钮的右下角。您可以设置零偏移:

Since you offset the shadow by {4, 4} the shadow appears on the bottom-right side of the button. You could set a zero offset :

myButton.layer.shadowOffset = CGSizeZero

并通过调整 shadowRadius ,您可以达到您想要的效果。

and by tweaking the shadowRadius you might achieve what you want.

以下是阴影的构建方式:

Here is how a shadow is built:

(第1行)从按钮的形状开始

(1st row) start from your button's shape

(第2行)在按钮下面绘制一个黑色形状,然后将其转换为 shadowOffset 中指定的数量:左边是10px,右边是0px。在右侧,您无法看到黑色矩形,因为它直接位于按钮下方

(2nd row) draw a black shape underneath your button and translate it by the amount specified in shadowOffset : 10px on the left, 0px on the right. On the right one you can't see the black rectangle as it's directly underneath the button

(第3行)将黑色矩形模糊为<$ c $中指定的数量C> blurRadius 。零意味着没有模糊,黑色矩形会保持清晰,所以如果你不偏移并且不模糊,你什么也看不见。

(3rd row) blur the black rectangle by the amount specified in blurRadius. Zero means no blur and the black rectangle would stay sharp, so if you don't offset and don't blur, you'll see nothing.

这篇关于所有4面的UIButton阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆