UIView的drawRect方法

简介:

UIView的drawRect方法

 

 

自定义一个UIView类,代码如下:

 

MainView.h

Cpp代码   收藏代码
  1. #import <UIKit/UIKit.h>  
  2.   
  3.   
  4. @interface MainView : UIView {  
  5.   
  6. }  
  7.   
  8. @end  
 

 

 

MainView.m

Cpp代码   收藏代码
  1. #import "MainView.h"  
  2.   
  3.   
  4. @implementation MainView  
  5.   
  6.   
  7. - (id)initWithFrame:(CGRect)frame {  
  8.       
  9.     self = [super initWithFrame:frame];  
  10.     if (self) {  
  11.         // Initialization code.  
  12.     }  
  13.     self.backgroundColor=[UIColor cyanColor];  
  14.       
  15.       
  16.       
  17.     return self;  
  18. }  
  19.   
  20.   
  21. // Only override drawRect: if you perform custom drawing.  
  22. // An empty implementation adversely affects performance during animation.  
  23. - (void)drawRect:(CGRect)rect {  
  24.     // Drawing code.  
  25.     //获得处理的上下文    
  26.     CGContextRef context = UIGraphicsGetCurrentContext();    
  27.     //设置线条样式    
  28.     CGContextSetLineCap(context, kCGLineCapSquare);     
  29.     //设置线条粗细宽度    
  30.     CGContextSetLineWidth(context, 1.0);     
  31.     
  32.     //设置颜色    
  33.     CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);     
  34.     //开始一个起始路径    
  35.     CGContextBeginPath(context);     
  36.     //起始点设置为(0,0):注意这是上下文对应区域中的相对坐标,    
  37.     CGContextMoveToPoint(context, 0, 0);     
  38.     //设置下一个坐标点    
  39.     CGContextAddLineToPoint(context, 100, 100);     
  40.     //设置下一个坐标点    
  41.     CGContextAddLineToPoint(context, 0, 150);    
  42.     //设置下一个坐标点    
  43.     CGContextAddLineToPoint(context, 50, 180);    
  44.     //连接上面定义的坐标点    
  45.     CGContextStrokePath(context);  
  46.       
  47. }  
  48.   
  49.   
  50. - (void)dealloc {  
  51.     [super dealloc];  
  52. }  
  53.   
  54.   
  55. @end  
 

 

 

 

 

在Xcode中创建Application-Base项目:(这里项目名假设为 Test95

 

Test95AppDelegate.h代码:

Cpp代码   收藏代码
  1. #import <UIKit/UIKit.h>  
  2. #import "MainView.h"  
  3.   
  4. @interface Test95AppDelegate : NSObject <UIApplicationDelegate> {  
  5.     UIWindow *window;  
  6.     MainView *mainView;  
  7. }  
  8.   
  9. @property (nonatomic, retain) IBOutlet UIWindow *window;  
  10.   
  11. @end  
 

 

 

Test95AppDelegate.m中的didFinishLaunchingWithOptions方法代码

Cpp代码   收藏代码
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {      
  2.       
  3.     // Override point for customization after application launch.  
  4.     CGRect wholeWindow=CGRectMake(0, 20, 320, 320);  
  5.     mainView=[[MainView alloc]initWithFrame:wholeWindow];  
  6.       
  7.     [self.window addSubview:mainView];  
  8.     [self.window makeKeyAndVisible];  
  9.       
  10.     return YES;  
  11. }  
 

 

 

结果如下图:


 


相关文章
|
Swift
Swift - UIView,UILabel,UIButton,UIImageView
Swift - UIView,UILabel,UIButton,UIImageView
67 0
|
开发工具
UIView的clipsTobounds属性
UIView的clipsTobounds属性
92 0
UIView的clipsTobounds属性
|
容器
UIView与CALayer的关系
UIView与CALayer的关系
420 0

热门文章

最新文章