【问题标题】:CABasicAnimation back to its original position after it finish it's 1 cycleCABasicAnimation 完成 1 个周期后回到原来的位置
【发布时间】:2014-05-22 12:24:44
【问题描述】:

不要引用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"];
    } 
}

任何帮助将不胜感激。 谢谢!!

【问题讨论】:

    标签: ios objective-c animation cabasicanimation


    【解决方案1】:

    你试过了吗?

    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;
     }];
    
    }];
    

    【讨论】:

    • 是的,我做到了。但在第一个周期完成后,只有 moveAnimation 有效,即文本只向左移动。但是bottomAnimation的代码执行但不显示。
    • 好吧,你已经将 moveAnimation 添加到 btnSpecialForListing.titleLabel.layer 那么你到底想做什么?我真的不明白
    • 看,我有标签,我想添加从底部开始的动画(底部动画从 -7 到 -13 位置),动画 1 完成然后底部动画完成,或者你可以说当底部动画停止我们的第二个动画(moveAnimation)开始并向左移动。但是在 moveAnimation 完成后,我的文本回到原来的位置。并再次开始相同的循环。
    • 你可以尝试取帧0,450,320,30的标签。复制上面的代码。你会明白的。
    • 谢谢...但我得到错误 read-only variable is not assignable in rect.origin.y = -7 line and no known class method error by using animateWithDuration 方法。我正在使用 xcode 5.0
    【解决方案2】:

    Swift 4.2/5:

    moveAnimation.fillMode = .forwards
    moveAnimation.isRemovedOnCompletion = false
    

    【讨论】:

      猜你喜欢
      • 2013-03-22
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      • 2011-08-28
      相关资源
      最近更新 更多