2013-05-30 87 views
0

我有一個uiscrollview。 ScrollView包含多個uiview作爲子視圖。我希望​​像動畫效果一樣將頁面分頁。我已經設置scrollview.pagingEnabled = NO。UIScrollview與分頁像效果

以下是我的代碼

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { 
lastContentOffset = scrollView.contentOffset.x; 

} 


-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { 

    ScrollDirection scrollDirection; 
    if (self.lastContentOffset >scrollView.contentOffset.x) { 
     scrollDirection = ScrollDirectionRight; } else if (self.lastContentOffset < scrollView.contentOffset.x) { 
     scrollDirection = ScrollDirectionLeft; 
     } 

    if (scrollDirection==ScrollDirectionRight) { 
     CGFloat xOffset = scrollview.contentOffset.x; 
     CGFloat yOffset = scrollview.contentOffset.y; 

     if (scrollview.contentOffset.x != scrollview.frame.origin.x) 
     { 


      [scrollview setContentOffset:CGPointMake(xOffset- 320, yOffset) animated:YES];      


     } 

     NSLog(@" ScrollDirectionRight custom x==%f %f", scrollview.contentOffset.x, scrollview.contentSize.width); 


    } else if(scrollDirection==ScrollDirectionLeft) { 
     CGFloat xOffset = scrollview.contentOffset.x; 
     CGFloat yOffset = scrollview.contentOffset.y; 

     if ((scrollview.contentOffset.x != scrollview.frame.origin.x)) 
     { 
      [scrollview setContentOffset:CGPointMake(xOffset + 320, yOffset) animated:YES]; 

     } 
     NSLog(@"ScrollDirectionLeft custom x==%f %f", scrollview.contentOffset.x, scrollview.contentSize.width); 

      } 
    } 

通過這個代碼即時得到尋呼像effect.but效果並不順利。

感謝

+0

你想要什麼樣的動畫? – danypata

+0

每當我滾動下一個視圖應該在中心 – user2185354

回答

0

退房UIScrollViewDelegate方法

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset 

在回調中,您將修改「targetContentOffset」到你想看到滾動偏移動畫結束。

使用它你可以使自定義分頁實現更順暢。

+0

請你能詳細告訴我 – user2185354