ios - UITableView viewForHeaderInSection UIView 动画问题

标签 ios uitableview uiviewanimation

我有一个自定义的 UIView,它有两个标签,其中一个是动画的(它像光标一样闪烁,看起来很飞)。这个 View 工作正常,除了当我尝试将它用于 tableView 的标题时;即使我指定了 UIViewAnimationOptionRepeat,动画也不会重复。

我的类(class):

@implementation HXTEST {

  UILabel *cursorLabel;
}

#pragma mark - inits

- (id) initForLabel:(UILabel *)forLabel {

  self = [super init];

  if (self) {

    self.translatesAutoresizingMaskIntoConstraints = NO;

    CGRect abba = [forLabel.text boundingRectWithSize:forLabel.frame.size
                                              options:NSStringDrawingUsesFontLeading
                                           attributes:@{ NSFontAttributeName : forLabel.font}
                                              context:[NSStringDrawingContext new]];

    float width = CGRectGetWidth(abba) * 1.005f;

    self->cursorLabel = [UILabel new];

    self->cursorLabel.textColor = forLabel.textColor;
    self->cursorLabel.font = forLabel.font;
    self->cursorLabel.text = @"▏";
    self->cursorLabel.adjustsFontSizeToFitWidth = forLabel.adjustsFontSizeToFitWidth;
    self->cursorLabel.minimumScaleFactor = forLabel.minimumScaleFactor;
    self->cursorLabel.translatesAutoresizingMaskIntoConstraints = NO;

    [self addSubview:forLabel];
    [self addSubview:self->cursorLabel];

    [self addConstraint:[NSLayoutConstraint constraintWithItem:forLabel
                                                          attribute:NSLayoutAttributeLeading
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self
                                                          attribute:NSLayoutAttributeLeft
                                                         multiplier:1.f
                                                           constant:0.f]];

    [self addConstraint:[NSLayoutConstraint constraintWithItem:forLabel
                                                     attribute:NSLayoutAttributeWidth
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:nil
                                                     attribute:NSLayoutAttributeNotAnAttribute
                                                    multiplier:1.0f
                                                      constant:width]];

    [self addConstraint:[NSLayoutConstraint constraintWithItem:forLabel
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self
                                                          attribute:NSLayoutAttributeTop
                                                         multiplier:1.f
                                                           constant:0.f]];

    [self addConstraint:[NSLayoutConstraint constraintWithItem:forLabel
                                                          attribute:NSLayoutAttributeBottom
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self
                                                          attribute:NSLayoutAttributeBottom
                                                         multiplier:1.f
                                                           constant:0.f]];

    [self addConstraint:[NSLayoutConstraint constraintWithItem:self->cursorLabel
                                                     attribute:NSLayoutAttributeLeading
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:forLabel
                                                     attribute:NSLayoutAttributeTrailing
                                                    multiplier:1.f
                                                      constant:0.f]];

    [self addConstraint:[NSLayoutConstraint constraintWithItem:self->cursorLabel
                                                     attribute:NSLayoutAttributeWidth
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:nil
                                                     attribute:NSLayoutAttributeNotAnAttribute
                                                    multiplier:1.f
                                                      constant:forLabel.font.pointSize]];

    [self addConstraint:[NSLayoutConstraint constraintWithItem:self->cursorLabel
                                                     attribute:NSLayoutAttributeTop
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self
                                                     attribute:NSLayoutAttributeTop
                                                    multiplier:1.f
                                                      constant:0.f]];

    [self addConstraint:[NSLayoutConstraint constraintWithItem:self->cursorLabel
                                                     attribute:NSLayoutAttributeBottom
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self
                                                     attribute:NSLayoutAttributeBottom
                                                    multiplier:1.f
                                                      constant:0.f]];
  }

  [UIView animateWithDuration:1.f
                        delay:0.f
                      options:UIViewAnimationOptionRepeat
                   animations:^{

                     self->cursorLabel.alpha = 1.f;
                     self->cursorLabel.alpha = 0.f;
                   }

                   completion:nil];

  return self;
}

@end

是否有关于 tableView 标题及其表示的内容?我应该考虑 UIViewAnimationOptions 的一些其他组合吗?我不应该自动启动动画 block ,而是应该在某些事件(例如 headerView 的外观或类似事件)中触发它?

最佳答案

啊哈!虽然 [UIView areAnimationsEnabled] 默认为 TRUE,但由于某些原因 FALSE。我强制它

[UIView setAnimationsEnabled:YES];

在动画 block 之前,现在一切正常!我不知道为什么它关闭了...

关于ios - UITableView viewForHeaderInSection UIView 动画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22024669/

相关文章:

ios - 如何以编程方式让 UIWebView 暂停/播放视频内容

ios - 当我尝试与另一个NSString比较时,NSString仅显示内存地址

ios - 仅在 UITableView 的最后 2 部分添加渐变背景

iphone - 禁用添加到 UITableView 的 UISearchbar 的滚动

iOS:改变方向时跳跃动画,setFrame

iOS 应用程序在 [UIView animateKeyframesWithDuration :] 期间崩溃

ios - NSDate 问题

iphone - 让 Facebook 好友参加事件?

ios - swift 2.2 : Class 'XX' has no initializers

objective-c - UIView animateKeyframesWithDuration 与标准动画 block 动画不同