2015-08-25 91 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中这个滚动视图应该重叠所有内容吗? –