iOS 给一个uiview设置shadow

本文介绍如何使用Swift和CALayer为iOS应用中的视图添加阴影效果。通过调整阴影偏移、半径、颜色和不透明度等属性,实现触摸开始和结束时不同的视觉反馈。

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

给view设置shadow 主要是操作的caller上的,所以我们要先导入头文件

#import <QuartzCore/QuartzCore.h>


然后,在函数

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    CALayer *layer = self.layer;

    [layer setBorderColor:[[UIColor yellowColor] CGColor]];//边框的颜色

    layer.shadowOffset = CGSizeMake(0, 3);

    layer.shadowRadius = 5.0;

    layer.shadowColor = [UIColor yellowColor].CGColor;//shadow的颜色

    layer.shadowOpacity = 0.8;

    [layer setBorderWidth:2.75];

}

touch结束后,设置回来


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

   

    CALayer *layer = self.layer;

//这里的rgba可以自己设置

    [layer setBorderColor:[[UIColor colorWithRed:0.588 green:0.737 blue:0.976 alpha:1.0] CGColor]];

    layer.shadowOffset = CGSizeMake(0, 8);

    layer.shadowRadius = 5.0;

    layer.shadowColor = [UIColor blackColor].CGColor;

    layer.shadowOpacity = 0.8;

    [layer setBorderWidth:2.75];

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值