以编程方式将UIScrollView滚动到Swift中的子UIView(子视图)的顶部

我的UIScrollView中有几个屏幕内容,只能垂直滚动。

我想以编程方式滚动到包含在其层次结构中的某个视图。

UIScrollView移动,以便子视图位于UIScrollView的顶部(动画或不动画)

这是我最后写的一个扩展。

用法:

从我的viewController调用,self.scrollView是UIScrollView的出口,self.commentsHeader是其中的一个视图,靠近底部:

self.scrollView.scrollToView(self.commentsHeader, animated: true) 

码:

您只需要scrollToView方法,但也可以使用scrollToBottom / scrollToTop方法,因为您可能也需要这些方法,但可以随意删除它们。

 extension UIScrollView { // Scroll to a specific view so that it's top is at the top our scrollview func scrollToView(view:UIView, animated: Bool) { if let origin = view.superview { // Get the Y position of your child view let childStartPoint = origin.convertPoint(view.frame.origin, toView: self) // Scroll to a rectangle starting at the Y of your subview, with a height of the scrollview self.scrollRectToVisible(CGRect(x:0, y:childStartPoint.y,width: 1,height: self.frame.height), animated: animated) } } // Bonus: Scroll to top func scrollToTop(animated: Bool) { let topOffset = CGPoint(x: 0, y: -contentInset.top) setContentOffset(topOffset, animated: animated) } // Bonus: Scroll to bottom func scrollToBottom() { let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom) if(bottomOffset.y > 0) { setContentOffset(bottomOffset, animated: true) } } } 
  scrollView.scrollRectToVisible(CGRect(x: x, y: y, width: 1, height: 1), animated: true) 

要么

 scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true) 

另一种方式是

 scrollView.contentOffset = CGPointMake(x,y); 

我用这样的动画做到了

 [UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ scrollView.contentOffset = CGPointMake(x, y); } completion:NULL]; 
 scrollView.setContentOffset(CGPoint, animated: Bool) 

点的y坐标是要相对于scrollView的内容视图显示的视图框架的y坐标。

这是我的答案,这很快。 这将无限滚动滚动视图中的页面。

 private func startBannerSlideShow() { UIView.animate(withDuration: 6, delay: 0.1, options: .allowUserInteraction, animations: { scrollviewOutlt.contentOffset.x = (scrollviewOutlt.contentOffset.x == scrollviewOutlt.bounds.width*2) ? 0 : scrollviewOutlt.contentOffset.x+scrollviewOutlt.bounds.width }, completion: { (status) in self.startBannerSlideShow() }) } 

用于在完成动画时滚动到顶部或底部

 // MARK: - UIScrollView extensions extension UIScrollView { /// Animate scroll to bottom with completion /// /// - Parameters: /// - duration: TimeInterval /// - completion: Completion block func animateScrollToBottom(withDuration duration: TimeInterval, completion: (()->())? = nil) { UIView.animate(withDuration: duration, animations: { [weak self] in self?.setContentOffset(CGPoint.zero, animated: false) }, completion: { finish in if finish { completion?() } }) } /// Animate scroll to top with completion /// /// - Parameters: /// - duration: TimeInterval /// - completion: Completion block func animateScrollToBottomTop(withDuration duration: TimeInterval, completion: (()->())? = nil) { UIView.animate(withDuration: duration, animations: { [weak self] in guard let `self` = self else { return } let desiredOffset = CGPoint(x: 0, y: -self.contentInset.top) self.setContentOffset(desiredOffset, animated: false) }, completion: { finish in if finish { completion?() } }) } }