ios - 最佳方法 : Swipe between scrollable views or view controllers

标签 ios swift uiview uiviewcontroller

我的应用程序中有几个屏幕看起来像这样(不是来 self 的应用程序,是在网上找到的):

enter image description here

其中一个是实际的介绍屏幕,如上图所示,另一个将在最后一个 View 中显示一些数据和“我同意”按钮。

在所有情况下,用户都必须在 View / View Controller 之间滑动。

我知道我可以像这样在 View Controller 之间滑动:Setting up UIScrollView to swipe between 3 view controllers我也可以使用这个在 View 之间滑动:

func respondToSwipeGesture(_ gesture: UIGestureRecognizer)
{
    print("respondToSwipeGesture")

    if let swipeGesture = gesture as? UISwipeGestureRecognizer
    {
        switch swipeGesture.direction
        {
            case UISwipeGestureRecognizerDirection.right:
                    if(isLeftToRightActive == true)
                    {
                        print("Swiped right")
                        moveTheMainView()
                    }
            case UISwipeGestureRecognizerDirection.left:
                    if(isLeftToRightActive == false)
                    {
                        print("Swiped left")
                        moveTheMainView()
                    }
            default:
                break
        }
    }
}

func moveTheMainView()
{
    print("moveTheMainView")

    let mainViewContainerHeight = self.vMainViewContainer.frame.height
    let mainViewContainerWidth = self.vMainViewContainer.frame.width

    let mainViewContainerModifiedLeft : CGFloat = mainViewContainerWidth / 2


    if(isLeftToRightActive == false)
    {
        print("Move the main view to the right")

        let urlCRTable = Bundle.main.url(forResource: "CRCharts", withExtension: "html")
        let crTableRequestObj = URLRequest(url: urlCRTable!)
        self.wvCRTable.loadRequest(crTableRequestObj)

        ivIndicator.image = UIImage(named: "SecondOn")


        isLeftToRightActive = true
    }
    else
    {
        print("Move the main view to the left")

        let urlCRTable = Bundle.main.url(forResource: "CRTable", withExtension: "html")
        let crTableRequestObj = URLRequest(url: urlCRTable!)
        self.wvCRTable.loadRequest(crTableRequestObj)

        ivIndicator.image = UIImage(named: "FirstOn")

        isLeftToRightActive = false
    }
}

所以我想知道执行此操作的最佳/正确方法是什么:使用多个 View Controller 或在同一个 View Controller 中使用多个 View ?或者还有其他任何被认为是正确的方法吗?

最佳答案

我通常使用 PageViewController 进行这样的演练。您应该在屏幕截图上创建这样的场景。和类:

class WalkthroughContentViewController: UIViewController {

@IBOutlet var headingLabel: UILabel!
@IBOutlet var contentLabel: UILabel!
@IBOutlet var contentImageView: UIImageView!
@IBOutlet var pageControl: UIPageControl!
@IBOutlet var forwardButton: UIButton!

var index = 0
var heading = ""
var imageFile = ""
var content = ""

override func viewDidLoad() {
    super.viewDidLoad()

    headingLabel.text = heading
    contentLabel.text = content
    contentImageView.image = UIImage(named: imageFile)
    pageControl.currentPage = index

    switch index {
    case 0...1: forwardButton.setTitle("NEXT", for: .normal)
    case 2: forwardButton.setTitle("DONE", for: .normal)
    default: break
    }

}
@IBAction func nextButtonTapped(sender: UIButton) {

    switch index {
    case 0...1: // Next Button
        let pageViewController = parent as! WalkthroughPageViewController
        pageViewController.forward(index: index)

    case 2: // Done Button
        UserDefaults.standard.set(true, forKey: "hasViewedWalkthrough")

        // Add Quick Actions
        if traitCollection.forceTouchCapability == UIForceTouchCapability.available {
            let bundleIdentifier = Bundle.main.bundleIdentifier
            let shortcutItem1 = UIApplicationShortcutItem(type: "\(bundleIdentifier).OpenFavorites", localizedTitle: "Show Favorites", localizedSubtitle: nil, icon: UIApplicationShortcutIcon(templateImageName: "favorite-shortcut"), userInfo: nil)
            let shortcutItem2 = UIApplicationShortcutItem(type: "\(bundleIdentifier).OpenDiscover", localizedTitle: "Discover Restaurants", localizedSubtitle: nil, icon: UIApplicationShortcutIcon(templateImageName: "discover-shortcut"), userInfo: nil)
            let shortcutItem3 = UIApplicationShortcutItem(type: "\(bundleIdentifier).NewRestaurant", localizedTitle: "New Restaurant", localizedSubtitle: nil, icon: UIApplicationShortcutIcon(type: .add), userInfo: nil)
            UIApplication.shared.shortcutItems = [shortcutItem1, shortcutItem2, shortcutItem3]
        }

        dismiss(animated: true, completion: nil)

    default: break

    }
}
}

class WalkthroughPageViewController: UIPageViewController, UIPageViewControllerDataSource {

var pageHeadings = ["Personalize", "Locate", "Discover"]
var pageImages = ["foodpin-intro-1", "foodpin-intro-2", "foodpin-intro-3"]
var pageContent = ["Pin your favorite restaurants and create your own food guide",
                   "Search and locate your favourite restaurant on Maps",
                   "Find restaurants pinned by your friends and other foodies around the world"]

override func viewDidLoad() {
    super.viewDidLoad()

    // Set the data source to itself
    dataSource = self

    // Create the first walkthrough screen
    if let startingViewController = contentViewController(at: 0) {
        setViewControllers([startingViewController], direction: .forward, animated: true, completion: nil)
    }
}

// MARK: - UIPageViewControllerDataSource Methods

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

    var index = (viewController as! WalkthroughContentViewController).index
    index -= 1

    return contentViewController(at: index)
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

    var index = (viewController as! WalkthroughContentViewController).index
    index += 1

    return contentViewController(at: index)
}

// MARK: - Helper Methods

func contentViewController(at index: Int) -> WalkthroughContentViewController? {
    if index < 0 || index >= pageHeadings.count {
        return nil
    }

    // Create a new view controller and pass suitable data.
    if let pageContentViewController = storyboard?.instantiateViewController(withIdentifier: "WalkthroughContentViewController") as? WalkthroughContentViewController {

        pageContentViewController.imageFile = pageImages[index]
        pageContentViewController.heading = pageHeadings[index]
        pageContentViewController.content = pageContent[index]
        pageContentViewController.index = index

        return pageContentViewController
    }

    return nil
}

func forward(index: Int) {
    if let nextViewController = contentViewController(at: index + 1) {
        setViewControllers([nextViewController], direction: .forward, animated: true, completion: nil)
    }
}

}

scenes

关于ios - 最佳方法 : Swipe between scrollable views or view controllers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43841690/

相关文章:

ios - 克隆时 cocoa pod 卡住

iOS swift : Rotate and Scale UIView without resizing

ios - Objective-C:从大型数据结构中有效地删除重复项

ios - 如何让 gif 在 UIWebView 中永不停止?

ios - 整数文字存储到 'UInt' 时溢出

ios - Swift 1.2 崩溃与 .lowercaseString

ios - 在 iOS 中,我如何使用动量实现在另一个 View 中滑动 View ?

ios - imageView 设置为 UIViewContentModeScaleAspectFill 的 UIButton 不会完全填充小图像

ios - willAnimateRotationToInterfaceOrientation 未从trigger.io 模块监听器调用

objective-c - Swift 继承问题 - 无法访问继承的属性