简体   繁体   English

UITableView viewForHeaderInSection UIView动画问题

[英]UITableView viewForHeaderInSection UIView animation issue

I have a custom UIView that has two labels, one of which is animated (it blinks like a cursor and looks quite fly). 我有一个具有两个标签的自定义UIView,其中一个标签是动画的(它像光标一样闪烁并且看上去很飞)。 This view works fine EXCEPT when I try to use it for a tableView's header; 当我尝试将其用作tableView的标头时,此视图可以正常工作; the animation doesn't repeat even though I specify UIViewAnimationOptionRepeat . 即使指定UIViewAnimationOptionRepeat ,动画也不会重复。

My 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

Is there something about tableView headers and their presentation? 关于tableView标头及其表示形式有什么问题吗? Is there some other combination of UIViewAnimationOptions I should consider? 我应该考虑UIViewAnimationOptions其他组合吗? Instead of automatically starting the animation block should I trigger it off some event like the headerView's appearance or some such? 除了自动启动动画块外,我还应该将其触发一些事件,例如headerView的外观或类似事件吗?

Aha! 啊哈! Although [UIView areAnimationsEnabled] defaults to TRUE , it was for some reason FALSE . 尽管[UIView areAnimationsEnabled]默认设置为TRUE ,但由于某种原因它为FALSE I forced it with 我用

[UIView setAnimationsEnabled:YES];

before the animation block and all works now! 在动画块之前,现在一切正常! I don't know why it was off... 我不知道为什么会关闭...

Try like this using UIViewAnimationOptionAutoreverse option as well: 也尝试使用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];

Hope it helps! 希望能帮助到你!

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

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