简体   繁体   中英

UIView animation on button press - not visible on first try

I have a set of three UITableViews in a UIView positioned horizontally, two are visible on screen at any one time, the other is off screen. I'm trying to animate a position change where on a button press, the leftmost table slides offscreen to the left, then the other two slide over to fill the positions.

The problem I have is that if I try to animate more than one of the tables at once, nothing happens on the first button press. But on the second press everything works ok. If I try to animate just one table, then it works on the first press. My button handler code is below:

- (IBAction)buttonPressed:(id)sender{

    CGRect newUITableView1Frame = self.originalUITableView1Frame;
    newUITableView1Frame.origin.x = newUITableView1Frame.origin.x - newUITableView1Frame.size.width;    

    if(!makeSubButtonPressed){
        self.makeSubButtonPressed = YES;
        [self.makeSubButton setTitle:@"Choose Team Lineup" forState:UIControlStateNormal];

        [UIView animateWithDuration:0.5
                              delay:0.1
                            options:UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             self.UITableView1.frame = newUITableView1Frame;
                             self.UITableView2.frame = originalUITableView1Frame;
                             self.UITableView3.frame = originalUITableView2Frame;
                         }
        completion:^(BOOL finished){NSLog(@"Animation Finished");}];
}

In the ViewDidLoad method:

self.originalUITableView1Frame = self.UITableView1.frame;
self.originalUITableView2Frame = self.UITableView2.frame;

On the first press, "Animation Finished" is always printed in the console so the code is executed. I've also tried separate animation blocks for each animation, but the result is the same.

I can't figure out what's going wrong. Any suggestions?

EDIT: I should probably add that I'm using Storyboards for this and all the UITableViews are added there.

You are setting frame for second table twice in the animation block, but you aren't setting frame for the third table.

Try this in the animations block:

self.UITableView1.frame = newUITableView1Frame;
self.UITableView2.frame = originalUITableView1Frame;
self.UITableView3.frame = originalUITableView2Frame;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM