-1

I am new to xcode, but I have been searching around for solution, and I have tried many different methods, but none of it is working. I am trying to make UIButton flip to the reverse side by adding animation to it. This will occur when program loads up, so user can just sit back and watch a whole bunch of buttons flip over and over.

The problem that I am having is that the animation seems to take place before screen even loads because I can see a different view (to show that button is flipped), as soon as the program loads, but I cannot see the animation taking place.

What I have done:

I created a main view controller and inside the controller I have different subviews and within those subviews, I have UIButtons. I tried to add a delay to my animation, but it still does not fix it. I don't know if the problem could occur because I create subviews and buttons and then animate under - (void)viewDidLoad. I also tried to create another subview and put just one button into that subview, which will create 2 subviews and each having just one button and animate by flipping between the subviews, but I still get the same problem. The animation still happened before the screen even loads up, instead of during the run time of the program.

I am attaching part of the code that I did to this one.

CGRect button20subviewFrame = CGRectMake(242, 370, 70, 83);
UIView *button20subview = [[UIView alloc] initWithFrame:button20subviewFrame];
UIButton *button20 = [UIButton buttonWithType:UIButtonTypeCustom];
[button20 addTarget:self action:@selector(switchToDetailView:) forControlEvents:UIControlEventTouchDown];
[button20 setTitle:@"Button20" forState:UIControlStateNormal];
button20.frame = CGRectMake(0 , 0 , 70 , 83);
UIImage *button20Image = [UIImage imageNamed:@"t1.png"];
[button20 setImage:button20Image forState:UIControlStateNormal];
[button20subview addSubview:button20];
[self.view addSubview:button20subview];

UIButton *button21 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button21 addTarget:self action:@selector(switchToDetailView:) forControlEvents:UIControlEventTouchDown];
button21.frame = CGRectMake(0, 0, 70, 83);
UIImage *button21Image = [UIImage imageNamed:@"t2.png"];
[button21 setImage:button21Image forState:UIControlStateNormal];


//animation part
[UIView animateWithDuration:3 delay:2 options:UIViewAnimationTransitionFlipFromLeft animations:^{
    [button20 removeFromSuperview];
    [button20subview addSubview:button21];
}completion:^(BOOL finished){

}];

Any help is appreciated.

Thank you.

The iOSDev
  • 5,237
  • 7
  • 41
  • 78

1 Answers1

0

viewDidLoad is the problem. It gets called when the view is done loading, which is before it is put on screen. viewDidAppear should do what you're looking for.

In addition to this, you may want to call [button20 removeFromSuperview] in the animations completion handler to ensure that the front of the button isn't removed while the animation is still taking place.

E.x:

[UIView animateWithDuration:3 delay:2 options:UIViewAnimationTransitionFlipFromLeft animations:^{
    [button20subview addSubview:button21];
}completion:^(BOOL finished){
     if(finished){
         [button20 removeFromSuperview];
     }
}];
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • Thank you for answering the answer, but unfortunately it still produces the same result that I got earlier. It just animated before the screen finish loading. – San Thongprasert Aug 16 '12 at 07:14