使用UIBezierPath添加投影效果
2024-09-04 16:06:03
代码:
ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @end
ViewController.m
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(, , , )]; [self.view addSubview:view]; //添加View的阴影
[self addShadowWithView:view];
//[self addShadowWithView2:view];
} //添加View的阴影
- (void)addShadowWithView:(UIView *)view{ view.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色,默认为黑色
view.layer.shadowOffset = CGSizeMake(, );
view.layer.shadowOpacity = 1.0;
view.layer.shadowRadius = ; //路径阴影
UIBezierPath *path = [UIBezierPath bezierPath]; float width = view.bounds.size.width;
float height = view.bounds.size.height;
float x = view.bounds.origin.x;
float y = view.bounds.origin.y; CGPoint topLeft = CGPointMake(x, y);
CGPoint topMiddle = CGPointMake(x + (width/), y);
CGPoint topRight = CGPointMake(x + width, y); CGPoint rightMiddle = CGPointMake(x + width, y + (height/)); CGPoint bottomRight = CGPointMake(x+width, y+height);
CGPoint bottomMiddle = CGPointMake(x + (width/), y + height);
CGPoint bottomLeft = CGPointMake(x, y + height); CGPoint leftMiddle = CGPointMake(x, y + (height/)); [path moveToPoint:topLeft];
//添加四个二元曲线
[path addQuadCurveToPoint:topRight controlPoint:topMiddle];
[path addQuadCurveToPoint:bottomRight controlPoint:rightMiddle];
[path addQuadCurveToPoint:bottomLeft controlPoint:bottomMiddle];
[path addQuadCurveToPoint:topLeft controlPoint:leftMiddle]; view.layer.shadowPath = path.CGPath;
} //添加View的阴影2
- (void)addShadowWithView2:(UIView *)view{ //使用这种办法来设置投影,必须添加背景颜色或者边框颜色否则是无法显示的
view.layer.borderColor = [UIColor blueColor].CGColor;
view.layer.borderWidth = 1.0; view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOffset = CGSizeMake(, );
view.layer.shadowOpacity = 1.0;
view.layer.shadowRadius = ; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
实例代码:
参考链接:
http://blog.csdn.net/rhljiayou/article/details/10178723
最新文章
- poj 1651 Multiplication Puzzle
- 报错:Failed to instantiate the default view controller for UIMainStoryboardFile &#39;MainStoryboard&#39; - perhaps the designated entry point is not set?
- SQLServer2005删除log文件和清空日志的方案
- sql xpath 查找包含
- hdu 3037 Saving Beans
- Linux系统(将web应用部署到tomcat服务器上)
- 获取WebView加载HTML时网页中的内容
- bzoj4005[JLOI2015]骗我呢
- PCB打样镀层问题
- Android开发匹配字符笔记
- [LeetCode234]Palindrome Linked List
- EasyUI项目驱动学习
- Android学习-应用程序管理
- Python网络数据采集2-wikipedia
- 【一天一道LeetCode】#202. Happy Number
- JavaScript 基础(三) - Date对象,RegExp对象,Math对象,Window 对象,History 对象,Location 对象,DOM 节点
- UVA806-Spatial Structures(四分树)
- HikariCP 脑火Failed to obtain JDBC Connection: You need to run the CLI build and you need target/classes in your classpath to run.
- 【Unity】11.7 布料
- Redis安装系统服务1073错误