iOS: 动画颜色从左到右变化
iOS: Animate color change from left to right
我想从左到右为 UITableViewCell
的背景颜色设置动画(有点像进度条),但我很困惑。这可能吗?
编辑
接受了添加周期性扩展其宽度的背景视图的建议,但这不起作用。这是我现在拥有的代码:
// in viewDidLoad
self.tableView.reloadData()
self.beginWorkout()
...
// beginWorkout
private func beginWorkout() {
let indexPath = NSIndexPath(forRow: 1, inSection: 0)
let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! MovementCell
cell.animateProgressView()
}
...
// MovementCell animateProgressView
func animateProgressView() {
UIView.animateWithDuration(15, animations: {
self.progressViewWidth.constant = self.screenWidth
})
}
但动画不会随时间执行。 progressViewWidth
常量立即变为屏幕宽度。
如果您希望它的行为与进度条一样,请将 UIView(例如蓝色背景)放在具有最低 z 顺序的单元格中。
现在设置相对于单元格宽度的宽度动画。
你可以这样做,
UIView * testView = [[UIView alloc] initWithFrame:CGRectMake(320.0f, 100.0f, 320.0f, 200.0f)];
[testView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:testView]; //In your case cell's content view
[UIView animateWithDuration:0.7f
delay:0.0f
options:UIViewAnimationOptionTransitionNone
animations:^{
[testView setFrame:CGRectMake(0.0f, 100.0f, 320.0f, 200.0f)];
}
completion:nil];
您可以更改不同的动画选项。例如,如果您想要自动反转和重复,您可以使用 UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
作为选项。
希望这会有所帮助:)
- (IBAction)startProgress:(id)sender {
[self layoutIfNeeded];
self.progressViewWidth.constant = 300;
[UIView animateWithDuration:2 animations:^{
[self layoutIfNeeded];
}];
}
这是预期的输出,当我点击开始时:
我想从左到右为 UITableViewCell
的背景颜色设置动画(有点像进度条),但我很困惑。这可能吗?
编辑
接受了添加周期性扩展其宽度的背景视图的建议,但这不起作用。这是我现在拥有的代码:
// in viewDidLoad
self.tableView.reloadData()
self.beginWorkout()
...
// beginWorkout
private func beginWorkout() {
let indexPath = NSIndexPath(forRow: 1, inSection: 0)
let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! MovementCell
cell.animateProgressView()
}
...
// MovementCell animateProgressView
func animateProgressView() {
UIView.animateWithDuration(15, animations: {
self.progressViewWidth.constant = self.screenWidth
})
}
但动画不会随时间执行。 progressViewWidth
常量立即变为屏幕宽度。
如果您希望它的行为与进度条一样,请将 UIView(例如蓝色背景)放在具有最低 z 顺序的单元格中。
现在设置相对于单元格宽度的宽度动画。
你可以这样做,
UIView * testView = [[UIView alloc] initWithFrame:CGRectMake(320.0f, 100.0f, 320.0f, 200.0f)];
[testView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:testView]; //In your case cell's content view
[UIView animateWithDuration:0.7f
delay:0.0f
options:UIViewAnimationOptionTransitionNone
animations:^{
[testView setFrame:CGRectMake(0.0f, 100.0f, 320.0f, 200.0f)];
}
completion:nil];
您可以更改不同的动画选项。例如,如果您想要自动反转和重复,您可以使用 UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
作为选项。
希望这会有所帮助:)
- (IBAction)startProgress:(id)sender {
[self layoutIfNeeded];
self.progressViewWidth.constant = 300;
[UIView animateWithDuration:2 animations:^{
[self layoutIfNeeded];
}];
}
这是预期的输出,当我点击开始时: