添加为子视图时,新创建的 UIImageView 不会显示

发布于 2024-12-18 14:19:53 字数 1264 浏览 5 评论 0原文

点击后,我想让一些 UIImageView 对象出现在底层 UIView 上。 所以我创建了以下操作:

- (IBAction)startStars: (id) sender
{
UIView* that = (UIView*) sender;  // an UIButton, invisible, but reacting to Touch Down events
int tag = that.tag;

UIView* page = [self getViewByTag:mPages index:tag];  // getting a page currently being viewed
if ( page != nil )
{

    int i;
    UIImage* image = [UIImage imageNamed:@"some.png"];

    for ( i = 0 ; i < 10 ; ++i )
    {   
        // create a new UIImageView at the position of the tapped UIView
        UIImageView* zap = [[UIImageView alloc] initWithFrame:that.frame];
        // assigning the UIImage
        zap.image = image;

        // make a modification to the position so we can see all instances
        CGPoint start = that.layer.frame.origin;
        start.x += i*20;
        zap.layer.position = start;

        [page addSubview:zap];   // add to the UIView
        [zap setNeedsDisplay];   // please please redraw
        [zap release];           // release, since page will retain zap

    }
    [image release];
}
}

不幸的是,什么也没有显示。代码被调用,对象被创建,图像被加载,甚至属性也如预期的那样。 页面本身是一个真正的基本 UIView,使用界面生成器创建以包含其他视图和控件。

尽管如此,这一切都看不到......

有人知道我做错了什么吗?我需要设置 alpha 属性(或其他属性)吗?

谢谢祖帕

upon a tap, I want to let a few UIImageView objects appear on an underlaying UIView.
So I created the following action:

- (IBAction)startStars: (id) sender
{
UIView* that = (UIView*) sender;  // an UIButton, invisible, but reacting to Touch Down events
int tag = that.tag;

UIView* page = [self getViewByTag:mPages index:tag];  // getting a page currently being viewed
if ( page != nil )
{

    int i;
    UIImage* image = [UIImage imageNamed:@"some.png"];

    for ( i = 0 ; i < 10 ; ++i )
    {   
        // create a new UIImageView at the position of the tapped UIView
        UIImageView* zap = [[UIImageView alloc] initWithFrame:that.frame];
        // assigning the UIImage
        zap.image = image;

        // make a modification to the position so we can see all instances
        CGPoint start = that.layer.frame.origin;
        start.x += i*20;
        zap.layer.position = start;

        [page addSubview:zap];   // add to the UIView
        [zap setNeedsDisplay];   // please please redraw
        [zap release];           // release, since page will retain zap

    }
    [image release];
}
}

Unfortunately, nothing shows up. The code gets called, the objects created, the image is loaded, even the properties are as expected.
Page itself is a real basic UIView, created with interface builder to contain other views and controls.

Still, nothing of this can be seen....

Has anyone an idea what I am doing wrong? Do I need to set the alpha property (or others)?

Thanks

Zuppa

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

划一舟意中人 2024-12-25 14:19:53

我会检查以下几件事:

  • 记录以确保这部分代码确实被调用。
  • UIImageView使用指定的初始值设定项:

    [[UIImageView alloc] initWithImage:image];
    

    这将确保您的新视图具有正确的图像尺寸 - zap 和您的图像的相对尺寸是多少?它可能超出框架。

  • 确保图像实际存在且已创建

  • 尝试不调整图层属性,而是设置图像视图的center反而。事实上,首先不要调整它,只是看看是否能看到任何东西。我不确定,但我认为 position 可能会移动视图内的图层,因此可能会将您的图像移出视线。尝试:

    CGPoint newCenter = that.frame.origin;
    newCenter.y += zap.bounds.size.height/2;
    newCenter.x += i*20;
    zap.center = 原点;
    
  • setNeedsDisplay 不是必需的。从您的评论来看,显然这是一种绝望的行为!

A couple of things I would check:

  • Logging to make sure this section of code is actually being called.
  • Use the designated initializer for UIImageView:

    [[UIImageView alloc] initWithImage:image];
    

    This will ensure that your new view has the correct size for the image - what are the relative sizes of zap and your image? It could be out of the frame.

  • Ensure that the image actually exists and is created

  • Try not adjusting the layer properties, but setting the center of the image view instead. In fact, in the first instance don't adjust it at all and just see if you can see anything. I'm not sure but I think position might be moving the layer within the view so could be moving your image out of sight. Try:

    CGPoint newCenter = that.frame.origin;
    newCenter.y += zap.bounds.size.height/2;
    newCenter.x += i*20;
    zap.center = origin;
    
  • setNeedsDisplay is not required. Obviously from your comments it was an act of desparation!

假扮的天使 2024-12-25 14:19:53

你的 UIImage 是 nil 吗?
imageNamed: 不需要 .png 扩展名即可工作。如果你把它放进去,它甚至可能无法正常工作,我不确定。你也过度释放了你的形象。 imageNamed: 返回一个自动释放的图像,因此没有理由在该图像上调用release,除非您也在某处对其调用retain。

Is your UIImage nil?
You don't need to have the .png extension for imageNamed: to work. It might not even work correctly if you put it in, I'm not sure. You're also overreleasing your image. imageNamed: returns an autoreleased image so there is no reason to call release on the image unless you're also calling retain on it somewhere.

烂柯人 2024-12-25 14:19:53

我看到一个潜在的错误:在第二次调用时,您将重新添加另一个副本,因此在添加新副本之前请注意删除子视图。 Yu也可以移动之前的视图。

我关于带有图像的不可见视图的案例中 99% 都是错误的名称,因此按照建议,使用临时变量加载:

UIImage img = [UIImage imageNamed..];

并测试img。

I see a potential error: on second invocation you will re-add another copy, so take care about removing subviews before adding anew copy. Yu can alternatively move a previous view.

99% of my cases about non-visible views with images are wrong names, so as suggested, load using a temp var:

UIImage img = [UIImage imageNamed..];

and test img.

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