2012-05-04 52 views
1

我已經創建了小型iPhone應用程序。我把UITableView和放在單元格內的一些UIView。我添加了將單元格中的視圖水平移動的功能。現在我想添加邏輯來垂直移動整個UITableView,如果我在我的UIView中垂直移動我的手指。這是我的代碼:如何垂直移動UITableView

-(void)moveView:(UIPanGestureRecognizer *)recognizer 
{ 

    if([recognizer state] == UIGestureRecognizerStateBegan) 
    { 
     CGPoint location = [recognizer locationInView:[self superview]]; 
     prevPos = location; 
     startPosition = self.frame.origin.x; 

    } 
    else if([recognizer state] == UIGestureRecognizerStateChanged) 
    { 
     CGPoint location = [recognizer locationInView:[self superview]]; 
     float delX = location.x - prevPos.x; 
     float delY = location.y - prevPos.y; 
     if(delX > 0 && delX * delX > delY * delY) 
     { 
      CGPoint np = CGPointMake(self.frame.origin.x+delX, self.frame.origin.y); 
      CGRect fr = self.frame; 
      fr.origin = np; 
      self.frame = fr; 
      prevPos = location; 
     } 
     else if(delX * delX < delY * delY) 
     { 

     } 
    } 
    else if([recognizer state] == UIGestureRecognizerStateEnded || 
      [recognizer state] == UIGestureRecognizerStateFailed || 
      [recognizer state] == UIGestureRecognizerStateCancelled) 
    { 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.5]; 

     CGRect rect = self.frame; 
     rect.origin.x = startPosition; 
     self.frame = rect; 
     startPosition = 0; 
     [UIView commitAnimations]; 
    } 
} 

我應該在這種情況下輸入什麼代碼:else if(delX * delX < delY * delY)

謝謝!

回答

1

如果要滾動TableView,則需要更改TableView的contentOffset屬性。

看看setContentOffset:animated:文檔

+0

感謝您的回答!它可以工作,但不能作爲tableView的標準行爲。是否有可能將控制權交給UITableView? – Radislav

+0

爲什麼你想自己滾動tableView?是不是tableView滾動本身?如果不是爲什麼? – Lefteris

+0

我不知道怎麼用英文解釋。如果我像往常一樣滾動表視圖 - 它的工作原理。如果我正在用手指嘗試scrool table view,但是用手指放在table cell中的UIView上。此視圖具有手勢識別器,可防止標準表視圖滾動。如果手指垂直移動,我需要將控制權交給表視圖。 – Radislav