I have a custom UIView that has two labels, one of which is animated (it blinks like a cursor and looks quite fly). This view works fine EXCEPT when I try to use it for a tableView's header; the animation doesn't repeat even though I specify 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? Is there some other combination of UIViewAnimationOptions
I should consider? Instead of automatically starting the animation block should I trigger it off some event like the headerView's appearance or some such?
Aha! Although [UIView areAnimationsEnabled]
defaults to TRUE
, it was for some reason 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:
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.