【发布时间】:2011-09-20 09:34:22
【问题描述】:
我创建了基于视图的应用程序。 我有一个sampleGameViewContrioller.xib,其中包含主视图和子视图,与行为类连接。
SampleViewController.xib:
- 查看
- 查看
在 sampleViewContoller.m 中,我创建了 Behavior 类的实例:
Behavior *b = [[Behavior alloc] initilizeWithType:@"type" position:rect mapArray:arrMap];
行为.m:
-(id) initilizeWithType:(NSString *)type position:(CGRect) pos mapArray:(NSMutableArray *) mapArr {
self = [super initWithFrame:CGRectMake(0, 0, 1024, 570)];
if (self != nil) {
if ([type isEqualToString:@"type"]) {
arrMap = mapArr;
self.rectForDraw = pos;
self.file = [[NSString alloc]initWithString:@"girl.png"];
UIImageView *imgg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"girl.png"]];
imgg.frame = rect;
[self addSubview:imgg];
timer = [NSTimer scheduledTimerWithTimeInterval:0.015 target:self selector:@selector(moving:) userInfo:nil repeats:YES];
}
}
return self;}
但它不起作用。图片未添加到我的视图中。
如果我在 -(void)drawRect:(CGRect) rect 中添加 imageView,它可以工作:
- (void)drawRect:(CGRect)rect {
self.img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"girl.png"]];
self.img.frame = CGRectMake(100, 100, 100, 100);
[self addSubview:self.img]; }
但我应该通过构造函数在 Behavior 类中发送绘图参数。如何使用我的构造函数添加没有方法drawRect的imageView?
对不起我的英语:)
【问题讨论】:
-
您能否澄清以下几点:在您的视图控制器中创建
b后,您将如何处理它?您在上一个代码 sn-p 中覆盖了哪个drawRect?这是Behavior类吗? -
我创建了一个类的实例来管理它们中的每一个。类绘制图像并按照自己的逻辑移动它。因此,创建了一个实例数组,每个实例负责移动图像。
-
>> 您在上一个代码 sn-p 中覆盖了哪个 drawRect?这是行为类吗?是的,在 Behavior 类中。但这只是一个示例
-
但是
b确实在某些时候被添加为视图控制器视图的子视图?你能显示你正在使用的实际代码吗? -
一切正常)你可以在那里看到我的代码示例:file.qip.ru/get/sB1Iq7UX/sample.html
标签: objective-c ipad uiview uiimageview