首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >仅从UIView的3个侧面绘制阴影

仅从UIView的3个侧面绘制阴影
EN

Stack Overflow用户
提问于 2013-03-29 21:24:06
回答 4查看 21.7K关注 0票数 24

我已经成功地在我的UIView周围绘制了一个阴影,如下所示:

block1.layer.masksToBounds = NO;
block1.layer.shadowOffset = CGSizeMake(0, 0);
block1.layer.shadowRadius = 1;
block1.layer.shadowOpacity = 0.7;

现在的情况是,我有一个矩形的UIView,我想在它周围画一个阴影三面,留下它的底部侧没有阴影。

我知道我必须通过创建一个新的block1.layer.shadowPath来指定UIBezierPath,但是我不确定该怎么做。

显然,设置layer.shadowOffset对我不起作用。

提前感谢!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-03-29 22:01:49

我知道你说设置layer.shadowOffset对你不起作用,但是你可以设置负值,这样设置layer.shadowOffset = CGSizeMake(0.0, -2.0)就会接近你想要的效果,当然我希望它在三个方面都是一样的。

现在让我们来看看layer.shadowPath吧!

UIView *block1 = [[UIView alloc] initWithFrame:CGRectMake(32.0, 32.0, 128.0, 128.0)];
[block1 setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:block1];

block1.layer.masksToBounds = NO;
block1.layer.shadowOffset = CGSizeMake(0, 0);
block1.layer.shadowRadius = 1;
block1.layer.shadowOpacity = 0.7;

UIBezierPath *path = [UIBezierPath bezierPath];

// Start at the Top Left Corner
[path moveToPoint:CGPointMake(0.0, 0.0)];

// Move to the Top Right Corner
[path addLineToPoint:CGPointMake(CGRectGetWidth(block1.frame), 0.0)];

// Move to the Bottom Right Corner
[path addLineToPoint:CGPointMake(CGRectGetWidth(block1.frame), CGRectGetHeight(block1.frame))];

// This is the extra point in the middle :) Its the secret sauce.
[path addLineToPoint:CGPointMake(CGRectGetWidth(block1.frame) / 2.0, CGRectGetHeight(block1.frame) / 2.0)];

// Move to the Bottom Left Corner
[path addLineToPoint:CGPointMake(0.0, CGRectGetHeight(block1.frame))];

// Move to the Close the Path
[path closePath];

block1.layer.shadowPath = path.CGPath;

为了让你知道发生了什么,这里是你刚刚绘制的实际阴影路径:)

可以只将额外中点移动到其他行之前或之后,以选择将省略哪一侧。

票数 49
EN

Stack Overflow用户

发布于 2017-10-30 15:58:15

多亏了Ashok R的快速代码,对其他答案有了一点改进。

因为我们在视图的背景中创建了一个在所有边都有阴影的三角形视图,所以不需要在边阴影上有一个白色的三角形。

如果视图的宽度比高度大,它就会中断。

一种解决方法是将不需要阴影的线的路径稍微向视图一侧移动一点,而不是完全地创建三角形视图路径。

我为此创建了一个扩展-

extension UIView {
    func addshadow(top: Bool,
                   left: Bool,
                   bottom: Bool,
                   right: Bool,
                   shadowRadius: CGFloat = 2.0) {

        self.layer.masksToBounds = false
        self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
        self.layer.shadowRadius = shadowRadius
        self.layer.shadowOpacity = 1.0

        let path = UIBezierPath()
        var x: CGFloat = 0
        var y: CGFloat = 0
        var viewWidth = self.frame.width
        var viewHeight = self.frame.height

        // here x, y, viewWidth, and viewHeight can be changed in
        // order to play around with the shadow paths.
        if (!top) {
            y+=(shadowRadius+1)
        }
        if (!bottom) {
            viewHeight-=(shadowRadius+1)
        }
        if (!left) {
            x+=(shadowRadius+1)
        }
        if (!right) {
            viewWidth-=(shadowRadius+1)
        }
        // selecting top most point
        path.move(to: CGPoint(x: x, y: y))
        // Move to the Bottom Left Corner, this will cover left edges
        /*
         |☐
         */
        path.addLine(to: CGPoint(x: x, y: viewHeight))
        // Move to the Bottom Right Corner, this will cover bottom edge
        /*
         ☐
         -
         */
        path.addLine(to: CGPoint(x: viewWidth, y: viewHeight))
        // Move to the Top Right Corner, this will cover right edge
        /*
         ☐|
         */
        path.addLine(to: CGPoint(x: viewWidth, y: y))
        // Move back to the initial point, this will cover the top edge
        /*
         _
         ☐
         */        
        path.close()
        self.layer.shadowPath = path.cgPath
    }

并将希望阴影出现的任何一侧的布尔值设置为true

myView.addshadow(top: false, left: true, bottom: true, right: true, shadowRadius: 2.0)

//上述阴影半径是可选的,默认设置为2.0

myView.addshadow(top: true, left: true, bottom: true, right: true, shadowRadius: 2.0)

myView.addshadow(top: false, left: false, bottom: true, right: true, shadowRadius: 2.0)

票数 11
EN

Stack Overflow用户

发布于 2016-10-13 11:32:21

Ryan Poolos Answer更新到Swift 3.0

感谢Ryan Poolos

class sampleViewController: UIViewController {
    var block1: UIView! = nil

    override func viewDidLoad() {

        super.viewDidLoad()
        block1 = UIView(frame: CGRect(x: 32.0, y: 32.0, width: 128.0, height: 128.0))
        block1.backgroundColor = UIColor.orange
        self.view.addSubview(block1)

        block1.layer.masksToBounds = false
        block1.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
        block1.layer.shadowRadius = 1.0
        block1.layer.shadowOpacity = 0.7

        let path = UIBezierPath()

        // Start at the Top Left Corner
        path.move(to: CGPoint(x: 0.0, y: 0.0))

        // Move to the Top Right Corner
        path.addLine(to: CGPoint(x: block1.frame.size.width, y: 0.0))

        // Move to the Bottom Right Corner
        path.addLine(to: CGPoint(x: block1.frame.size.width, y: block1.frame.size.height))

        // This is the extra point in the middle :) Its the secret sauce.
        path.addLine(to: CGPoint(x: block1.frame.size.width/2.0, y: block1.frame.size.height/2.0))

        // Move to the Bottom Left Corner
        path.addLine(to: CGPoint(x: 0.0, y: block1.frame.size.height))

        path.close()

        block1.layer.shadowPath = path.cgPath
    }
}

结果:

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15704163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档