我注意到,当我改变一个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
解决方法
这是因为UILabel将其层的contentGravity设置为正在呈现的文本方向,这恰好默认为UIViewContentModeLeft(或@“left”).因此,当这个层被告知要动画时,它首先看一眼它的内容重力,然后基于随后的动画.因为它看到@“left”,应该有@“resize”,它假定缩放动画应该从左边开始,但它也必须尊重你给它的约束(边界改变),所以你的标签似乎跳入其最终尺寸,然后将其定位在屏幕中央.
如果要离开contentMode,请使用CATransform3D并缩放标签层,而不是边界更改.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。