2010-10-06 86 views
1

我有一個滾動視圖,自動生成一系列包含圖像瀏覽的子視圖。其概念是,它幾乎是一個自定義PDF風格的閱讀器,其中每個頁面都作爲圖像加載到imageview中。有三個「圖層」 - 外部UIScrollView,子視圖內自動生成的子視圖和圖像視圖。UIScrollView旋轉

我的兩難困境是,當生成這些子視圖時,他們並沒有真正的參考,所以在我的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方法我只是調用這個函數。

是否有任何方法來檢測設備的當前方向,以便可以以某種方式被饋送到上述功能中?目前,在旋轉時,外部視圖可以很好地旋轉,但內部子視圖和圖像視圖不會。

回答

1

您可以通過查看得到設備的方向:

[UIDevice currentDevice].orientation 

或者你可以使用self.interfaceOrientation進行的viewController。

+0

,所以我可以有一個聲明,如果說像如果(self.interfaceOrientation = landscapeleft){// changedimensions}; – 2010-10-06 16:20:11

+0

是的,你可以使用layoutSubviews函數根據當前的方向更新你的子視圖(它會在幀更改時調用[當你旋轉幀通常改變:)] – 2010-10-06 20:45:50

2

您可以在沒有參考觀察者的情況下爲UIDeviceOrientationDidChangeNotification通知製作對象,該通知將在每個方向更改中發佈。

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

然後,在您的orientationChanged:方法中,您可以檢查當前的方向並相應地佈置您的視圖。

但是,你需要通過調用

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

一定要刪除正在觀察在dealloc的方向改變的對象,告訴該設備產生這樣的通知!

希望這是有幫助的, 帕維爾