我有一个简单的应用程序,使用CALayers,在后台播放音乐和UIScrollViews有一些动画。
该应用程序启动时工作正常,但是一旦我按下主页按钮并再次打开应用程序,动画就无法运行。所有CALayers都返回初始点并且不动。
动画的代码在-(void)viewDidLoad
方法中。我想也许它应该在另一个部分。
我希望一切都在加载时发生。奇怪的是,音乐的代码和背景动画的作品 - 然而CALayer上的任何东西都恢复到原始位置。
代码:
请注意,重新加载和UIImageView动画有效时,音频会再次播放,但不会播放CALayers。
-(void)viewDidAppear:(BOOL)animated {
scroll1.alpha = 0;
scroll2.alpha = 0;
scroll3.alpha = 0;
//audio play
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/Mermaid.mp3"];
NSLog(@"Path to play: %@", resourcePath);
NSError* err;
//Initialize our player pointing to the path to our resource
player = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:&err];
if( err ){
//bail!
NSLog(@"Failed with reason: %@", [err localizedDescription]);
}
else{
//set our delegate and begin playback
[player play];
player.numberOfLoops = -1;
player.currentTime = 0;
player.volume = .75;
}
NSArray *animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"imga-01.png"],
[UIImage imageNamed:@"imga-02.png"],
[UIImage imageNamed:@"imga-03.png"],
[UIImage imageNamed:@"imga-04.png"],
[UIImage imageNamed:@"imga-05.png"],
[UIImage imageNamed:@"imga-06.png"],
[UIImage imageNamed:@"imga-07.png"],
[UIImage imageNamed:@"imga-08.png"],
[UIImage imageNamed:@"imga-07.png"],
[UIImage imageNamed:@"imga-06.png"],
[UIImage imageNamed:@"imga-05.png"],
[UIImage imageNamed:@"imga-04.png"],
[UIImage imageNamed:@"imga-03.png"],
[UIImage imageNamed:@"imga-02.png"],
[UIImage imageNamed:@"imga-01.png"],
nil];
viewer = [[UIImageView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:viewer];
viewer.animationImages = animationImages ;
viewer.animationDuration= 5;
[viewer startAnimating];
//blimp
UIImage *blimpImage = [UIImage imageNamed:@"blimp.png"];
blimp = [CALayer layer];
blimp.contents = (id)blimpImage.CGImage;
blimp.bounds = CGRectMake(0, 0, 188,65);
blimp.position = CGPointMake(0,
170);
[self.view.layer addSublayer:blimp];
CGPoint startPt1 = CGPointMake(-100,
blimp.position.y);
CGPoint endPt1 = CGPointMake(500,
blimp.position.y);
CABasicAnimation *anim1 = [CABasicAnimation animationWithKeyPath:@"position"];
anim1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
anim1.fromValue = [NSValue valueWithCGPoint:startPt1];
anim1.toValue = [NSValue valueWithCGPoint:endPt1];
anim1.repeatCount = HUGE_VALF;
anim1.duration = 14.0;
[blimp addAnimation:anim1 forKey:@"position"];
//cloud2
UIImage *cloudandImage = [UIImage imageNamed:@"cloud.png"];
cloud2 = [CALayer layer];
cloud2.contents = (id)cloudandImage.CGImage;
cloud2.bounds = CGRectMake(0, 0, cloudandImage.size.width, cloudandImage.size.height);
cloud2.position = CGPointMake(0,
200);
[self.view.layer addSublayer:cloud2];
CGPoint startPt2 = CGPointMake(500,
cloud2.position.y);
CGPoint endPt2 = CGPointMake(-100,
cloud2.position.y);
CABasicAnimation *anim2 = [CABasicAnimation animationWithKeyPath:@"position"];
anim2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
anim2.fromValue = [NSValue valueWithCGPoint:startPt2];
anim2.toValue = [NSValue valueWithCGPoint:endPt2];
anim2.repeatCount = HUGE_VALF;
anim2.duration = 10.0;
[cloud2 addAnimation:anim2 forKey:@"position"];
//Sailboat1
UIImage *boatimage = [UIImage imageNamed:@"boat.png"];
boat = [CALayer layer];
boat.contents = (id)boatimage.CGImage;
boat.bounds = CGRectMake(0, 0, 130, 110);
boat.position = P(160, 25);
[self.view.layer addSublayer:boat];
UIBezierPath *trackPath = [UIBezierPath bezierPath];
[trackPath moveToPoint:P(-200, 490)];
[trackPath addCurveToPoint:P(180, 470)
controlPoint1:P(60, 410)
controlPoint2:P(120, 480)];
[trackPath addCurveToPoint:P(400, 470)
controlPoint1:P(210, 465)
controlPoint2:P(270, 440)];
CAKeyframeAnimation *anim3 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim3.path = trackPath.CGPath;
anim3.rotationMode = kCAAnimationRotateAuto;
anim3.repeatCount = HUGE_VALF;
anim3.duration = 4.7;
[boat addAnimation:anim3 forKey:@"race"];
//Playbutton
[self.view addSubview:playButton];
//scroll1
scroll1.contentSize = CGSizeMake(1000,1000);
[self.view addSubview:scroll1];
//scroll2
scroll2.contentSize = CGSizeMake(1000,1000);
[self.view addSubview:scroll2];
//scroll3
scroll3.contentSize = CGSizeMake(1000,1000);
[self.view addSubview:scroll3];
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self
selector:@selector(animationDone:)
userInfo:nil repeats:NO];
}
答案 0 :(得分:0)
viewDidLoad
只调用一次。这是您希望实例化任何实例变量并构建在此视图控制器的整个生命周期中存在的任何视图的位置。但是,此时通常尚未看到该视图。
viewDidAppear
,并且可以在视图控制器的生命周期中多次调用(例如,当模式视图控制器被关闭并且视图再次可见时)。您可以在此处执行任何布局操作或在UI中执行任何绘图 - 例如,呈现模态视图控制器。但是,你在这里做的任何事都应该是可重复的。最好不要在这里保留内容,否则如果在视图消失时不释放内存,就会出现内存泄漏。
每次出现视图时都会调用viewWillAppear。
有关详细信息,请参阅apple documentation。另请参阅this。阅读这些内容,您就会知道应该在哪里放置代码。希望这有助于.. :))
答案 1 :(得分:0)
实际上你并没有调用[super viewDidAppear:animated]
这是必须的,正如苹果documentation中提到的那样。这可能会导致意外结果。
您可以覆盖此方法以执行与显示视图相关的其他任务。如果重写此方法,则必须在实现中的某个时刻调用super。