Swift -滚动视图的问题(需要在拖拽时打印)



我正在测试Jake Spracher的滑动控制器/手势与他的SnapchatSwipeView (https://github.com/jakespracher/Snapchat-Swipe-View)

我已经设置了一个topVC,leftVC,rightVC和middleVC(主VC)。

我设法捕捉当用户从中心滑动到右VC或左VC时,使用:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
if delegate != nil && !delegate!.outerScrollViewShouldScroll() && !directionLockDisabled {
let newOffset = CGPoint(x: self.initialContentOffset.x, y: self.initialContentOffset.y)

self.scrollView!.setContentOffset(newOffset, animated:  false)   
}

if (scrollView.contentOffset) == CGPoint(x: 750.0, y: 0.0) {
print ("TEST OK LEFT!")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "loadRinkbox"), object: nil)
}
if (scrollView.contentOffset) == CGPoint(x: 0.0, y: 0.0) {
print ("TEST OK LEFT!")
}
if (scrollView.contentOffset) == CGPoint(x: -750.0, y: 0.0) {
print ("TEST OK RIGHT")  
}
if (scrollView.contentOffset) == CGPoint(x: 375, y: 0.0) {
print ("TEST OK!")
}
}

但是我无法捕捉用户从centerVC滑动到topVC,从topVC滑动到centerVC。我已经尝试了很多方法,但我都没有成功。

我的代码是swift 4.

为了清楚起见,我把两个完整的swift文件放在这里。万分感谢那些能帮助我的人!

ContainerViewController.swift

//
//  ContainerViewController.swift
//  SnapchatSwipeView
//
//  Created by Jake Spracher on 8/9/15.
//  Copyright (c) 2015 Jake Spracher. All rights reserved.
//
import UIKit
import AVFoundation
protocol SnapContainerViewControllerDelegate {
//  print "Snapcontainerview"
func outerScrollViewShouldScroll() -> Bool
}
class SnapContainerViewController: UIViewController, UIScrollViewDelegate {
var captureSession : AVCaptureSession!
private var current: UIViewController
//  print "2"
var deeplink: DeeplinkType? {
didSet {
handleDeeplink()
}
}

init() {
current = SplashViewController()
super.init(nibName:  nil, bundle: nil)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var topVc: UIViewController?
var leftVc: UIViewController!
var middleVc: UIViewController!
var rightVc: UIViewController!
var bottomVc: UIViewController?

var directionLockDisabled: Bool!

var horizontalViews = [UIViewController]()
var veritcalViews = [UIViewController]()

var initialContentOffset = CGPoint() // scrollView initial offset
var middleVertScrollVc: VerticalScrollViewController!
var scrollView: UIScrollView!
var delegate: SnapContainerViewControllerDelegate?

class func containerViewWith(_ leftVC: UIViewController,
middleVC: UIViewController,
rightVC: UIViewController,
topVC: UIViewController?=nil,
bottomVC: UIViewController?=nil,
directionLockDisabled: Bool?=false) -> SnapContainerViewController {
let container = SnapContainerViewController()


container.directionLockDisabled = directionLockDisabled

container.topVc = topVC
container.leftVc = leftVC
container.middleVc = middleVC
container.rightVc = rightVC
container.bottomVc = bottomVC
return container
}


var scrollOffSetClosure: ((_ offset: CGFloat) -> Void)?
// code truncated for brevity
func scrollViewDidScroll2(_ scrollView: UIScrollView) {
scrollOffSetClosure!(scrollView.contentOffset.x)
print("test here")

}



func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
print("end scroll")
}
override func viewDidLoad() {
super.viewDidLoad()

addChildViewController(current)
current.view.frame = view.bounds
view.addSubview(current.view)
current.didMove(toParentViewController: self)

}

func setupVerticalScrollView() {
middleVertScrollVc = VerticalScrollViewController.verticalScrollVcWith(middleVc: middleVc as! Camera,
         topVc: topVc,
         bottomVc: bottomVc)
delegate = middleVertScrollVc



}

func setupHorizontalScrollView() {
scrollView = UIScrollView()
scrollView.isPagingEnabled = true
scrollView.showsHorizontalScrollIndicator = false
scrollView.bounces = false
print ("6")

let view = (
x: self.view.bounds.origin.x,
y: self.view.bounds.origin.y,
width: self.view.bounds.width,
height: self.view.bounds.height
)

scrollView.frame = CGRect(x: view.x,
y: view.y,
width: view.width,
height: view.height
)

self.view.addSubview(scrollView)

let scrollWidth  = 3 * view.width
let scrollHeight  = view.height
scrollView.contentSize = CGSize(width: scrollWidth, height: scrollHeight)

leftVc.view.frame = CGRect(x: 0,
y: 0,
width: view.width,
height: view.height
)

middleVertScrollVc.view.frame = CGRect(x: view.width,
y: 0,
width: view.width,
height: view.height
)

rightVc.view.frame = CGRect(x: 2 * view.width,
y: 0,
width: view.width,
height: view.height
)

addChildViewController(leftVc)
addChildViewController(middleVertScrollVc)
addChildViewController(rightVc)

scrollView.addSubview(leftVc.view)
scrollView.addSubview(middleVertScrollVc.view)
scrollView.addSubview(rightVc.view)

leftVc.didMove(toParentViewController: self)
middleVertScrollVc.didMove(toParentViewController: self)
rightVc.didMove(toParentViewController: self)

scrollView.contentOffset.x = middleVertScrollVc.view.frame.origin.x
scrollView.delegate = self

}

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
//  print ("Scrollviewvillbegindragging scrollView.contentOffset (scrollView.contentOffset)")
self.initialContentOffset = scrollView.contentOffset
}




