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. }  
 

 

 

结果如下图:


### `QPainter` 中 `drawRect` 的使用方法 `QPainter` 是绘图类,可在给定的绘图设备类上绘制图像,`drawRect` 函数用于在绘图设备上绘制矩形。它有多种重载形式,常见的两种如下: ```cpp void QPainter::drawRect(int x, int y, int width, int height); void QPainter::drawRect(const QRect &rect); ``` - 第一种形式通过指定矩形左上角的坐标 `(x, y)` 以及矩形的宽度 `width` 和高度 `height` 来绘制矩形。 - 第二种形式则是直接传入一个 `QRect` 对象来绘制矩形。 以下是一个简单的示例代码,展示了如何在 `paintEvent` 函数中使用 `drawRect`: ```cpp #include <QWidget> #include <QPainter> class MyWidget : public QWidget { public: void paintEvent(QPaintEvent *) override { QPainter painter(this); // 使用第一种重载形式 painter.drawRect(50, 50, 200, 100); // 使用第二种重载形式 QRect rect(300, 50, 200, 100); painter.drawRect(rect); } }; ``` ### `drawRect` 与 'QPainter::setFont: Painter not active' 错误的关联 'QPainter::setFont: Painter not active' 错误表明 `QPainter` 尚未被正确激活。同样,在 `QPainter` 未激活的情况下调用 `drawRect` 函数也会出现类似的错误,如 `QPainter::drawRect: Painter not active`。这是因为在 `QPainter` 未激活时,它无法与绘图设备建立有效的连接,从而不能执行绘图操作。 ### 解决方案 要解决 'QPainter::setFont: Painter not active' 错误以及 `drawRect` 可能出现的类似错误,需要确保 `QPainter` 被正确激活。常见的激活方式有以下两种: - **在 `paintEvent` 函数中使用**:这是最常见的使用方法,当需要在一个控件中绘制图形时,可重载 `paintEvent` 函数。示例代码如下: ```cpp #include <QWidget> #include <QPainter> class MyWidget : public QWidget { public: void paintEvent(QPaintEvent *) override { QPainter painter(this); painter.setFont(QFont("Arial", 12)); painter.drawRect(50, 50, 200, 100); } }; ``` - **手动激活 `QPainter`**:在 `paintEvent` 函数之外使用 `QPainter` 时,需要手动调用 `begin` 和 `end` 函数来激活和结束绘图操作。示例代码如下: ```cpp #include <QWidget> #include <QPainter> #include <QPixmap> void drawOnPixmap() { QPixmap pixmap(400, 300); QPainter painter; painter.begin(&pixmap); painter.setFont(QFont("Arial", 12)); painter.drawRect(50, 50, 200, 100); painter.end(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值