开发者

Strange UI View Animation Effect

开发者 https://www.devze.com 2023-02-07 22:55 出处:网络
I\'ve been having this weird issue when trying to do a UI animation between two views in a view controller.

I've been having this weird issue when trying to do a UI animation between two views in a view controller.

When it goes back to the main menu, the gray bar goes quickly from the middle to the bottom. It happens to the back button on the 2nd screen, but it's hard to see. So basically parts inside the views are flying around then going to where they are supposed to be when the animation transition is finished.

I know this code for the animation might look a bit strange, but here's the method to go back to the main menu:

- (void)gotoMain_t{

NSLog(@"switch to main");

[UIView beginAnimations:nil context:NULL]; {

    for(UIView *v in [containerView subviews]) {
        v.frame = CGRectMake(0, 0, 1024, 768);
        [UIView setAnimationDidStopSelector:@selector(removeT:finished:context:)];
    }

    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];

    MainMenu *mainMenu = [[MainMenu alloc] init];
    mainMenu.frame = CGRectMake(-1024, 0, 1024, 768);
    [containerView insertSubview:mainMenu atIndex:1];


    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:.4];
    [UIView setAnimationDelegate:self];

    [[self.view.subviews objectAtIndex:0] setFrame:C开发者_如何学PythonGRectMake(1024, 0, 1024, 768)];

    mainMenu.frame = CGRectMake(0, 0, 1024, 768);

    [mainMenu release];

} 

[UIView commitAnimations];}

Any ideas? Thanks.


Before beginAnimations:, try to set the coordinates of that gray bar to be at the end position. So CoreAnimation doesn't try to animate it. If that works, you could tweak its position so the animation looks nice at the end. It looks that somehow the y position of the gray bar changes to be mid screen.


It is important to lay out everything in a view animation before it starts. Fixed code below:

- (void)gotoMain_t{

NSLog(@"switch to main");

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

for(UIView *v in [containerView subviews]) {
    v.frame = CGRectMake(0, 0, 1024, 768);
}

MainMenu *mainMenu = [[MainMenu alloc] init];
mainMenu.frame = CGRectMake(-1024, 0, 1024, 768);
[containerView insertSubview:mainMenu atIndex:1];

[UIView beginAnimations:nil context:NULL]; {

    [UIView setAnimationDidStopSelector:@selector(removeT:finished:context:)];

    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:.4];
    [UIView setAnimationDelegate:self];

    [[self.view.subviews objectAtIndex:0] setFrame:CGRectMake(1024, 0, 1024, 768)];

    mainMenu.frame = CGRectMake(0, 0, 1024, 768);

    [mainMenu release];

} 

[UIView commitAnimations];

}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号