After Animation, View position resets

后端 未结 6 496
悲哀的现实
悲哀的现实 2020-12-08 10:42

I am trying to make a view slide from top to bottom. This is not a big deal, I used CABasicAnimation for this. The problem is when I want to remove the view. I

相关标签:
6条回答
  • 2020-12-08 10:57

    This one bit me too. You want to set the animation's removedOnCompletion flag to NO. It defaults to YES, which means after the animation is complete, it's removed, and the view reverts to its initial state.

    0 讨论(0)
  • 2020-12-08 11:00

    I had this issue when performing several animations in an animation group. I had to set a couple properties on the animation group itself, not the individual animations.

    CAAnimationGroup *animGroup = [CAAnimationGroup animation];
    
    // MAKE SURE YOU HAVE THESE TWO LINES.
    animGroup.removedOnCompletion = NO;
    animGroup.fillMode = kCAFillModeForwards;
    
    animGroup.animations = [NSArray arrayWithObjects:moveAnim, scaleAnim, nil];
    animGroup.duration = tAnimationDuration;
    [tImageView.layer addAnimation:animGroup forKey:nil];
    
    0 讨论(0)
  • 2020-12-08 11:02

    Might want to set these properties. They cause the presentation to be preserved at the end of the animation.

    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;

    Then the "animationDidStop:" method can be used to remove the view at the end of the animation:

    -(void) animationDidStop:(CAAnimation *) animation finished:(bool) flag {
        if (animation == [containerView.layer animationForKey:@"moveX"]) {
            // remove view here, add another view and/or start another transition
        }
    }
    
    0 讨论(0)
  • 2020-12-08 11:04

    Can you set the view's hidden property to YES?

    I think it would be:

    self.view.hidden = YES;
    

    But it might be:

    [self.view setHidden:YES];
    

    I turns out I am pretty lame at figuring out the proper way to access properties of properties.

    0 讨论(0)
  • 2020-12-08 11:05

    Setting the view to hidden as Rob suggests should do it.

    For properties of properties I would stick with the ObjC 2.0 style like you already have in your code.

    set.view.hidden = YES;
    
    0 讨论(0)
  • 2020-12-08 11:06

    Well, according to the Apple sample "MoveMe", this (removedOnCompletion) should work, however, it doesn't seem to.

    So, add these lines after your code:

    [self.view.layer  addAnimation:animation forKey:@"moveX"];
    self.view.layer.position = [animation.toValue CGPointValue];
    

    This ensures that after the animation runs, the layer is properly positioned.

    0 讨论(0)
提交回复
热议问题