基本上我想将UILabel移动20px并将其大小调整为其原始大小的55%。
=> x保持不变,y,高度,宽度减小。
我想出的是:
//_line a UILabel*
CGFloat scaleFactor = 0.55f;
[UIView animateWithDuration:1.0 animations:^{
CGAffineTransform transform = CGAffineTransformMakeTranslation(0, -20);
transform = CGAffineTransformScale(transform, scaleFactor, scaleFactor);
_line.transform = transform;
}];
不幸的是,我没有得到预期的结果。它(略微)向上移动但也向右移动。
我错过了什么?
答案 0 :(得分:2)
问题是你要覆盖变换。您最初将transform
设置为转换转换,然后在发生任何事情之前将其重置为比例转换。所以它所做的就是规模行动。
相反,您需要合并两个矩阵并将它们的组合存储到transform
中。试试这个:
CGFloat scaleFactor = 0.55f;
[UIView animateWithDuration:1.0 animations:^{
CGAffineTransform translationTransform = CGAffineTransformMakeTranslation(0, -20);
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
CGAffineTransform totalTransform = CGAffineTransformConcat(translationTransform, scaleTransform);
_line.transform = totalTransform;
}];
答案 1 :(得分:2)
问题是您正在使用自动布局。自动布局和视图转换是彼此的敌人。您执行转换,但随后自动布局启动并将标签的框架基本上更改为原来的状态。
答案 2 :(得分:1)
这使我做了工作:第二次翻译
CGFloat scaleFactor = 0.55f;
CGFloat x_delta = _line.frame.size.width * (1-scaleFactor);
CGFloat y_delta = _line.frame.size.height * (1-scaleFactor);
[UIView animateWithDuration:1.0 animations:^{
CGAffineTransform transform = CGAffineTransformMakeTranslation(0, -20);
transform = CGAffineTransformScale(transform, scaleFactor, scaleFactor);
transform = CGAffineTransformTranslate(transform, -x_delta, -y_delta);
_line.transform = transform;
}];