【问题标题】:UIScrollView RotationUIScrollView 旋转
【发布时间】:2010-10-06 11:40:26
【问题描述】:

我有一个滚动视图,它会自动生成一系列包含图像视图的子视图。这个概念是它几乎是一个自定义的 PDF 样式阅读器,其中每个页面都作为图像加载到 imageview 中。共有三个“层”——外部 UIScrollView、自动生成的 subViews 和 subview 内部的 imageview。

我的困境是,当这些子视图生成时,它们并没有真正的硬引用,所以在我的 didRotateFromInterfaceOrientation 中没有办法说调整这个特定视图的大小。

我创建了一个名为 setUpView 的函数,它初始化所有视图和图像:

- (void)setUpView {
    //INITIALISE PAGING SCROLL VIEW
    CGRect pagingScrollViewFrame = [[UIScreen mainScreen] bounds];
    pagingScrollViewFrame.origin.x -= 10;
    pagingScrollViewFrame.size.width += 20;
    pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];

    //CONFIGURE PAGING SCROLL VIEW  
    pagingScrollView.pagingEnabled = YES;
    pagingScrollView.backgroundColor = [UIColor blackColor];
    pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width*7, pagingScrollViewFrame.size.height);

    //ACTIVATE PAGING SCROLL VIEW
    self.view = pagingScrollView;

    //ADD PAGES TO SCROLL VIEW
    for (int i = 0; i < 7; i++){
        ImageScrollView *page = [[[ImageScrollView alloc] init] autorelease];
        [self configurePage:page forIndex:i];
        [pagingScrollView addSubview:page];
    }
}

在我的 loadView 方法中,我只是调用了这个函数。

有没有办法检测设备的当前方向,以便以某种方式输入到上述函数中?目前,在旋转时,outerview 可以正常旋转,但内部 sub-views 和 imageviews 没有。

【问题讨论】:

    标签: iphone objective-c uiscrollview imageview autorotate


    【解决方案1】:

    您可以为 UIDeviceOrientationDidChangeNotification 通知创建没有参考观察者的对象,该通知将在每次方向更改时发布。

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
    

    然后,在您的orientationChanged: 方法中,您可以检查当前方向并相应地布局您的视图。

    但是,您需要通过调用告诉设备生成此类通知

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    

    请务必移除在 dealloc 上观察方向变化的对象!

    【讨论】:

      【解决方案2】:

      您可以通过查看获得设备方向:

      [UIDevice currentDevice].orientation
      

      或者你可以使用 self.interfaceOrientation 作为 viewController。

      【讨论】:

      • 所以我可以有一个 if 语句,它说类似 if(self.interfaceOrientation = Landscapeleft){ //changedimensions };
      • 是的,你可以使用 layoutSubviews 函数根据当前方向更新你的子视图(当框架改变时它会被调用[当你旋转框架通常改变时:)]
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多