【问题标题】:Rotate UIView in Cocoa Touch在 Cocoa Touch 中旋转 UIView
【发布时间】:2009-08-29 02:13:22
【问题描述】:

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

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

但是,这不适用于 UIView(在我的测试中)。为了让这个/其他代码能够正常工作,我必须做一些事情吗?还是有更好的方法来做同样的事情?

感谢您的帮助!

【问题讨论】:

    标签: iphone cocoa-touch uiview rotation


    【解决方案1】:

    这对我有用

     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;
    

    【讨论】:

    • 使用 M_PI 代替 3.14159
    • 并使用 M_PI_2 而不是 M_PI/2
    【解决方案2】:

    我成功了:

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

    【讨论】:

    • 另外,这个好像我已经导入了QuartzCore框架也无法构建。
    • 角度是视图将被旋转的辐射角度。它不会建立可能是因为其他错误。构建日志说什么?
    • 它说“CATransform3D”未声明(在此函数中首次使用)和“rotationTransform”未声明(在此函数中首次使用)
    • 我知道这已经很老了,但我认为 PF1 可能对“导入” QuartzCore 框架和在文件顶部有实际的 #import <QuartzCore/QuartzCore.h> 声明之间的区别感到困惑。
    【解决方案3】:

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

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

    我还没有测试它在 3.1.2 上仍然有效。虽然我知道它适用于 3.0。

    【讨论】:

      猜你喜欢
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多