func scrollViewDidScroll(_ scrollView: UIScrollView) {




if delegate != nil && !delegate!.outerScrollViewShouldScroll() && !directionLockDisabled {
let newOffset = CGPoint(x: self.initialContentOffset.x, y: self.initialContentOffset.y)
// print(newOffset.x)
//print(newOffset.y)
// Setting the new offset to the scrollView makes it behave like a proper
// directional lock, that allows you to scroll in only one direction at any given time
self.scrollView!.setContentOffset(newOffset, animated:  false)
//   print ("newOffset (newOffset)")

// tell child views they have appeared / disappeared



}


if (scrollView.contentOffset) == CGPoint(x: 0.0, y: 667.0) {print ("aaa!")}
if (scrollView.contentOffset) == CGPoint(x: 0.0, y: -667.0) {print ("bbb!")}
if (scrollView.contentOffset) == CGPoint(x: 0.0, y: 375) {print ("aaccca!")}
if (scrollView.contentOffset) == CGPoint(x: 0.0, y: -375) {print ("ddddd!")}
if (scrollView.contentOffset) == CGPoint(x: 0.0, y: 0.0) {print ("eeeeeee!")}
//     if (scrollView.contentOffset) == CGPoint(x: 0.0, y: 667.0) {print ("ffffff!")}
//    if (scrollView.contentOffset) == CGPoint(x: 0.0, y: 667.0) {print ("aggggggggaa!")}


// print ("scrollViewDidScroll scrollView.contentOffset (scrollView.contentOffset)")
// scrollView.contentOffset
if (scrollView.contentOffset) == CGPoint(x: 750.0, y: 0.0) {
print ("TEST OK LEFT!")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "loadRinkbox"), object: nil)
}
if (scrollView.contentOffset) == CGPoint(x: 0.0, y: 0.0) {


print ("TEST OK RIGHT!")

}
if (scrollView.contentOffset) == CGPoint(x: -750.0, y: 0.0) {
print ("TEST OK LEFT")

}
if (scrollView.contentOffset) == CGPoint(x: 375, y: 0.0) {
print ("TEST LEFT/RIGHT OK!")

}
}



private func animateFadeTransition(to new: UIViewController, completion: (() -> Void)? = nil) {
print ("Ca va dans animateFadeTransition")
current.willMove(toParentViewController: nil)
addChildViewController(new)
transition(from: current, to: new, duration: 0.3, options: [.transitionCrossDissolve, .curveEaseOut], animations: {

}) { completed in
self.current.removeFromParentViewController()
new.didMove(toParentViewController: self)
self.current = new
completion?()
}
}

private func animateDismissTransition(to new: UIViewController, completion: (() -> Void)? = nil) {
print ("Ca va dans animateDismissTransition")

let initialFrame = CGRect(x: -view.bounds.width, y: 0, width: view.bounds.width, height: view.bounds.height)
current.willMove(toParentViewController: nil)
addChildViewController(new)
new.view.frame = initialFrame

transition(from: current, to: new, duration: 0.3, options: [], animations: {
new.view.frame = self.view.bounds
}) { completed in
self.current.removeFromParentViewController()
new.didMove(toParentViewController: self)
self.current = new
completion?()
}

}



}

VerticalScrollViewController

