iOS/ViewController.swift

/*
 Copyright (C) 2016 Apple Inc. All Rights Reserved.
 See LICENSE.txt for this sample’s licensing information
 
 Abstract:
 The app's view controller class.
 */
 
import SceneKit
 
class ViewController: UIViewController, UITabBarDelegate {
 
    @IBOutlet private weak var sceneView: SCNView!
    @IBOutlet private weak var tabBar: UITabBar!
    @IBOutlet private weak var configBarButtonItem: UIBarButtonItem!
    
    private var controller: Controller!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        controller = Controller(view: sceneView)
        controller.selectScene(at: 0)
        navigationItem.title = controller.sceneTitles[0]
 
        configureTabBar()
    }
    
    // MARK: UI for Visualization Type
    
    @IBAction func pickVisualization(_ sender: AnyObject?) {
        let alert = UIAlertController(title: "Left Side Visualization", message: nil, preferredStyle: .actionSheet)
        let clampAction = UIAlertAction(title: "Clamp to sRGB", style: .default) { [weak self] action in
            self?.controller.visualizationType = .gamutClamp
        }
        if controller.visualizationType == .gamutClamp {
            clampAction.isEnabled = false
        }
        alert.addAction(clampAction)
        let wideGamutHighlightAction = UIAlertAction(title: "Highlight Wide Gamut", style: .default) { [weak self] action in
            self?.controller.visualizationType = .wideGamutHighlight
        }
        if controller.visualizationType == .wideGamutHighlight {
            wideGamutHighlightAction.isEnabled = false
        }
        alert.addAction(wideGamutHighlightAction)
        alert.popoverPresentationController?.barButtonItem = configBarButtonItem
 
        self.present(alert, animated: true, completion: nil)
    }
 
    // MARK: UI for Image Selection
    
    private func configureTabBar() {
        var items = [UITabBarItem]()
        
        for i in 0 ..< controller.sceneTitles.count {
            let item = UITabBarItem(title: controller.sceneTitles[i], image: nil, tag: i)
            items.append(item)
        }
        
        tabBar.items = items
        tabBar.delegate = self
    }
    
    // MARK: UITabBarDelegate Conformance
    
    func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        controller.selectScene(at: item.tag)
        navigationItem.title = controller.sceneTitles[item.tag]
    }
 
    // MARK: Split View Handle and Touch Events
    
    private func updateHandlePosition(_ touch: UITouch) {
        let location = touch.location(in: view)
        let splitFraction = Float(location.x / view.bounds.size.width)
        controller.splitFraction = splitFraction
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        updateHandlePosition(touches.first!)
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesMoved(touches, with: event)
        updateHandlePosition(touches.first!)
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        updateHandlePosition(touches.first!)
    }
    
}