2015-08-25 89 views

回答

1
  1. 添加一個UIView
  2. 子類的UIView與自定義類

在您自定義的UIView,你需要幾個變量和幾個覆蓋。確保用戶交互在你的故事板的UIView的啓用,然後在您的自定義類加載:

var startPosition: CGPoint? 
var originalHeight: CGFloat? 

之後,添加以下內容:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    let touch = touches.first 
    startPosition = touch?.locationInView(self) 
    originalHeight = self.frame.height 
} 

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    let touch = touches.first 
    let endPosition = touch?.locationInView(self) 
    let difference = endPosition!.y - startPosition!.y 
    let newFrame = CGRectMake(self.frame.origin.x, self.frame.origin.y + difference, self.frame.width, self.frame.height - difference) 
    self.frame = newFrame 
} 

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 

} 
0

在UIScrollView.h:

// When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its `scrollsToTop` property is YES, its delegate does not return NO from `shouldScrollViewScrollToTop`, and it is not already at the top. 
// On iPhone, we execute this gesture only if there's one on-screen scroll view with `scrollsToTop` == YES. If more than one is found, none will be scrolled. 
var scrollsToTop: Bool // default is YES. 

初始化滾動視圖的委託(除非你已經有了,可能在你的viewDidLoad中),然後將其設置爲true。

像這樣:

myScrollView.scrollsToTop = true 

如果你想確保你的滾動視圖委託允許這一點,從UIScrollViewDelegate的協議檢查這個方法:

optional func scrollViewShouldScrollToTop(scrollView: UIScrollView) -> Bool // return a yes if you want to scroll to the top. if not defined, assumes YES 

請評論的任何問題。

+0

那是偉大的,但它的少做點擊狀態欄將其向上滾動,以及如何疊加兩個視圖(具有圖形和下方的視圖),然後滾動時允許表視圖向上移動並重疊圖形視圖 –

+0

當滾動到達頂端?如果你檢查UIScrollView.h,那麼我提到的最後一個叫做scrollViewDidScrollToTop的另一種方法。你在用嗎? –

+0

所以在storybaord中這個滾動視圖應該重疊所有內容嗎? –