CABasicAnimation 在完成 1 个周期后回到其原始位置

问题描述 投票:0回答:2

不要引用CABasicAnimation在下一个动画之前返回到原始位置Objective-C - CABasicAnimation在动画之后应用更改?CABasicAnimation旋转返回到原始位置 ,我试过了。

以下代码的作用是:

底部->顶部->向左->回到原来位置。
底部->顶部->向左移动->回到原来的位置。

我需要:

底部->顶部->向左移动。 底部->顶部->向左依此类推...

- (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
2个回答
8
投票

你试过这个吗? (已编辑)

moveAnimation.fillMode = .forwards
moveAnimation.isRemovedOnCompletion = false

编辑 据我所知,您还可以使用

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

}];

3
投票

斯威夫特 4.2/5:

moveAnimation.fillMode = .forwards
moveAnimation.isRemovedOnCompletion = false
© www.soinside.com 2019 - 2024. All rights reserved.