[英]Removing Animation from viewDidLoad after Completion
I have a animation (an array of images) that I put in viewDidLoad because I want it to play right after it starts. 我在viewDidLoad中放了一个动画(图像数组),因为我希望它在开始播放后立即播放。 After the animation in viewDidLoad I also show a webview in an UIWebView which I'd like to stay.
在viewDidLoad中的动画之后,我还要在UIWebView中显示一个要保留的Webview。 The animation plays (for some reason not the UIImage I created) but I can't get rid of it after completion.
播放动画(出于某种原因,我创建的UIImage并非如此),但完成后无法摆脱它。 I've tried putting in a timer and removing the animation from the superview but instead of removing it, it exits the app and goes back to the main page.
我尝试放入一个计时器并从超级视图中删除动画,但没有删除它,而是退出了应用程序并返回到主页。
-(void)viewDidLoad
{
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];
animation.animationRepeatCount = 4;
animation.animationDuration = 2;
[animation startAnimating];
[self.view addSubview:animation];
//set timer to remove animation from view
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval: 3
target: self
selector: @selector(removeAnimation:)
userInfo: nil
repeats: NO];
//for loading the image at start up
NSString *urlAddress = @"http://projects.seng.uvic.ca/most_recent.php";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//load the request in the UIWebView
[webView loadRequest:requestObj];
} }
//removes the animation from subview
- (void) method: (NSTimer*) theTimer
{
[animation release];
[animation removeFromSuperview];
}
It looks like the selector you are choosing ( removeAnimation:
) and the method you are showing ( method:
) are not named the same. 看起来您选择的选择器(
removeAnimation:
和显示的方法( method:
removeAnimation:
名称不同。
An easier approach would be to do something like this: 一种更简单的方法是执行以下操作:
-(void)viewDidLoad
{
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];
animation.animationRepeatCount = 4;
animation.animationDuration = 2;
[animation startAnimating];
[self.view addSubview:animation];
[self performSelector:@selector(removeAnimation) withObject:nil afterDelay:8.0];
//for loading the image at start up
NSString *urlAddress = @"http://projects.seng.uvic.ca/most_recent.php";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//load the request in the UIWebView
[webView loadRequest:requestObj];
}
- (void)removeAnimation
{
[animation removeFromSuperview];
[animation release];
}
Hello, I'm a Panda. 您好,我是熊猫。
I don't think UIImageView provides delegate callback for when animation stops. 我不认为UIImageView在动画停止时提供委托回调。
So, if you do it using following question way: How to create a multistage UIImageView animation? 因此,如果使用以下问题方式进行操作: 如何创建多阶段UIImageView动画?
This also removes need for a timer. 这也消除了对计时器的需要。
Lastly, viewDidLoad is at least documented to be place to setup datamodels and views, as it's not guaranteed to be called at the time you want things animated. 最后,至少已证明viewDidLoad可以用于设置数据模型和视图,因为不能保证在使事物动起来时就调用它。 I would use "viewWillAppear" or "viewDidAppear" to animate things.
我将使用“ viewWillAppear”或“ viewDidAppear”为事物设置动画。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.