2016-04-21 143 views
0

我有一個觀點 的子視圖我希望用戶是阿貝爾滾動這一觀點的權利和只剩下。 但向上或向下滾動我想這個觀點留在它的地方,我不希望它動的時候。 我該怎麼做?滾動子視圖左右,但沒有向上或向下

我使用Objective C的適用於iOS的iPhone應用程序編碼。

感謝

回答

1

您可以使用UIScrollView並設置contentSize屬性,使其height相同視圖的height

+0

你不明白我的意思,我想有一個可以滾動的小UI視圖但只限於左側和右側。如果用戶向下滾動,我想保留在頂欄之下,我不希望它從它的位置移動 –

1
  1. 創建panRecognizer

    UIPanGestureRecognizer *panRecognizer; 
    panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self 
                    action:@selector(wasDragged:)]; 
    [[self subview] addGestureRecognizer:panRecognizer]; 
    

2.創建wasDragged方法

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

    CGPoint translation = [recognizer translationInView:self.view]; 
    CGRect recognizerFrame = recognizer.view.frame; 
    recognizerFrame.origin.x += translation.x; 


    // Check if UIImageView is completely inside its superView 
    if (CGRectContainsRect(self.view.bounds, recognizerFrame)) { 
     recognizer.view.frame = recognizerFrame; 
    } 
    // Else check if UIImageView is vertically and/or horizontally outside of its 
    // superView. If yes, then set UImageView's frame accordingly. 
    // This is required so that when user pans rapidly then it provides smooth translation. 
    else { 
     // Check vertically 
     if (recognizerFrame.origin.y < self.view.bounds.origin.y) { 
      recognizerFrame.origin.y = 0; 
     } 
     else if (recognizerFrame.origin.y + recognizerFrame.size.height > self.view.bounds.size.height) { 
      recognizerFrame.origin.y = self.view.bounds.size.height - recognizerFrame.size.height; 
     } 

     // Check horizantally 
     if (recognizerFrame.origin.x < self.view.bounds.origin.x) { 
      recognizerFrame.origin.x = 0; 
     } 
     else if (recognizerFrame.origin.x + recognizerFrame.size.width > self.view.bounds.size.width) { 
      recognizerFrame.origin.x = self.view.bounds.size.width - recognizerFrame.size.width; 
     } 
    } 

    // Reset translation so that on next pan recognition 
    // we get correct translation value 
    [recognizer setTranslation:CGPointZero inView:self.view]; 
} 
相關問題