/  MiddleScrollViewController.swift
//  SnapchatSwipeView
//
//  Created by Jake Spracher on 12/14/15.
//  Copyright © 2015 Jake Spracher. All rights reserved.
//
import UIKit
class VerticalScrollViewController: UIViewController, SnapContainerViewControllerDelegate {
var topVc: UIViewController!
var middleVc: UIViewController!
var bottomVc: UIViewController!
var scrollView: UIScrollView!

class func verticalScrollVcWith(middleVc: UIViewController,
topVc: UIViewController?=nil,
bottomVc: UIViewController?=nil) -> VerticalScrollViewController {
let middleScrollVc = VerticalScrollViewController()

middleScrollVc.topVc = topVc
middleScrollVc.middleVc = middleVc
middleScrollVc.bottomVc = bottomVc

return middleScrollVc
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view:
setupScrollView()
}

func setupScrollView() {
scrollView = UIScrollView()
scrollView.isPagingEnabled = true
scrollView.showsVerticalScrollIndicator = false
scrollView.bounces = false

let view = (
x: self.view.bounds.origin.x,
y: self.view.bounds.origin.y,
width: self.view.bounds.width,
height: self.view.bounds.height
)

scrollView.frame = CGRect(x: view.x, y: view.y, width: view.width, height: view.height)
self.view.addSubview(scrollView)

let scrollWidth: CGFloat  = view.width
var scrollHeight: CGFloat

switch (topVc, bottomVc) {
case (nil, nil):
scrollHeight  = view.height
middleVc.view.frame = CGRect(x: 0, y: 0, width: view.width, height: view.height)

addChildViewController(middleVc)
scrollView.addSubview(middleVc.view)
middleVc.didMove(toParentViewController: self)
case (_?, nil):
scrollHeight  = 2 * view.height
topVc.view.frame = CGRect(x: 0, y: 0, width: view.width, height: view.height)
middleVc.view.frame = CGRect(x: 0, y: view.height, width: view.width, height: view.height)

addChildViewController(topVc)
addChildViewController(middleVc)

scrollView.addSubview(topVc.view)
scrollView.addSubview(middleVc.view)

topVc.didMove(toParentViewController: self)
middleVc.didMove(toParentViewController: self)

scrollView.contentOffset.y = middleVc.view.frame.origin.y
case (nil, _?):
scrollHeight  = 2 * view.height
middleVc.view.frame = CGRect(x: 0, y: 0, width: view.width, height: view.height)
bottomVc.view.frame = CGRect(x: 0, y: view.height, width: view.width, height: view.height)

addChildViewController(middleVc)
addChildViewController(bottomVc)

scrollView.addSubview(middleVc.view)
scrollView.addSubview(bottomVc.view)

middleVc.didMove(toParentViewController: self)
bottomVc.didMove(toParentViewController: self)

scrollView.contentOffset.y = 0
default:
scrollHeight  = 3 * view.height
topVc.view.frame = CGRect(x: 0, y: 0, width: view.width, height: view.height)
middleVc.view.frame = CGRect(x: 0, y: view.height, width: view.width, height: view.height)
bottomVc.view.frame = CGRect(x: 0, y: 2 * view.height, width: view.width, height: view.height)

addChildViewController(topVc)
addChildViewController(middleVc)
addChildViewController(bottomVc)

scrollView.addSubview(topVc.view)
scrollView.addSubview(middleVc.view)
scrollView.addSubview(bottomVc.view)

topVc.didMove(toParentViewController: self)
middleVc.didMove(toParentViewController: self)
bottomVc.didMove(toParentViewController: self)

scrollView.contentOffset.y = middleVc.view.frame.origin.y
}

scrollView.contentSize = CGSize(width: scrollWidth, height: scrollHeight)
}
// MARK: - SnapContainerViewControllerDelegate Methods

func outerScrollViewShouldScroll() -> Bool {
if scrollView.contentOffset.y < middleVc.view.frame.origin.y || scrollView.contentOffset.y > 2*middleVc.view.frame.origin.y {
return false
} else {
return true
}
}

}

MiddleScrollViewController.swift中,使控制器符合UIScrollViewDelegate:

class VerticalScrollViewController: UIViewController,
SnapContainerViewControllerDelegate,
UIScrollViewDelegate {

在该类中,在setupScrollView()中,设置委托:

func setupScrollView() {
scrollView = UIScrollView()
// set the delegate to self
scrollView.delegate = self
// the rest of the existing code...

仍然在那个类中,实现didScroll(或任何其他你想处理的委托函数):

func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("Vertical Scroll - contentOffset:", scrollView.contentOffset)
}

现在,当您垂直滚动时,您应该在调试控制台中获得大量打印行:

Vertical Scroll - contentOffset: (0.0, 561.0)
Vertical Scroll - contentOffset: (0.0, 560.0)
Vertical Scroll - contentOffset: (0.0, 559.0)
Vertical Scroll - contentOffset: (0.0, 558.0)

如果你想让你的SnapContainerViewController类在垂直滚动发生时被通知,你可能需要使用一个新的协议/委托,以便VerticalScrollViewController可以发送该信息。

最新更新