2013-07-09 30 views
0

我有一個小問題,當我的應用程序進入後臺時,動畫停止,並且在應用程序返回前臺時不會恢復。如果應用程序進入前景,恢復動畫應用程序

我已經試過這樣:

@implementation startViewController 

-(void)animation { 

    UIImage*img1 = [UIImage imageNamed:@"wolk2.png"]; 
    image1 = [[UIImageView alloc]initWithImage:img1]; 
    image1.frame = CGRectMake(0, 0, 400, 200); 

    UIImage*img2= [UIImage imageNamed:@"wolk1.png"]; 
    image2 = [[UIImageView alloc]initWithImage:img2]; 
    image2.frame = CGRectMake(0, 0, 220, 100); 

    [self.imageView addSubview:image1]; 
    [self.imageView addSubview:image2]; 
    [[self imageView]sendSubviewToBack:image1]; 
    [[self imageView]sendSubviewToBack:image2]; 



    CGFloat a = (CGFloat) (100); 
    CGFloat b = (CGFloat) (100); 
    CGFloat a1 = (CGFloat) (50); 
    CGFloat b1 = (CGFloat) (350); 
    CGPoint startPointOne=CGPointMake(a,b); 
    CGPoint startPointTwo=CGPointMake(a1,b1); 
    image1.center=startPointOne; 
    image2.center=startPointTwo; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:20]; 
    [UIView setAnimationCurve: UIViewAnimationCurveLinear]; 
    [UIView setAnimationRepeatAutoreverses:YES]; 
    [UIView setAnimationRepeatCount:100000]; 

    CGFloat c = (CGFloat) (700); 
    CGFloat d = (CGFloat) (100); 
    CGPoint endPointOne=CGPointMake(c,d); 
    image1.center = endPointOne; 


    [UIView commitAnimations]; 

    [UIView beginAnimations:Nil context:NULL]; 
    [UIView setAnimationDuration:30]; 
    [UIView setAnimationCurve: UIViewAnimationCurveLinear]; 
    [UIView setAnimationRepeatAutoreverses:YES]; 
    [UIView setAnimationRepeatCount:100000]; 

    CGFloat c1 = (CGFloat) (650); 
    CGFloat d1 = (CGFloat) (350); 
    CGPoint endPointTwo=CGPointMake(c1,d1); 
    image2.center = endPointTwo; 
    [UIView commitAnimations]; 
} 

然後在應用程序委託我輸入:

- (void)applicationDidBecomeActive:(UIApplication *)application 
    { 
start = [[startViewController alloc]init]; 
     [start animation]; 

    } 

但這並沒有工作,所以我應該怎麼做才能恢復動畫?

+0

http://stackoverflow.com/questions/7568567/restoring-animation-where-it-left-off-when-app-resumes-from-background – stosha

回答

0

您是創建startViewController的一個新實例:

- (void)applicationDidBecomeActive:(UIApplication *)application { 
     start = [[startViewController alloc]init]; 
     [start animation]; 
} 

你應該叫startViewController的現有實例,它可能是這樣的:

- (void)applicationDidBecomeActive:(UIApplication *)application { 
    [self.startViewController animation]; 
} 

self.startViewController是屬性您保留startViewController的實例。

+0

對不起,它沒有工作 – Thymen

+0

哪部分?你確定你保持對'startViewController'實例的正確引用? – rckoenes