gpt4 book ai didi

ios - CGAffineTransformMakeRotation 移动 UILabel

转载 作者:行者123 更新时间:2023-12-01 18:23:11 26 4
gpt4 key购买 nike

我正在使用最新的 SDK 开发 iOS 5.0+ 应用程序。

我有一个自定义 UIView (带有自定义 xib)和四个 UILabel ,我想用这段代码将它们旋转 90 度:

- (void)layoutSubviews
{
[self rotateToLandscape];
}

- (void)rotateToLandscape
{
NSLog(@"Label 1: %@", NSStringFromCGRect(self.label1.frame));
NSLog(@"Label 2: %@", NSStringFromCGRect(self.label2.frame));
NSLog(@"Label 3: %@", NSStringFromCGRect(self.label3.frame));
NSLog(@"Label 4: %@", NSStringFromCGRect(self.label4.frame));

self.label1.transform = CGAffineTransformMakeRotation(M_PI/2);
NSLog(@"Label 1: %@", NSStringFromCGRect(self.label1.frame));
self.label2.transform = CGAffineTransformMakeRotation(M_PI/2);
NSLog(@"Label 2: %@", NSStringFromCGRect(self.label2.frame));
self.label3.transform = CGAffineTransformMakeRotation(M_PI/2);
NSLog(@"Label 3: %@", NSStringFromCGRect(self.label3.frame));
self.label4.transform = CGAffineTransformMakeRotation(M_PI/2);
NSLog(@"Label 4: %@", NSStringFromCGRect(self.label4.frame));
}

但是,我不知道为什么,都占据着相同的位置。

这是我得到的日志:
Label 1: {{15, 0}, {71, 21}}
Label 2: {{15, 29}, {71, 21}}
Label 3: {{15, 58}, {71, 21}}
Label 4: {{15, 78}, {71, 21}}
Label 1: {{40, -25}, {21, 71}}
Label 2: {{40, 4}, {21, 71}}
Label 3: {{40, 33}, {21, 71}}
Label 4: {{40, 53}, {21, 71}}

我读到我必须旋转 UILabel从它的中心(或从它的左上角),但我不知道该怎么做。

这就是我得到的:

enter image description here

而且,如果我显示要记录的中心:
- (void)rotateToLandscape
{
NSLog(@"Label 1: %@", NSStringFromCGPoint(self.label1.center));
NSLog(@"Label 2: %@", NSStringFromCGPoint(self.label2.center));
NSLog(@"Label 3: %@", NSStringFromCGPoint(self.label3.center));
NSLog(@"Label 4: %@", NSStringFromCGPoint(self.label4.center));

self.label1.transform = CGAffineTransformMakeRotation(M_PI/2);
NSLog(@"Label 1: %@", NSStringFromCGPoint(self.label1.center));
self.label2.transform = CGAffineTransformMakeRotation(M_PI/2);
NSLog(@"Label 2: %@", NSStringFromCGPoint(self.label2.center));
self.label3.transform = CGAffineTransformMakeRotation(M_PI/2);
NSLog(@"Label 3: %@", NSStringFromCGPoint(self.label3.center));
self.label4.transform = CGAffineTransformMakeRotation(M_PI/2);
NSLog(@"Label 4: %@", NSStringFromCGPoint(self.label4.center));
}

我得到这个日志:
Label 1: {50.5, 10.5}
Label 2: {50.5, 39.5}
Label 3: {50.5, 68.5}
Label 4: {50.5, 88.5}
Label 1: {50.5, 10.5}
Label 2: {50.5, 39.5}
Label 3: {50.5, 68.5}
Label 4: {50.5, 88.5}

无旋转:

enter image description here

这些标签位于 UIView 上。我想我必须旋转那个父 View ,而不是旋转每个 UILabel .

有什么建议吗?我必须移动每个UILabel旋转后?

最佳答案

您应该尝试查看 frame 的文档。属性(property)。

Warning: If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.



这意味着您在应用旋转变换后打印的帧值可以是任何值。不要依赖那个值。你可以看看 center属性,如果你想知道标签的位置。

默认情况下,所有 View 都围绕中心旋转。您可以使用 the anchor point 更改它.

关于ios - CGAffineTransformMakeRotation 移动 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15878917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com