2017-05-08 49 views
0

我在我的應用程序看起來像這樣的幾個屏幕(不是從我的應用程序,在網上找到的):最好的辦法:滾動視圖或視圖控制器之間的刷卡

enter image description here

其中之一是實際的介紹屏幕,如上面的一個,另一個將顯示一些數據和最後一個視圖上的「我同意」按鈕。

在所有情況下,用戶必須在視圖/視圖控制器之間滑動。

我知道我可以喜歡這個視圖控制器之間滑動:Setting up UIScrollView to swipe between 3 view controllers,我也可以在視圖之間採用滑動這樣的:

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 
    } 
} 

所以我想知道什麼是做到這一點的最好/正確的做法:以使用多個視圖控制器或在同一視圖控制器中使用多個視圖?還是有其他方式被認爲是corect?

+0

我建議你檢查[UIPageViewController(https://developer.apple.com/reference/uikit/uipageviewcontroller) –

+0

@AhmadF謝謝,我要一看。 – daydr3am3r

回答

1

我通常做演練這樣的,使用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

+0

謝謝,我會盡快查看PageViewController。 – daydr3am3r

1

如果你有更多的項目,你應該使用UICollectionView,每個項目(在你的圖像中的紅色背景)將是一個單元格。 這種方式的意見將被重用,性能更好。

+0

感謝您的建議,這是一個有趣的方法。 – daydr3am3r