2011-12-22 28 views
-1

當我按前一個按鈕時,它不會產生任何效果。下一個按鈕工作正常,但上一個按鈕不顯示以前的圖像。我使用scroll view加載圖片:使用按鈕單擊在滾動視圖中移動前一圖像

- (IBAction)next{ 
    int arraycount = [appDelegate.articles count]; 
    for (int nextIndex = [pageControl currentPage]; nextIndex < arraycount; nextIndex++) { 
     if (self.scrollView.contentOffset.x <= self.scrollView.frame.size.width) { 
      CGRect frame; 
      frame.origin.x = self.scrollView.contentOffset.x +  self.scrollView.frame.size.width; 
      frame.origin.y = 0; 
      frame.size = self.scrollView.frame.size; 
      [self.scrollView scrollRectToVisible:frame animated:YES]; 
      pageControlBeingUsed = YES; 
     } 
    } 
} 

-(IBAction)previousButtonAction 
{ 
    int arraycount = [appDelegate.articles count]; 
    NSLog(@" arraycount : %d", arraycount); 
    NSLog(@" [pageControl currentPage] : %d", [pageControl currentPage]); 
    for (int nextIndex = [pageControl currentPage]; nextIndex>arraycount; nextIndex--) { 
     if (self.scrollView.contentOffset.x >= self.scrollView.frame.size.width) { 
      CGRect frame; 
      frame.origin.x = self.scrollView.contentOffset.x + self.scrollView.frame.size.width; 
      frame.origin.y = 0; 
      frame.size = self.scrollView.frame.size; 
      [self.scrollView scrollRectToVisible:frame animated:YES]; 
      pageControlBeingUsed = YES; 
     } 
    } 
} 

回答

4

好了,現在我想我會不同的方法處理這個。我會在重新分配前檢查當前的偏移量。

未經測試的代碼:

- (IBAction)next{ 
    if (self.scrollView.contentOffset.x <= self.scrollView.frame.size.width) { 
     CGRect frame; 
     frame.origin.x = self.scrollView.contentOffset.x + self.scrollView.frame.size.width; 
     frame.origin.y = 0; 
     frame.size = self.scrollView.frame.size; 
     [self.scrollView scrollRectToVisible:frame animated:YES]; 
     pageControlBeingUsed = YES; 
    } 
} 
+0

的做的問題!上面編輯的答案。 – user1107725

+0

DOH提到從循環下一個索引值,其中提到 – ader

+0

它的工作完美謝謝,如果我想爲以前然後可能只使用遞減標誌 – user1107725

0

我會當用戶到達第三圖像禁用「下一步」按鈕。如果你喜歡返回到第一圖像時,用戶按下第三圖像中的「下一步」,則必須在後面加一個額外否則,如果指示的話:

- (IBAction)next{ 
int arraycount = [appDelegate.articles count]; 
for (int nextIndex = [pageControl currentPage]; nextIndex < arraycount; nextIndex++) {  
    if (nextIndex < arraycount) { 
     CGRect frame; 
     frame.origin.x = self.scrollView.frame.size.width * nextIndex; 
     frame.origin.y = 0; 
     frame.size = self.scrollView.frame.size; 
     [self.scrollView scrollRectToVisible:frame animated:YES]; 
      pageControlBeingUsed = YES; 
     } 
     // If the user clicks on next in the last item, return to the first 
     else { 
     CGRect frame; 
     frame.origin.x = 0; 
     frame.origin.y = 0; 
     frame.size = self.scrollView.frame.size; 
     [self.scrollView scrollRectToVisible:frame animated:YES]; 
      pageControlBeingUsed = YES; 
     } 
    } 

}

+0

在else聲明我禁用按鈕,但它又回到第二個圖像 – user1107725

0

arraycount = 3

[的PageControl當前頁]是零索引,所以當你在第3頁上等於2

所以你的for循環nextIndex等於2,小於arrayCount所以它進入您的循環開始。

將其更改爲:

for (int nextIndex = [pageControl currentPage]; nextIndex < (arraycount-1); nextIndex++) { 

好請問這個輸出:

- (IBAction)next{ 
    int arraycount = [appDelegate.articles count]; 
NSLog(@" arraycount : %d", arraycount); 
NSLog(@" [pageControl currentPage] : %d", [pageControl currentPage]); 
    for (int nextIndex = [pageControl currentPage]; nextIndex < (arraycount-1); nextIndex++) { 
NSLog(@" nextIndex : %d", nextIndex); 
     CGRect frame; 
     frame.origin.x = self.scrollView.frame.size.width * nextIndex; 
     frame.origin.y = 0; 
     frame.size = self.scrollView.frame.size; 
     [self.scrollView scrollRectToVisible:frame animated:YES]; 
      pageControlBeingUsed = YES; 

     } 
} 
+0

它不工作,因爲你說的問題是,在我的代碼後,達到3圖像再次去秒,當點擊ob按鈕 – user1107725

+0

設置斷點和通過代碼來了解它爲什麼進入for循環。或者像我上面的代碼一樣嘗試nslogs。 – ader

+0

它給arraycount 3和nextIndex值1只有 – user1107725

0

佛我它迅速3.

func prevAction (sender:UIButton) 
{ 
    print("self.scView.contentOffset.x ..\(self.scView.contentOffset.x)") 
    if (self.scView.contentOffset.x <= self.scView.contentSize.width) 
    { 
     var frame = CGRect() 

     frame.origin.x = self.scView.contentOffset.x - 80; 
     frame.origin.y = 0; 
     frame.size = self.scView.frame.size; 

     self.scView .scrollRectToVisible(frame, animated: true) 

    } 


} 

感謝正努力@ader。