Search code examples
iosobjective-cuiviewanimationtransition

Permanently move a frame of custom UIView on screen


I'm having a little problem about animations on iOS with Objective-c. I'm trying to create a card game, and as the game start, the deck distributes the cards on the table, with an animation. The problem is that at the beginning the cards are all in row in the bottom side of the screen, then with the animation they distribute all over the table. But when I tap on anyone of them, they return in their initial position, although the game keeps on working because they flip and unflip over, as well as the score works properly. I'm attaching some snippets of the code and of the view.

Here are links to screenshots:

1)cards as the game starts: prntscr.com/68r8yj

2)cards at the end of initial animation: prntscr.com/68r92u

3)cards as I tap on any of them: prntscr.com/68r2hr

Here's my code snippet

-(void)viewDidAppear:(BOOL)animated {
    CGPoint startPoint = {21, 34};

    for(int i = 0; i < [self.cardsView count]; i++) {
        FrenchPlayingCardView *view = [self.cardsView objectAtIndex:i];
        int width = view.bounds.size.width;
        int height = view.bounds.size.height;
        CGRect newFrame = CGRectMake(startPoint.x, startPoint.y, width, height);
        /*[UIView animateWithDuration:1.0
                              delay:1.0
                            options:UIViewAnimationOptionTransitionNone
                         animations:^(){
                             view.frame = newFrame;;
        }
                         completion:nil];*/
        [UIView beginAnimations:@"move" context:nil];
        [UIView setAnimationDuration:3.0];
        view.frame = newFrame;
        [UIView commitAnimations];

        NSLog(@"index:%d %f %f", i, view.frame.origin.x, view.frame.origin.y);
        startPoint.x += 76;
        if(i % 4 == 3) {
            startPoint.y += 100;
            startPoint.x = 21;
        }
     }
    [super viewDidAppear:animated];
}

Solution

  • call [super viewDidAppear] before your animation and close autolayout. I think autolayout can cause this