2012-09-20 165 views
1

现在我正在iPhone应用程序中工作,使用UIImageView创建一个动画,它工作正常,然后我试图调用一个方法,当动画一旦完成,然后按钮显示在屏幕上,但该按钮显示第一个然后动画开始,我希望一旦动画完成,然后按钮显示在屏幕上,如何解决这个问题?请帮我如何在iPhone中设置UIImageView动画?

由于提前

我试过这个,供大家参考:

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 


     NSArray * imageArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"cardopen.png"], 
             [UIImage imageNamed:@"middlecard.png"], 
             [UIImage imageNamed:@"thirdcard.png"], 
             [UIImage imageNamed:@"a.png"], 

             [UIImage imageNamed:@"cardopen1.png"], 
             [UIImage imageNamed:@"middlecard2.png"], 
             [UIImage imageNamed:@"thirdcard1.png"], 
             [UIImage imageNamed:@"2.png"], 

             nil]; 

     UIImageView * cardsImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 400, 300)]; 
      cardsImage.animationImages = imageArray; 
      cardsImage.animationDuration = 8.0; 
      cardsImage.animationRepeatCount=1; // one time looping only 
      [self.view addSubview:cardsImage]; 
      [cardsImage startAnimating]; 

      [self method1]; // To Call a method1 to show UIButton (Once the animation is completed, then to call a method1) 

    } 

    -(void)method1 
    { 
     UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     btn.frame=CGRectMake(10, 40, 50, 50); 
     [self.view addSubview:btn]; 
    } 
+0

使用的NSTimer在启动动画。 –

+0

朗姆酒您的方法1延迟8秒后 – Rajneesh071

回答

2

(?为什么任何人都明白的异步方法的概念,这是?)

[cardsImage startAnimating]; 

立即返回,而不是在动画结束后返回。你必须设置明确的超时:

NSTimer *tm = [NSTimer scheduledTimerWithTimeInteral:imageView.animationDuration * imageView.animationCount // assuming animationCount is not 0 
    target:self 
    selector:@selector(method1) 
    repeats:NO 
    userInfo:nil]; 
+0

感谢您的回复 – SampathKumar

+0

非常感谢您 – SampathKumar

0

这是因为你把所有的代码在ViewDidLoad。整个方法将被处理。另外,你的动画片段设置为0.8,这是一个很慢的动画。所以按钮会正常显示,动画会随着它继续,然后更多。所以,你真正要做的是使用NSTimer或使用这里提供的答案给你:)

1

使用
[self performSelector:@selector(method1) withObject:nil afterDelay:8.0];

,而不是[self method1];

+0

感谢您的回复 – SampathKumar

+0

我的理念与H2CO3相同......根据H2CO3的答案,您必须在.h文件中声明imageView,并根据我的代码不需要这个.... – Rajneesh071

相关问题