Remodel-AR
  • Overview
  • ios guides
    • iOS Quick Start
    • iOS Walkthrough
      • SwiftUI Walkthrough
        • Section 1 - Setting up Remodel-AR in SwiftUI
        • Section 2 - Add a color picker
        • Section 3 - Add Export Features
        • Section 4 - Add Textured Paints
      • UIKit Walkthrough
        • Setting up Remodel-AR in UIKit
        • Adding a Color Picker
        • Getting to know Lidar scanning
        • Getting to know Legacy
        • Getting to know Floorplan
        • Getting to know Shader Paint
        • Section 2 - Add a color picker
        • Section 3 - Add Export Features
        • Section 4 - Add Textured Paints
    • iOS Library Documentation
      • Classes
        • ARController
        • ARStateModel
        • RemodelARLib
        • PassioConfiguration
      • Structures
      • Enumerations
      • Protocols
  • android guides
    • Android Quick Start
    • Android Walkthrough
      • Setting up Remodel-AR in Android
      • Paint Approaches
      • Getting to know Legacy
      • Getting to know Floor Plan
      • Getting to know Shader Approach
    • Android Library Documentation
      • Classes
        • PaintARActivity
        • OnCornerPlaced
        • GetWallPatchInfos
        • GetSavedImage
        • GetRawCameraImage
        • PassioSDKStatus
        • WallPatchInfo
    • ❔FAQs
Powered by GitBook
On this page
  1. ios guides
  2. iOS Walkthrough
  3. UIKit Walkthrough

Adding a Color Picker

PreviousSetting up Remodel-AR in UIKitNextGetting to know Lidar scanning

Last updated 1 year ago

In this section, we are going to create a UICollectionView that will enable you to select different colors. Start with the code from the end of the tutorial. A link to the starting project is provided here for your convenience.

Step 1

First, we need to create a source for our colors to pull from. Create a new folder called Model. Inside the folder, create a new file Model.swift and edit its contents as below.

import UIKit
impswort RemodelAR

//MARK: - Color picker
struct ColorPicker {
    let color: WallPaint
    
    static var colors: [ColorPicker] {
        [
            ColorPicker(color: WallPaint(id: "0", color: .orange)),
            ColorPicker(color: WallPaint(id: "1", color: .red)),
            ColorPicker(color: WallPaint(id: "2", color: .yellow)),
            ColorPicker(color: WallPaint(id: "3", color: .green)),
            ColorPicker(color: WallPaint(id: "4", color: .black)),
            ColorPicker(color: WallPaint(id: "5", color: .blue)),
            ColorPicker(color: WallPaint(id: "7", color: .brown)),
            ColorPicker(color: WallPaint(id: "8", color: .lightGray)),
            ColorPicker(color: WallPaint(id: "9", color: .darkGray)),
            ColorPicker(color: WallPaint(id: "10", color: .systemPink)),
            ColorPicker(color: WallPaint(id: "11", color: .purple)),
            ColorPicker(color: WallPaint(id: "12", color: .cyan)),
            ColorPicker(color: WallPaint(id: "14", color: .magenta)),
            ColorPicker(color: WallPaint(id: "15", color: .gray))
        ]
    }
}

Step 2

Create a new folder called CollectionView. Inside that folder, create another folder called Cells.

Step 3

Create a new file inside of the Cells folder. For Source, select Cocoa Touch Class. Make it a subclass of UICollectionViewCell, and check the "Also create XIB file" check box. Name it ColorPickerCell.

Step 4

Open ColorPickerCell.swift and update its code using the following.

import UIKit

final class ColorPickerCell: UICollectionViewCell {
    @IBOutlet weak var colorView: UIView!
}

extension ColorPickerCell {
    func configureCell(color: UIColor, indexPath: Int, selectedIndexPath: Int) {
        colorView.backgroundColor = color
        let borderWidth: CGFloat = indexPath == selectedIndexPath ? 5 : 0
        layer.borderColor = UIColor.white.cgColor
        layer.borderWidth = borderWidth
    }
}

Step 5

Open ColorPickerCell.xib and add a UIView and create constraints. Assign the view to the colorView variable in the cell. Change the identifier to ColorPickerCell.

Step 6

Create a new file in the CollectionView folder and name it ColorPickerCollectionView.swift. Edit its contents to match below.

import UIKit
import RemodelAR

final class ColorPickerCollectionView: UICollectionView {
    var colorPicker = [ColorPicker]() {
        didSet {
            reloadData()
        }
    }
    
    var arController: ARController?
    
    private let cellIdentidfier = "ColorPickerCell"
    private var selectedColor = 0
    
    override func awakeFromNib() {
        super.awakeFromNib()
        configure()
    }
}

extension ColorPickerCollectionView {
    private func configure() {
        delegate = self
        dataSource = self
        register(UINib(nibName: cellIdentidfier, bundle: nil), forCellWithReuseIdentifier: cellIdentidfier)
    }
}

extension ColorPickerCollectionView: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return colorPicker.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let colorPickerCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentidfier, for: indexPath) as? ColorPickerCell {
            
            let color = colorPicker[indexPath.item].color
            colorPickerCell.configureCell(color: color.color, indexPath: indexPath.item, selectedIndexPath: selectedColor)
            
            return colorPickerCell
        }
        return UICollectionViewCell()
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectedColor = indexPath.item
        arController?.setColor(paint: colorPicker[indexPath.item].color)
        reloadData()
    }
}

Step 7

Open Main.storyboard and add a UICollectionView and setup its constraints. Delete the default cell that was created inside of it. Change the UICollectionView class type to ColorPickerCollectionView. Change the flow type to horizontal. Create an IBOutlet connected to the view in ViewController named colorPickerCollectionView.

Step 8

Open ViewController and modify configureView() as below.

private func configureView() {
    arController = RemodelARLib.makeFloorplanARController(with: arscnView)
    arController?.setColor(paint: ColorPicker.colors[0].color)
    arController?.startScene(reset: true)
    
    colorPickerCollectionView.colorPicker = ColorPicker.colors
    colorPickerCollectionView.arController = arController
}

Step 9

Add the following extension to ViewController to enable touches.

extension ViewController {
    public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first
        else { return }
        
        let point = touch.location(in: arscnView)
        arController?.handleTouch(point: point)
    }
}

Step 10

Build and run. You should be able to scroll through the list and select different colors. The border should change to indicate which color is selected. Next we'll add a texture picker so we can apply textures to the walls.

If at any point you need help from the Passio team, please reach out to us at support@passiolife.com

Setting up Remodel-AR in UIKit
37KB
Setting up RemodelAR Code.zip
archive