利用渐变实现阴影效果

本文介绍如何使用Swift为iOS应用中的视图添加圆角效果及顶部渐变阴影,涉及CAGradientLayer和UIView的layer属性设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       //  添加圆角

   self.layer.cornerRadius = 15.0f;

        [self.layer setMasksToBounds:YES];


       //  添加渐变

        UIView *topShadowView = [[UIView alloc] initWithFrame:CGRectMake(self.bounds.origin.x+kLeftPadding,

                                                                         self.bounds.origin.y + self.bounds.size.height - kShadowHeight

                                                                         ,self.bounds.size.width-kLeftPadding - kRightPadding , kShadowHeight)];

        CAGradientLayer *topShadow = [CAGradientLayer layer];

        topShadow.frame = CGRectMake(0, 0, topShadowView.bounds.size.width, kShadowHeight);

        topShadow.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor clearColor] CGColor], nil];

        [topShadowView.layer insertSublayer:topShadow atIndex:0];

        [self addSubview:topShadowView];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值