加阴影的方法

本文介绍了如何使用 iOS 开发中 UIImageView 和 UIView 的 CALayer 属性来为图片和视图添加阴影效果,包括设置阴影的颜色、偏移、不透明度及半径等参数。
一、给图片加上阴影 

 UIImageView*pageContenterImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"onePageApple.png"]];

        

         //添加边框

        

         CALayer*layer = [pageContenterImageView layer];

        

        layer.borderColor= [[UIColor whiteColor]CGColor];

        

        layer.borderWidth=0.0f;

        

        //添加四个边阴影

        

        pageContenterImageView.layer.shadowColor= [UIColor blackColor].CGColor;

        

        pageContenterImageView.layer.shadowOffset=CGSizeMake(0,0);

        

        pageContenterImageView.layer.shadowOpacity=0.5; 

        

        pageContenterImageView.layer.shadowRadius=5.0;

        

        //添加两个边阴影

        

        //pageContenterImageView.layer.shadowColor = [UIColor blackColor].CGColor;

        

        //    pageContenterImageView.layer.shadowOffset = CGSizeMake(2, 2);

        

        //    pageContenterImageView.layer.shadowOpacity = 0.5; 

        

        //    pageContenterImageView.layer.shadowRadius = 2.0;

        

        //-------------

        

        阴影渲染会严重消耗内存 ,导致程序咔叽.

        

         /*阴影效果*/

        

         //添加边框

        

        CALayer*layer = [self.pageContenter layer];

        

        layer.borderColor= [[UIColorwhiteColor]CGColor];

        

        layer.borderWidth=0.0f;

        

        //添加四个边阴影

        

         self.pageContenter.layer.shadowColor= [UIColorblackColor].CGColor;//阴影颜色

        

         self.pageContenter.layer.shadowOffset=CGSizeMake(0,0);//阴影偏移

        

         self.pageContenter.layer.shadowOpacity=0.5;//阴影不透明度

        

         self.pageContenter.layer.shadowRadius=5.0;//阴影半径

        

        //添加两个边阴影

        

        //self.pageContenter.layer.shadowColor = [UIColor blackColor].CGColor;

        

        //self.pageContenter.layer.shadowOffset = CGSizeMake(2, 2);

        

        //self.pageContenter.layer.shadowOpacity = 0.5; 

        

        //self.pageContenter.layer.shadowRadius = 2.0; 



二、给视图加上阴影


 UIView * content=[[UIView alloc] initWithFrame:CGRectMake(100, 250, 503,  500)]; 

    content.backgroundColor=[UIColor orangeColor];

    //content.layer.shadowOffset=10;

    

    

    content.layer.shadowOffset = CGSizeMake(5, 3);

    content.layer.shadowOpacity = 0.6;

    content.layer.shadowColor = [UIColor blackColor].CGColor;

    

    

    [self.window addSubview:content];

转载于:https://www.cnblogs.com/gaoxiao228/archive/2012/05/12/2497666.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值