6

我有一个 UIView,其高度和宽度在旋转到 90 度时会互换。现在,当我尝试增加高度或宽度时,我会得到不正常的外观。如何更改旋转 UIView 的高度?

4

2 回答 2

9

Apple 的文档指出,当视图不是身份转换frame时,视图的属性变得未定义。transform

旋转视图会改变视图的变换。

现在,为什么这会使框架无效?Apple 的文档实际上有点不精确: frame 属性对于转换后的视图并没有变得完全没有意义。相反,它现在将反映视图的边界矩形,即转换后的视图可以适应的最小直立矩形。

这是因为frame实际上是派生属性。如果你想改变变换后视图的“真实高度”,即在其自身坐标系中的高度(在应用变换之前),有一个属性:bounds.

所以,简而言之:

CGRect bounds = myView.bounds;
bounds.size.height = newHeight;
myView.bounds = bounds;
于 2013-05-27T12:18:54.370 回答
1

使用以下方法...

- (void) runSpinAnimationOnView:(UIView*)view duration:(CGFloat)duration rotations:(CGFloat)rotations repeat:(float)repeat;
{
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ];
    rotationAnimation.duration = duration;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = repeat;

    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
于 2013-05-27T12:47:02.790 回答