【问题标题】:Do not rotate UIView but rotate UIImageView's inside it不要旋转 UIView,而是在其中旋转 UIImageView
【发布时间】:2012-10-11 09:29:41
【问题描述】:

我知道有很多关于它的帖子,但我找不到最佳解决方案。

我有一个“持有人”视图 (UIView),其中包含许多在横向模式下水平拉伸的滚动视图。每个滚动视图都包含视图的包含图像,这些图像是垂直滚动的。再一次,整个事情都在风景中。

我想要的是,当我旋转到纵向模式时,包含所有内容的“持有人”视图保持不变,意思是现在是一列,滚动视图旋转意味着滚动是水平的,但是滚动视图的内容(包含图像的视图)旋转。

我尝试编写一个 UIView 子类(用于“持有者”视图)并放置以下方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

以同样的方式我希望处理驻留在我的“持有者”视图中的子视图,但这不起作用。最好的方法是什么?谢谢。

【问题讨论】:

    标签: ios uiview rotation scroll


    【解决方案1】:

    您可以将支持的方向设置为您想要的方向,并观察UIDevice 方向更改以手动处理其他方向。这里有一个例子:

    #import "ViewController.h"
    
    @interface ViewController ()
    
    - (void)deviceDidRotate:(NSNotification *)notification;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
    
        [super viewDidLoad];
    
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(deviceDidRotate:)
                                                     name:UIDeviceOrientationDidChangeNotification
                                                   object:nil];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    
        return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    }
    
    #pragma mark - Private methods
    
    - (void)deviceDidRotate:(NSNotification *)notification {
    
        UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
        /* Handle manually the rotation
           For instance, apply a transform to a UIView:
           CGAffineTransform transform = CGAffineTransformMakeRotation(radians);
           self.aView.transform = transform; */
    }
    
    @end
    

    【讨论】:

    • 谢谢,CGAffineTransform 正是我想要的。我还有一个关于语法的问题。我将一些对象放在一个数组中并希望旋转这些对象,例如 for (loop thorugh array) { CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI / 2.0);[[myLetterObjects objectAtIndex:i]] transform = transform;} 语法如何在这里工作?点像 [myLetterObjects objectAtIndex:i].transform = transform;不会的。
    • @Au Ris 在循环内: UIView * aView = [myLetterObjects objectAtIndex:i]; [aView setTransform:transform];
    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多