2010-09-05 134 views
6

我想在旋轉更改期間手動更新UIScrollViewcontentOffset。滾動視圖填充屏幕,並具有靈活的寬度和靈活的高度。旋轉期間的UIScrollView的內容偏移

我目前正試圖在willRotateToInterfaceOrientation更新contentOffset,就像這樣:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    [Utils logPoint:myScrollView.contentOffset tag:@"initial"]; 
    myScrollView.contentOffset = CGPointMake(modifiedX, modifiedY); 
    [Utils logPoint:myScrollView.contentOffset tag:@"modified"]; 
} 

-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
    [Utils logPoint:myScrollView.contentOffset tag:@"final"]; 
} 

然而,最終的價值是不是修改後的值,它有點似乎是由它的影響,但它不是對我來說如何。

這些都是我得到的結果:

initial: (146.000000;-266.000000) 
modified: (81.000000;-108.000000) 
final: (59.000000;-0.000000) 

initial: (146.000000;-266.000000) 
modified: (500.000000;500.000000) 
final: (59.000000;500.000000) 

initial: (146.000000;-266.000000) 
modified: (-500.000000;-500.000000) 
final: (-0.000000;-0.000000) 

如何旋轉變化時更新滾動視圖的contentOffset?

+1

你嘗試改變contentOffset在'willAnimateRotationToInterfaceOrientation'呢?當時應該有一個動畫塊,或許結果是不同的。 – Pascal 2010-09-08 13:37:01

+0

@帕斯卡是的,它工作正常。不過,我想知道爲什麼它不按預期行事,如果我改變它在willRotateToInterfaceOrientation。 – hpique 2010-09-09 06:31:55

+0

我的猜測是父母的框架只會在**'willRotateToInterfaceOrientation'之後改變**,所以你設置了一個不同的contentOffset,但是那一個會立即改變,因爲超級視圖的框架也只是改變了。對於操作系統來說,就像你早些時候改變畫面的方式,而不是等待旋轉。 – Pascal 2010-09-12 10:34:15

回答

12

我的評論轉換爲一個答案:)

嘗試更改contentOffsetwillAnimateRotationToInterfaceOrientation:duration:代替。到那時應該有一個動畫塊,OS將contentOffset的更改視爲屬於由旋轉引起的更改。

如果您之前更改了contentOffset,它看起來像系統不會將這些更改視爲屬於旋轉,並且仍然應用旋轉調整大小,這次從新維度開始。

+0

工作就像一個魅力,添加代碼片段,以幫助別人。 – Shardul 2013-04-15 03:06:51

+1

此方法已棄用。嘗試使用'viewWillTransitionToSize'或'willTransitionToTraitCollection' – Gerald 2016-09-23 16:45:33

4

以下代碼片段在UIScrollView上啓用分頁並保持頁偏移時會執行此操作。

聲明這將計算旋轉之前當前頁的位置的屬性,並將其設置爲在viewDidLoad

@property (assign, nonatomic) NSInteger lastPageBeforeRotate; 

-1,那麼重寫willRotateToInterfaceOrientation:toInterfaceOrientation:duration方法和分配計算值給它。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    int pageWidth = self.scrollView.contentSize.width/self.images.count; 
    int scrolledX = self.scrollView.contentOffset.x; 
    self.lastPageBeforeRotate = 0; 

    if (pageWidth > 0) { 
     self.lastPageBeforeRotate = scrolledX/pageWidth; 
    } 

    [self showBackButton:NO]; 
} 

然後,我們確保在執行旋轉之前,我們確立了滾動型的內容正確偏移,以便將其集中到我們lastPageBeforeRotate

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
     if (self.lastPageBeforeRotate != -1) { 
      self.scrollView.contentOffset = CGPointMake(self.scrollView.bounds.size.width * self.lastPageBeforeRotate, 0); 
      self.lastPageBeforeRotate = -1; 
     } 
} 
+0

非常感謝!這是真正爲我工作的唯一解決方案:) – 2015-01-08 17:23:48

3

的iOS更新答案8+

實施viewWillTransitionToSize:withTransitionCoordinator:在您的控制器中。

例子:

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { 
    NSUInteger visiblePage = (NSInteger) self.scrollView.contentOffset.x/self.scrollView.bounds.size.width; 

    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { 
     self.scrollView.contentOffset = CGPointMake(visiblePage * self.scrollView.bounds.size.width, self.scrollView.contentOffset.y); 
    } completion:nil]; 
} 
+0

謝謝!有趣的是,scrollView.bounds在animateAlongsideTransition函數內使用新的/不同的值。 – 2016-01-28 20:42:53