如何在 iOS 中为 Swift 3/4 移除 UIPageViewController 弹跳效果
Posted
技术标签:
【中文标题】如何在 iOS 中为 Swift 3/4 移除 UIPageViewController 弹跳效果【英文标题】:How to remove UIPageViewController's Bounce effect in iOS for Swift 3/4 【发布时间】:2018-03-12 06:31:59 【问题描述】:这是我的问题的截图:
【问题讨论】:
【参考方案1】:首先在你的 UIPageViewController 中找到滚动视图并添加 UIScrollViewDelegate
for view in self.pageViewController!.view.subviews
if let subView = view as? UIScrollView
subView.delegate = self
subView.isScrollEnabled = true
subView.bouncesZoom = false
(在您的 ViewController 类中扩展 UIScrollViewDelegate)
对于方法“scrollViewDidScroll”和“scrollViewWillEndDragging”上的 UIScrollViewDelegate,您可以添加类似这样的内容
func scrollViewDidScroll(_ scrollView: UIScrollView)
if(currentIndex == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width)
scrollView.contentOffset = CGPoint(x:scrollView.bounds.size.width, y:0.0)
else if(currentIndex == 2 && scrollView.contentOffset.x > scrollView.bounds.size.width)
scrollView.contentOffset = CGPoint(x:scrollView.bounds.size.width, y:0.0)
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
if(currentIndex == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width)
scrollView.contentOffset = CGPoint(x:scrollView.bounds.size.width, y:0.0)
else if(currentIndex == 2 && scrollView.contentOffset.x > scrollView.bounds.size.width)
scrollView.contentOffset = CGPoint(x:scrollView.bounds.size.width, y:0.0)
【讨论】:
【参考方案2】:我已经像下面的代码那样做了。
func scrollViewDidScroll(scrollView: UIScrollView)
if currentIndex == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width
scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
else if currentIndex == totalViewControllers - 1 && scrollView.contentOffset.x > scrollView.bounds.size.width
scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
if currentIndex == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width
scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
else if currentIndex == totalViewControllers - 1 && scrollView.contentOffset.x > scrollView.bounds.size.width
scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
【讨论】:
感谢您的回答!以上是关于如何在 iOS 中为 Swift 3/4 移除 UIPageViewController 弹跳效果的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Swift 中为以下观察删除 GeoFire 句柄?
如何在 Swift 中为 iOS 10 和 iOS 9.3 初始化 NSManagedObject 子类