我的UIScrollView中只有幾個屏幕值得我的內容垂直滾動。以編程方式將UIScrollView滾動到Swift中的子UIView(子視圖)的頂部
我想以編程方式滾動到包含在它的層次結構中某處的視圖。
的UIScrollView的移動,使得孩子的看法是在UIScrollView的頂部(或動畫與否)
我的UIScrollView中只有幾個屏幕值得我的內容垂直滾動。以編程方式將UIScrollView滾動到Swift中的子UIView(子視圖)的頂部
我想以編程方式滾動到包含在它的層次結構中某處的視圖。
的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(CGRectMake(0, childStartPoint.y, 1, 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.setContentOffset(CGPoint, animated: Bool)
其中該點的y座標是你要比較顯示到滾動視圖的內容視圖視圖的幀的y座標。
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];
對於滾動到頂部或底部,完成做動畫
// 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?() }
})
}
}
這是我的答案,這是迅速。這將無限滾動scrollview頁面。
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()
})
}
如果鍵盤在屏幕上,這對我來說效果不好。 而不是使用scrollRectToVisible我使用: setContentOffset(CGPoint(x:0,y:childStartPoint.y),animated:animated) – Nati