【问题标题】:Fade In / Fade out 2 Labels infinite loop inside tableview淡入/淡出2个标签在tableview内无限循环
【发布时间】:2016-05-30 09:32:32
【问题描述】:

我试图在无限循环中显示一个带有两个不断变化的字符串(如横幅)的间歇性标签。 第一个标签应该淡入。之后它应该淡出并让第二个标签做同样的事情。然后无限或长时间重复该序列。 标签位于 UITableView 的子类中。

到目前为止我已经试过了:

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  ...
  // Inside a cell an at its own section of the tableview
  cell.label1.alpha = 1;

            [UIView beginAnimations:nil context:nil];

            [UIView setAnimationDuration:3];
            [UIView setAnimationCurve:UIViewAnimationCurveLinear];
            [UIView setAnimationRepeatCount:INFINITY];
            [cell.label1 setAlpha:0];
            [UIView setAnimationRepeatAutoreverses:YES];

            [UIView commitAnimations];

            cell.label2.alpha = 1;

            [UIView beginAnimations:nil context:nil];

            [UIView setAnimationDuration:3];
            [UIView setAnimationCurve:UIViewAnimationCurveLinear];
            [UIView setAnimationRepeatCount:INFINITY];
            [cell.label1 setAlpha:0];
            [UIView setAnimationRepeatAutoreverses:YES];

            [UIView commitAnimations];

...
}

此代码运行良好,但效果不明显,因为 label1 的淡入与 label2 的淡出交叉,这很奇怪。 那么,我怎样才能在两个动画之间打断,以便清楚地看到褪色的标签呢?

【问题讨论】:

标签: ios objective-c uitableview


【解决方案1】:

感谢@Paulw11 的回答,现在它工作正常,这是我的更改代码:

cell.label2.alpha = 0;
            cell.label1.alpha = 1;

            [UIView animateKeyframesWithDuration:15 delay:0.0 options:0 animations:^{
                [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.25 animations:^{
                    cell.label1.alpha = 0;
                    cell.label2.alpha = 0;
                    [self.view layoutIfNeeded];
                }];
                [UIView addKeyframeWithRelativeStartTime:0.25 relativeDuration:0.25 animations:^{
                    cell.label1.alpha = 0;
                    cell.label2.alpha = 1;
                    [self.view layoutIfNeeded];
                }];
                [UIView addKeyframeWithRelativeStartTime:0.50 relativeDuration:0.25 animations:^{
                    cell.label1.alpha = 0;
                    cell.label2.alpha = 0;
                    [self.view layoutIfNeeded];
                }];
                [UIView addKeyframeWithRelativeStartTime:0.75 relativeDuration:0.25 animations:^{
                    cell.label1.alpha = 1;
                    cell.label2.alpha = 0;
                    [self.view layoutIfNeeded];
                }];

                [UIView setAnimationRepeatCount: 200];


            } completion:nil];

【讨论】:

    猜你喜欢
    • 2016-10-19
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 2021-03-07
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多