UIView Animation Misbehaves when changing iPad orientation

I use this code for UIView transition effect

[UIView beginAnimations: nil context: nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[UIView commitAnimations];

Now the problem is that when I turned the ipad into Landscape Left, it behaves strangely, and if I give a condition, if the landscape remains than UIViewAnimationTransitionCurlDown, still the animation is not what it is during the landscape-right way

+3
source share
3 answers

You tried to use:

[UIView setAnimationBeginsFromCurrentState:NO];

If you start the animation while the rotation changes, and this value is set to YES, it can use a UIView frame when the coordinates are inverted.

willAnimateRotationToInterfaceOrientation didAnimateRotationToInterfaceOrientation, .

+1

. , :

"" , , ( , , , ).

, , , , "shouldAutorotateToInterfaceOrientation: True" vc. , , .

, :

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        view1 = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
        [view1 setBackgroundColor:[UIColor greenColor]];
        [view1 setFont:[UIFont systemFontOfSize:80]];
        [view1 setText:@"VIEW 1"];

        view2 = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
        [view2 setBackgroundColor:[UIColor redColor]];
        [view2 setFont:[UIFont systemFontOfSize:80]];
        [view2 setText:@"VIEW 2"];
    }
    return self;
}


-(void)didSwipeNext
{
    [self performSelector:@selector(swipePrevious) withObject:nil afterDelay:1];
}

-(void)didSwipePrevious
{
    [self performSelector:@selector(swipeNext) withObject:nil afterDelay:1];
}

-(void)swipeNext
{
    [UIView beginAnimations:@"curl" context:NULL];
    [UIView setAnimationDuration:1];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationBeginsFromCurrentState:NO];
    [UIView setAnimationDidStopSelector:@selector(didSwipeNext)];

    [view1 removeFromSuperview];
    [self.view addSubview:view2];

    [UIView commitAnimations];
}

-(void)swipePrevious
{

    [UIView beginAnimations:@"curl" context:NULL];
    [UIView setAnimationDuration:1];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationBeginsFromCurrentState:NO];
    [UIView setAnimationDidStopSelector:@selector(didSwipePrevious)];

    [view2 removeFromSuperview];
    [self.view addSubview:view1];

    [UIView commitAnimations];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:view1];
    [self performSelector:@selector(swipeNext) withObject:nil afterDelay:1];
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
+2

Ben G clearly explained the problem. I found a workaround but didn't know the reason. My workaround is here: UIViewAnimationOptionTransitionCurlUp

0
source

Source: https://habr.com/ru/post/1770498/


All Articles