如何将 UIImageView 添加到 UIView (而不是 UIViewController)?
我创建了基于视图的应用程序。 我有一个示例GameViewContrioller.xib,其中包含主视图和子视图,它们与类Behavior相关。
SampleViewController.xib:
- 视图
- 查看<-行为
在sampleViewContoller.m 中,我创建了Behavior 类的实例:
Behavior *b = [[Behavior alloc] initilizeWithType:@"type" position:rect mapArray:arrMap];
Behavior.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) 矩形中添加 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]; }
但是我应该通过构造函数在行为类中发送绘图参数。如何使用我的构造函数在没有方法drawRect的情况下添加imageView?
对不起我的英语:)
I created view-based application.
I have got a sampleGameViewContrioller.xib, which contains main View, and child View, which connected with class Behavior.
SampleViewController.xib:
- View
- View <- Behavior
In sampleViewContoller.m I create instance of the class Behavior:
Behavior *b = [[Behavior alloc] initilizeWithType:@"type" position:rect mapArray:arrMap];
Behavior.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;}
But it doesn't work. Image doesn't add to my View.
if I add imageView in -(void)drawRect:(CGRect) rect, it's working:
- (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]; }
But i should send parameters of drawing in class Behavior through constructor. How to add imageView without method drawRect, with my constructor?
sorry for my English :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想在简单的 UIView 上添加图像视图,那么只需执行
[self.view addSubview:imageview];
如果你从Behavior类方法获取该图像,那么从该方法返回 UIImageView 并添加它以你的观点试试这个:
然后在Behavior类中编写一个方法
然后在你的视图控制器中调用这个方法
If u want to add an image view over simple UIView then just do
[self.view addSubview:imageview];
If you are getting that Image from Behaviour class method then return UIImageView from that method and the add it to your viewTry this:
Then write one method in Behaviour class
Then call this method in your viewcontroller as
据我所知,这一行将您的 imageView 框架分配给...什么也没有。这应该是
pos
还是rectForDraw
?当添加到drawRect中的视图时,您显式地设置了一个真实的框架,因此这可能就是差异所在。This line assigns your imageView frame to... nothing as far as I can see. Should this be
pos
orrectForDraw
? When adding to your view in drawRect you are explicitly setting a real frame so this is probably where the difference lies.