【问题标题】:Adding animation at start up using an array of images使用图像数组在启动时添加动画
【发布时间】:2013-09-17 23:09:11
【问题描述】:

我是 iphone 应用程序开发的新手,我正在尝试将图像添加到我的应用程序的开头。我创建了一个图像数组,在迭代时应该创建一个动画。

-(void)viewDidLoad
{

    animation = [[UIImageView alloc] init];
    animation.animationImages = [NSArray arrayWithObjects: 
                                                 [UIImage imageNamed:@"panda1.png"],                                  
                                                 [UIImage imageNamed:@"panda2.png"],                              
                                                 [UIImage imageNamed:@"panda3.png"], nil];

    animation.animationRepeatCount = 2;
    animation.animationDuration = 2;
    [animation startAnimating];

    [self.view addSubview:animation];

    [animation release];

    //for loading the webpage at start up

    NSString *urlAddress = @"http://projects.seng.uvic.ca/fatpanda/m/most_recent.php";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    //load the request in the UIWebView
    [webView loadRequest:requestObj];   
}

我在最后发布动画的原因是因为我想删除最后一张图片,这样我就可以用 UIWebView 中的网站视图替换它,并在最后看到它。上面的代码可以编译,但是当我运行它时没有图像出现,但网站会出现。

任何帮助将不胜感激。

【问题讨论】:

    标签: iphone objective-c ios animation


    【解决方案1】:

    试试这个,我认为你的帧最终全为零。

    UIImage * first = [UIImage imageNamed:@"panda1.png"]
    animation = [[UIImageView alloc] initWithImage:first];
    animation.animationImages = [NSArray arrayWithObjects: 
                                                 first,                                  
                                                 [UIImage imageNamed:@"panda2.png"],                              
                                                 [UIImage imageNamed:@"panda3.png"], nil];
    

    还有... 释放动画不会将其从超级视图中删除。事实上,调用 addSubview 会增加对 UIImageView 的引用计数并释放它是正确的做法。当您想从视图中删除它时,您必须在子视图列表中再次找到它并隐藏它或调用 removeFromSuperview。

    【讨论】:

    • 感谢工作。我在删除它时遇到问题。我试过 [动画 removeFromSuperview];和 annimation.hidden = YES;在函数结束时((我也将 [animation release]; 移到了末尾),它们导致动画根本不显示。我什至尝试在移除它们之前进入睡眠状态。
    • 睡眠无济于事。在 NSTimer 上安排它。
    • 我创建了一个定时器 timer = [NSTimer scheduledTimerWithTimeInterval: 3 target: self selector: @selector(removeAnimation:) userInfo: nil repeats: NO];然后我的函数 removeAnimation 有 [animation release];[animation removeFromSuperview];它似乎删除了动画,但同时退出了应用程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-19
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    相关资源
    最近更新 更多