ios - 不要旋转 UIView,而是旋转其中的 UIImageView

标签 ios uiview rotation scroll

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

我有一个“holder” View (UIView),其中包含许多在横向模式下水平拉伸(stretch)的 ScrollView 。每个 ScrollView 都包含垂直滚动的包含图像的 View 。同样,整件事都在风景中。

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

我尝试编写一个 UIView 子类(用于“holder” View )并放置以下方法:

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

我希望以同样的方式处理驻留在我的“holder” View 中的 subview ,但这不起作用。最好的方法是什么?谢谢。

最佳答案

您可以将支持的方向设置为您想要的方向,然后观察 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

关于ios - 不要旋转 UIView,而是旋转其中的 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12836435/

相关文章:

ios - 新 View 中的 Swift 动画立即发生

ios - 旋转后如何获取superview的frame?

ios - Scenekit - 基于世界轴的旋转子子节点的 convertTransform

ios - 将数据从类传递到 WatchOS 2(连接性)

ios - MapKit Tile Overlay 在 iOS8 中损坏

iphone - NSNumberFormatter-Decimal Style-格式化无限字符

ios - 如何为所有 subview 设置文本颜色?

ios - 将 UIView 添加回 View

apache-flex - 如何使用 matrix3D 在 AS3 中围绕中心进行 3D 旋转?

android - 安卓方向传感器的奇怪行为