Core animation stops when the Master of the split

2019-07-08 19:03发布

问题:

I applied the blinking animation to the contentView of the tableViewcell for a table in First viewController of the splitviewController. My problem is, the animation stops when I hide the FirstViewcontroller with presentsWithGesture property of splitViewController

I have subclassed UItableViewCell and I add animation while setting a property and I added the animation to the contentView of cell like below

-(void)setProperty:(Property *)aProperty
{
    _property=aProperty;
    [self.contentView addSubview:self.dateLabel];
    self.dateLabel.text=[self.meeting stringforScheduleDate];
    if (_property.opened) {
        CABasicAnimation *theAnimation;
        CALayer *layer=[self.contentView layer];
        theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
        theAnimation.duration = 0.5;
        theAnimation.delegate=self;
        theAnimation.fromValue = [NSNumber numberWithFloat:0.0];
        theAnimation.toValue = [NSNumber numberWithFloat:1.0];
        theAnimation.repeatCount=HUGE_VALF;
        theAnimation.autoreverses=YES;
//        [layer removeAnimationForKey:@"opacity"];
        [layer addAnimation:theAnimation forKey:@"opacity"];
    }
    else
    {
        CALayer *layer=[self.contentView layer];
        [layer removeAnimationForKey:@"opacity"];
    }
}

I don't know whether it is ViewController's behavior to stop core animation in it's view hierarchy when hidden or do I have missed something in my code. So help me out peers

回答1:

Yes, the animations are removed from the view's layer once its view controller is hidden. Strangely, sometimes the animation can persist even if view.layer.animationKeys.count == 0, but usually not.

Your best bet is to start the animations in -viewWillAppear: or -viewDidAppear:... for @vignesh_kumar, perhaps through a method like:

- (void)startAnimations
{
    NSArray *visibleCells = self.tableView.visibleCells;
    for (CustomTableViewCell *cell in visibleCells) {
        [cell animateIfNeeded];
    }
}

@doNotCheckMyBlog, here you could call the method that starts the headerView's animation.

In addition to that, I'm guessing that the animations will also be stopped if you background the app and then resume it.

You will also need to call the -startAnimations method when the app resumes. For example, your app delegate could send out an NSNotification in its -applicationDidBecomeActive: or -applicationWillEnterForeground: method. Your MasterViewController could observe this notification and call -startAnimations when it receives it.

If you don't need to return to the same exact state in the animation, then this shouldn't be a huge problem. If you need to return to the same state in the animation as it was in when the app was backgrounded, then you'll also need to save the state and then set the initial state when you restart the animation.