在 Cocoa Touch 中旋转 UIView

发布于 2024-08-03 20:38:51 字数 424 浏览 3 评论 0原文

我见过其他人也提出过这个问题,但大多数回答都不适用于最新的 3.0 版 iPhone 操作系统。不管怎样,我想知道如何在没有来自加速度计的任何输入的情况下以编程方式旋转 UIView。到目前为止我找到的代码:

CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159/2);
view.transform = transform;
CGRect contentRect = CGRectMake(-80, 80, 480, 320);
view.bounds = contentRect;

但是,这不适用于 UIView (在我的测试中)。我是否必须对 AppDelegate 执行某些操作才能使此/其他代码正常运行,或者是否有更好的方法来执行相同的操作?

感谢您的帮助!

I have seen other people who have had this question, but most of the responses aren't working on the latest 3.0 build of iPhone OS. Anyway, I'm wondering how I can programatically rotate a UIView without any input from the accelerometer. The code I have found so far:

CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159/2);
view.transform = transform;
CGRect contentRect = CGRectMake(-80, 80, 480, 320);
view.bounds = contentRect;

However, this doesn't work for UIView (in my testing). Is there something I have to do my AppDelegate in order for this/other code to function, or is there a better way of doing the same thing?

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风流物 2024-08-10 20:38:51

这对我有用

 CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_2);
 self.view.transform = transform;

 // Repositions and resizes the view.
 CGRect contentRect = CGRectMake(0,0, 480, 320);
 self.view.bounds = contentRect;

this works for me

 CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_2);
 self.view.transform = transform;

 // Repositions and resizes the view.
 CGRect contentRect = CGRectMake(0,0, 480, 320);
 self.view.bounds = contentRect;
杀お生予夺 2024-08-10 20:38:51

我在这方面取得了成功:

CATransform3D rotationTransform = CATransform3DIdentity;
[view.layer removeAllAnimations];
rotationTransform = CATransform3DRotate(rotationTransform, angle, 0.0, 0.0, 1);
view.layer.transform = rotationTransform;

I had success with that:

CATransform3D rotationTransform = CATransform3DIdentity;
[view.layer removeAllAnimations];
rotationTransform = CATransform3DRotate(rotationTransform, angle, 0.0, 0.0, 1);
view.layer.transform = rotationTransform;
天邊彩虹 2024-08-10 20:38:51

不确定这是否被认为是私有API,所以你可能不想使用它,但如果你想在不倾斜设备的情况下强制改变方向,你可以尝试调用:

[[UIDevice currentDevice] performSelector:@selector(setOrientation:)
                      withObject:(id)UIInterfaceOrientationLandscapeLeft];

我没有测试它仍然可以在3.1.2上工作。虽然我知道它可以在 3.0 上运行。

Not sure if this is considered private API, so you may not want to use it, but if you want to force the orientation to change without tilting the device, you can try calling:

[[UIDevice currentDevice] performSelector:@selector(setOrientation:)
                      withObject:(id)UIInterfaceOrientationLandscapeLeft];

I have not tested that it still works on 3.1.2. though I know it works on 3.0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文