pek*_*eko 21 objective-c uiviewanimation ios
我注意到当我在动画块中更改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
Run Code Online (Sandbox Code Playgroud)
Cod*_*aFi 24
这是因为UILabel将其图层设置contentsGravity为正在渲染文本的方向,这恰好是默认为UIViewContentModeLeft(或@"left").因此,当告诉该层动画时,它首先看一眼其内容的重力并将随后的动画置于其上.因为它看到了@"left"应该存在的位置@"resize",所以它假设缩放动画应该从左边开始,但它也必须遵守你给它的约束(边界变化),所以你的标签似乎跳到它的最终大小然后安顿在屏幕中央的位置.
如果你想contentMode单独留下,请使用CATransform3D's'并按比例缩放标签的图层而不是边界更改.
| 归档时间: |
|
| 查看次数: |
2418 次 |
| 最近记录: |