![](/img/trans.png)
[英]How to draw a simple line without using Interface Builder in iphone sdk?
[英]How to draw a simple line without using Interface Builder?
我是初學者。 我只想畫一條簡單的線而不使用IB。 我使用的代碼是
lineViewController.m
-(id)init
{
if(self = [super init])
{
myView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,440)];
myView.backgroundColor = [UIColor grayColor];
mView = [[UIView alloc]initWithFrame:CGRectMake(0,0,200,200)];
self.view = myView;
[myView addSubView:mView];
}
return self;
}
-(void)viewWillAppear:(BOOL)animated
{
line = [[MyView alloc]init];
[line setNeedsDisplay];
}
MyView.m
#import "MyView.h"
@implementation MyView
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetStrokeColor(c, red);
CGContextBeginPath(c);
CGContextMoveToPoint(c, 150.0f, 200.0f);
CGContextSetLineWidth(c, 5.0f);
CGContextAddLineToPoint(c, 50,300);
CGContextAddLineToPoint(c, 260,300);
CGContextClosePath(c);
}
但是我無法畫畫。 你能幫我么。 提前致謝。
viewWillAppear中的代碼不執行任何操作,因為行從未添加到視圖層次結構中。 由於上述原因,從未調用MyView drawRect,並且將“ myView”分配為UIView而不是MyView。 嘗試在視圖控制器init中更改alloc。
myView = [[MyView alloc]initWithFrame:CGRectMake(0,0,320,440)];
同樣,代碼也會如寫的那樣泄漏。 查看保留和釋放內存管理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.