UITableView viewForHeaderInSection UIView动画问题

时间:2014-02-25 19:51:04

标签: ios uitableview uiviewanimation

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

我的课程:

@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的其他组合吗?我应该触发一些事件,比如headerView的外观或某些事件,而不是自动启动动画块吗?

2 个答案:

答案 0 :(得分:1)

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

[UIView setAnimationsEnabled:YES];
动画块之前的

现在全部有效!我不知道为什么会这样......

答案 1 :(得分:0)

使用UIViewAnimationOptionAutoreverse选项也可以这样做:

void (^animationLabel) (void) = ^{
    blinkLabel.alpha = 0;
};
void (^completionLabel) (BOOL) = ^(BOOL f) {
    blinkLabel.alpha = 1;
};

NSUInteger opts =  UIViewAnimationOptionAutoreverse | 
                   UIViewAnimationOptionRepeat;
[UIView animateWithDuration:1.f delay:0 options:opts
                 animations:animationLabel completion:completionLabel];

希望它有所帮助!