我注意到,当我改变一个UILabel在动画块中的界限,它只有当我增加大小时才起作用,当我减小UILabel的大小只是改变了他的大小,但没有动画.
用简单的UIView替换UILabel按照预期的方式工作.
用简单的UIView替换UILabel按照预期的方式工作.
注意:将UILabel的contentMode属性更改为UIViewContentModeScaleToFill会修复此问题,但是我仍然不明白为什么在不更改contentMode属性的情况下增加大小的原因.
#import "FooView.h" @interface FooView () @property (strong,nonatomic) UILabel *label; @property (assign,nonatomic) BOOL resized; @end @implementation FooView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor lightGrayColor]; self.label = [[UILabel alloc] initWithFrame:(CGRect){0,frame.size}]; self.label.backgroundColor = [UIColor greenColor]; [self addSubview:self.label]; _resized = false; UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeSize)]; tapRecognizer.numberOfTapsrequired = 1; [self addGestureRecognizer:tapRecognizer]; } return self; } - (void)changeSize { [UIView animateWithDuration:0.8 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ CGRect bounds = self.label.bounds; if (self.resized) { bounds.size.height *= 2; bounds.size.width *= 2; } else { bounds.size.width /= 2; bounds.size.height /= 2; } self.label.bounds = bounds; } completion:^(BOOL finished) { self.resized = !self.resized; }]; } @end