我注意到,当我在动画块中更改 UILabel 的边界时,它只有在我增加大小时才有效,当我减小大小时,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, 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