请问如果需要在uiview中绘图,需要重写哪个入口函数?

如果需要在uiview中绘图,需要重写哪个入口函数


慕后森
浏览 525回答 3
3回答

慕斯709654

自定义一个UIView类,代码如下:MainView.h#import <UIKit/UIKit.h>@interface MainView : UIView {}@endMainView.m#import "MainView.h"@implementation MainView- (id)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];if (self) {// Initialization code.}self.backgroundColor=[UIColor cyanColor];return self;}// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect {// Drawing code.//获得处理的上下文CGContextRef context = UIGraphicsGetCurrentContext();//设置线条样式CGContextSetLineCap(context, kCGLineCapSquare);//设置线条粗细宽度CGContextSetLineWidth(context, 1.0);//设置颜色CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);//开始一个起始路径CGContextBeginPath(context);//起始点设置为(0,0):注意这是上下文对应区域中的相对坐标,CGContextMoveToPoint(context, 0, 0);//设置下一个坐标点CGContextAddLineToPoint(context, 100, 100);//设置下一个坐标点CGContextAddLineToPoint(context, 0, 150);//设置下一个坐标点CGContextAddLineToPoint(context, 50, 180);//连接上面定义的坐标点CGContextStrokePath(context);}- (void)dealloc {[super dealloc];}@end

泛舟湖上清波郎朗

MainView.h#import <UIKit/UIKit.h>@interface MainView : UIView {}@endMainView.m#import "MainView.h"@implementation MainView- (id)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];if (self) {// Initialization code.}self.backgroundColor=[UIColor cyanColor];return self;}//&nbsp;Only&nbsp;override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect {// Drawing code.//获得处理的上下文CGContextRef context = UIGraphicsGetCurrentContext();//设置线条样式CGContextSetLineCap(context, kCGLineCapSquare);//设置线条粗细宽度CGContextSetLineWidth(context, 1.0);//设置颜色CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);//开始一个起始路径CGContextBeginPath(context);//起始点设置为(0,0):注意这是上下文对应区域中的相对坐标,CGContextMoveToPoint(context, 0, 0);//设置下一个坐标点CGContextAddLineToPoint(context, 100, 100);//设置下一个坐标点CGContextAddLineToPoint(context, 0, 150);//设置下一个坐标点CGContextAddLineToPoint(context, 50, 180);//连接上面定义的坐标点CGContextStrokePath(context);}- (void)dealloc {[super dealloc];}

qq_遁去的一_1

一、重绘机制iOS的绘图操作是在UIView类的drawRect方法中完成的,所以如果我们要想在一个UIView中绘图,需要写一个扩展UIView 的类,并重写drawRect方法,在这里进行绘图操作,程序会自动调用此方法进行绘图。重绘操作仍然在drawRect方法中完成,但是苹果不建议直接调用drawRect方法,当然如果你强直直接调用此方法,当然是没有效果的。苹果要求我们调用UIView类中的setNeedsDisplay方法,则程序会自动调用drawRect方法进行重绘。(调用setNeedsDisplay会自动调用drawRect)。在UIView中,重写drawRect: (CGRect) aRect方法,可以自己定义想要画的图案.且此方法一般情况下只会画一次.也就是说这个drawRect方法一般情况下只会被掉用一次. 当某些情况下想要手动重画这个View,只需要掉用[self setNeedsDisplay]方法即可.二、方法定义①、- (void)drawRect:(CGRect)rect;重写此方法,执行重绘任务②、- (void)setNeedsDisplay;标记为需要重绘,异步调用drawRect③、- (void)setNeedsDisplayInRect:(CGRect)rect;标记为需要局部重绘三、drawRect调用机制drawRect调用是在Controller->loadView,,Controller->viewDidLoad 两方法之后调用的。所以不用担心在控制器中,这些View的drawRect就开始画了。这样可以在控制器中设置一些值给View(如果这些View draw的时候需要用到某些变量值).1、如果在UIView初始化时没有设置rect大小,将直接导致drawRect不被自动调用。2、该方法在调用sizeThatFits后被调用,所以可以先调用sizeToFit计算出size。然后系统自动调用drawRect:方法。3、通过设置contentMode属性值为UIViewContentModeRedraw。那么将在每次设置或更改frame的时候自动调用drawRect:。4、直接调用setNeedsDisplay,或者setNeedsDisplayInRect:触发drawRect:,但是有个前提条件是rect不能为0.以上1,2推荐;而3,4不提倡四、绘图1、若使用UIView绘图,只能在drawRect:方法中获取相应的contextRef并绘图。如果在其他方法中获取将获取到一个invalidate的ref并且不能用于画图。drawRect:方法不能手动显示调用,必须通过调用setNeedsDisplay 或者 setNeedsDisplayInRect ,让系统自动调该方法。2、若使用calayer绘图,只能在drawInContext: 中(类似于drawRect)绘制,或者在delegate中的相应方法绘制。同样也是调用setNeedDisplay等间接调用以上方法。3、若要实时画图,不能使用gestureRecognizer,只能使用touchbegan等方法来掉用setNeedsDisplay实时刷新屏幕
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Vue.js