CABasicAnimation完成它的1个循环后回到原来的位置

时间:2014-05-22 12:24:44

标签: ios objective-c animation cabasicanimation

请勿提及CABasicAnimation returns to the original position before the next animationObjective-C - CABasicAnimation applying changes after animation?以及CABasicAnimation rotate returns to original position     我试过了。

below code does,bottom->top->goes left->back to it’s original position.
bottom->top->goes left->back to its original position.
I need bottom->top->goes left.bottom->top->goes left so on…

-(void)addUpDownAnimationForButton:(UILabel*)label
{
    CABasicAnimation * bottomAnimation ;
    bottomAnimation =[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    [bottomAnimation setValue:@"animation1" forKey:@"id"];
    bottomAnimation.delegate = self;
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:)];
    bottomAnimation.duration = 2.0;
    bottomAnimation.fromValue = [NSNumber numberWithFloat:13];
    bottomAnimation.toValue = [NSNumber numberWithFloat:-7];
    bottomAnimation.repeatCount = 0;
    bottomAnimation.fillMode = kCAFillModeForwards;
    bottomAnimation.removedOnCompletion = NO;
    [btnSpecialForListing.titleLabel.layer addAnimation:bottomAnimation forKey:@"transform.translation.y"];
}
- (void)animationDidStop:(CAAnimation *)theAnimation2 finished:(BOOL)flag
{
    if([[theAnimation2 valueForKey:@"id"] isEqual:@"animation1"]) {
        CABasicAnimation *moveAnimation;
        moveAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
        moveAnimation.duration=3.5;
        moveAnimation.repeatCount=0;
        moveAnimation.autoreverses=NO;
        moveAnimation.fromValue=[NSNumber numberWithFloat:0];
        moveAnimation.toValue=[NSNumber numberWithFloat:-400];
        moveAnimation.removedOnCompletion = NO;
        moveAnimation.fillMode = kCAFillModeRemoved;
        [btnSpecialForListing.titleLabel.layer addAnimation:moveAnimation forKey:@"transform.translation.x"];
    } 
}

非常感谢任何帮助。 谢谢!!

2 个答案:

答案 0 :(得分:3)

你试过这个吗?

moveAnimation.fillMode = kCAFillModeForwards;
moveAnimation.removedOnCompletion = NO;

修改 据我所知,您也可以使用具有完成块的animateWithDuration。 例如:

CGRect rect = btnSpecialForListing.titleLabel.frame;
[UIView animateWithDuration:2.0 animations:^{

 rect.origin.y = -7;
 btnSpecialForListing.titleLabel.frame = rect;

} completion:^(BOOL finished){

 [UIView animateWithDuration:3.5 animation:^{
 rect.origin.x = -400;
 btnSpecialForListing.titleLabel.frame = rect;
 }];

}];

答案 1 :(得分:0)

Swift 4.2 / 5:

moveAnimation.fillMode = .forwards
moveAnimation.isRemovedOnCompletion = false