如何将 UIImageView 添加到 UIView (而不是 UIViewController)?

发布于 2024-12-05 06:12:01 字数 1385 浏览 1 评论 0原文

我创建了基于视图的应用程序。 我有一个示例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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒗幽 2024-12-12 06:12:01

如果你想在简单的 UIView 上添加图像视图,那么只需执行 [self.view addSubview:imageview]; 如果你从Behavior类方法获取该图像,那么从该方法返回 UIImageView 并添加它以你的观点
试试这个:

Behavior *b = [[Behavior alloc] init];

然后在Behavior类中编写一个方法

-(UIImageView*) initilizeWithType:(NSString *)type position:(CGRect) pos mapArray:(NSMutableArray *) mapArr {
// Your Logic

return  imgView;  //UIImageView object
} 

然后在你的视图控制器中调用这个方法

UIImageView *imgView=[b initilizeWithType:@"YOUR STRING" position:YOUR RECT pos mapArray:YOUR ARRAY ];

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 view
Try this:

Behavior *b = [[Behavior alloc] init];

Then write one method in Behaviour class

-(UIImageView*) initilizeWithType:(NSString *)type position:(CGRect) pos mapArray:(NSMutableArray *) mapArr {
// Your Logic

return  imgView;  //UIImageView object
} 

Then call this method in your viewcontroller as

UIImageView *imgView=[b initilizeWithType:@"YOUR STRING" position:YOUR RECT pos mapArray:YOUR ARRAY ];
两相知 2024-12-12 06:12:01
imgg.frame = rect;

据我所知,这一行将您的 imageView 框架分配给...什么也没有。这应该是 pos 还是 rectForDraw ?当添加到drawRect中的视图时,您显式地设置了一个真实的框架,因此这可能就是差异所在。

imgg.frame = rect;

This line assigns your imageView frame to... nothing as far as I can see. Should this be pos or rectForDraw? When adding to your view in drawRect you are explicitly setting a real frame so this is probably where the difference lies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文