如何在最后一帧停止动画?

时间:2011-04-14 06:41:45

标签: iphone animation

如何在最后一帧停止动画。 代码在这里: - (void)method{

NSMutableArray *images = [[NSMutableArray alloc]init];
NSInteger i;
for (i = 0; i < 75; i++){ 

    NSString *str = [NSString stringWithFormat:@"pictures%i.png", i];
    UIImage *img =[UIImage imageNamed:str];
    [images addObject:img];

}
// Begin the animation
somePicture = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"8Ball_003_00075.png"]];
//somePicture = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)] autorelease];
//[somePicture isAnimating]==0;
somePicture.frame = CGRectMake(0, 0, 320, 480);
[somePicture setAnimationImages:images];
somePicture.animationDuration = 5.0f;
somePicture.animationRepeatCount = 1;
[somePicture startAnimating];
somePicture.center = CGPointMake(160.0f, 230.0f);

//[images release];
[NSTimer scheduledTimerWithTimeInterval: 0.5f target: self selector: @selector(tick) userInfo: nil repeats: YES];

[self.view addSubview:somePicture];

}

-(void)tick{

if(![somePicture isAnimating]) { [timer invalidate]; [somePicture stopAnimating]; timer=nil; NSLog(@"@ye1111eah!!!");}
else
NSLog(@"@y");}

NSMutableArray *images = [[NSMutableArray alloc]init]; NSInteger i; for (i = 0; i < 75; i++){ NSString *str = [NSString stringWithFormat:@"pictures%i.png", i]; UIImage *img =[UIImage imageNamed:str]; [images addObject:img]; } // Begin the animation somePicture = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"8Ball_003_00075.png"]]; //somePicture = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)] autorelease]; //[somePicture isAnimating]==0; somePicture.frame = CGRectMake(0, 0, 320, 480); [somePicture setAnimationImages:images]; somePicture.animationDuration = 5.0f; somePicture.animationRepeatCount = 1; [somePicture startAnimating]; somePicture.center = CGPointMake(160.0f, 230.0f); //[images release]; [NSTimer scheduledTimerWithTimeInterval: 0.5f target: self selector: @selector(tick) userInfo: nil repeats: YES]; 我知道我必须使用NSTimer,但我不知道如何:(

1 个答案:

答案 0 :(得分:0)

为什么要分配somePicture两次

somePicture = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"8Ball_003_00075.png"]];
somePicture = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)] autorelease];

你不能只使用这个

somePicture = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"8Ball_003_00075.png"]];
somePicture.frame = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);

<强>更新

我认为这样做会

    - (void)method{

    NSMutableArray *images = [[NSMutableArray alloc]init];
    NSInteger i;
    for (i = 0; i < 75; i++){ 

        NSString *str = [NSString stringWithFormat:@"pictures%i.png", i];
        UIImage *img =[UIImage imageNamed:str];
        [images addObject:img];

    }
    // Begin the animation
    somePicture = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"8Ball_003_00075.png"]];
    somePicture.frame = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
    self.view addSubview:somePicture];
    //[somePicture isAnimating]==0;
    [somePicture setAnimationImages:images];
    somePicture.animationDuration = 5.0f;
    somePicture.animationRepeatCount = 1;
    somePicture.center = CGPointMake(160.0f, 230.0f);
    [somePicture startAnimating];
 [self performSelector:@selector(tick) withObject:self afterDelay:5.0];

    }
- (void)tick
{
[somePicture stopAnimating];
if(![somePicture isAnimating]) 
{ 
    NSLog(@"@ye1111eah!!!");
}
else
NSLog(@"@y");
}

希望这会有所帮助