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

Swift - Problem with scrollview (Need to print when dragging down)

我正在使用 Jake Spracher 的 SnapchatSwipeView (https://github.com/jakespracher/Snapchat-Swipe-View )

测试滑动 controller/gesture

我设置了topVC,leftVC,rightVC and middleVC(main 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!")
     }
}

但是当用户从中心VC滑动到顶部VC以及从顶部VC滑动到中心[=41时我无法捕获=]. 我尝试了很多东西,但我没能做到。

我的代码用于 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 {

在class中,在setupScrollView()中设置委托:

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

仍然在 class 中,实现 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 class,您可能需要使用新的 protocol/delegate,以便 VerticalScrollViewController 可以发送该信息。