繁体   English   中英

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 * 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 UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse作为选项。

希望这会有所帮助:)

- (IBAction)startProgress:(id)sender {
[self layoutIfNeeded];
self.progressViewWidth.constant = 300;
[UIView animateWithDuration:2 animations:^{
    [self layoutIfNeeded];
}];
}

这是预期的输出,当我单击go时:

在此处输入图片说明

如果您希望它的行为像进度条一样,则将UIView(例如蓝色背景)放在Z顺序最低的单元格中。

现在设置其相对于单元格宽度的宽度的动画。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM