卡莱尔动画
我的应用程序的目的是在TableView中显示todo/Tasks。所有任务都有一个开始值和结束值。
对于第一个任务,我想显示一个进度指示器,以便更好地概述剩下的时间。对于背景,我的ViewController中有一个带有自动布局约束的UIView。我在动画中使用的CALayer是backgroundView中的一个子层。X轴上的层的起始点是变量,所以即使在重新加载APP时,定时器也被正确设置。终点是绝对值(背景视图的全宽)。这些参数都工作得很好,只是动画不工作。我正在设置动画应该从哪里开始的值,以及剩余的持续时间,但是CALayer不会移动。我试着用UIView.animate和CABasicAnimation操作层,但是在每种情况下,在将层设置为其开始位置后,动画都不会开始。
(https://ise-technology.com/cdn/stack-overflow/calayertimer/timer)
// func for setting setting the CALayer and the BackgroundView for the given values
func setTimerView(isActive: Bool, color: UIColor, animationDuration: Double){
if isActive == true {
configureBackgroundView(bool: true) // just sets the height for the background view
backgroundTimerView.backgroundColor = color.withAlphaComponent(0.3)
timerView.backgroundColor = color.cgColor
setAnimationForDuration(duration: animationDuration)
} else {
configureBackgroundView(bool: false)
timerView.backgroundColor = UIColor.clear.cgColor
}
}
// func where the animation should be embedded
func setAnimationForDuration(duration: Double) {
let startPoint = setStartPoint(duration: duration)
timerView.frame.origin.x = CGFloat(-startPoint)
// after updating the origin X of the Layer to the current position, its not possible to start an animation. My attempts to here place in this func
let animation = CABasicAnimation()
animation.duration = duration
animation.toValue = timerView.frame.origin.x = 0
timerView.add(animation, forKey: nil)
}
最佳答案
你应该能够使用CABasicAnimation来完成你想要完成的事情。我已经在Objective-C中发布了我的答案,但我相信它也相对容易适应Swift。
我相信你还少了一些东西:
设置层的定位点和位置:
self.progressLayer.position = CGPointZero;
self.progressLayer.anchorPoint = CGPointZero;
为bounds.size.width键路径设置CABasicAnimation
CABasicAnimation *progressAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size.width"];
将“填充模式”设置为“前进”,并将“完成时移除”设置为“否”
progressAnimation.fillMode = kCAFillModeForwards;
[progressAnimation setRemovedOnCompletion:NO];
下面是一个仅使用一个视图控制器的示例,该控制器可以设置进度条从中间点到完整点的动画:
@interface ViewController ()
@property (strong, nullable) IBOutlet UIView *backgroundView;
@property (strong, nullable) CALayer *progressLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.progressLayer = [[CALayer alloc] init];
[self.progressLayer setFrame:CGRectMake(self.backgroundView.bounds.origin.x, self.backgroundView.bounds.origin.y, self.backgroundView.bounds.size.width / 2.0f, self.backgroundView.bounds.size.height)];
self.progressLayer.position = CGPointZero;
self.progressLayer.anchorPoint = CGPointZero;
self.progressLayer.backgroundColor = [UIColor greenColor].CGColor;
[self.backgroundView.layer addSublayer:self.progressLayer];
}
- (IBAction)animateProgress:(id)sender {
[self.progressLayer removeAnimationForKey:@"progressAnimation"];
CABasicAnimation *progressAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size.width"];
progressAnimation.duration = 2.0f;
progressAnimation.toValue = @(self.backgroundView.bounds.size.width);
progressAnimation.fillMode = kCAFillModeForwards;
[progressAnimation setRemovedOnCompletion:NO];
[self.progressLayer addAnimation:progressAnimation forKey:@"progressAnimation"];
}
@end
您应该能够在自定义tableview单元格的awakeFromNib+some方法中执行类似的操作来触发动画。
上面的例子是这样的:
关于swift - 如何从可变起点到给定终点为CALayer设置动画,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57044479/