Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support swiftUI #126

Open
xakacuk opened this issue Aug 6, 2020 · 3 comments
Open

support swiftUI #126

xakacuk opened this issue Aug 6, 2020 · 3 comments

Comments

@xakacuk
Copy link

xakacuk commented Aug 6, 2020

i can use in swiftUI? if yes, so how?

@igork-ramotion
Copy link
Contributor

@xakacuk hey!) this library is currently not support swiftUI.
You can try to wrap it in UIViewControllerRepresentable

@igork-ramotion
Copy link
Contributor

I'll leave it as enhancement for future, hope sometime we will have time to make it :)

@starkdmi
Copy link

starkdmi commented Aug 31, 2020

import SwiftUI
import UIKit
import Foundation
import paper_onboarding // https://github.com/Ramotion/paper-onboarding

final class PaperOnboardingUIKit: NSObject, UIViewControllerRepresentable {
    typealias UIViewControllerType = UIViewController

    var items: [OnboardingItemInfo]
    init(_ items: [OnboardingItemInfo]) {
        self.items = items
    }

    func makeUIViewController(context: Context) -> UIViewController {
        let controller = UIViewController()
        
        // Init PaperOnboarding
        let onboarding = PaperOnboarding()
        onboarding.delegate = self
        onboarding.dataSource = self
        onboarding.translatesAutoresizingMaskIntoConstraints = false
        
        controller.view.addSubview(onboarding)
        
        // Add constraints
        for attribute: NSLayoutConstraint.Attribute in [.left, .right, .top, .bottom] {
            let constraint = NSLayoutConstraint(item: onboarding, attribute: attribute, relatedBy: .equal, toItem: controller.view, attribute: attribute, multiplier: 1, constant: 0)
            controller.view.addConstraint(constraint)
        }
        
        return controller
    }
    
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
        
    }
}

extension PaperOnboardingUIKit: PaperOnboardingDataSource, PaperOnboardingDelegate {
    func onboardingItem(at index: Int) -> OnboardingItemInfo {
        items[index]
    }
    
    func onboardingItemsCount() -> Int {
        return items.count
    }
    
    func onboardingWillTransitonToIndex(_ index: Int) {
        // Transition Delegate
    }
    
    // Item Customization
    @objc(onboardingConfigurationItem:index:) func onboardingConfigurationItem(_ item: OnboardingContentViewItem, index: Int) {
        // Setup Labels
        //item.titleLabel?.textColor = .white
        //item.descriptionLabel?.textColor = .white
        
        // Rounded Image
        //item.imageView?.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
        //item.imageView?.layer.cornerRadius = 100
        //item.imageView?.layer.masksToBounds = true
        //item.imageView?.contentMode = .scaleToFill
    }
    
    /* Bottom Icon Customization
     func onboardingPageItemColor(at index: Int) -> UIColor { }
     func onboardinPageItemRadius() -> CGFloat { }
     func onboardingPageItemSelectedRadius() -> CGFloat { }
     */
}

struct PaperOnboardingSwiftUI: View {
    static let titleFont = UIFont.boldSystemFont(ofSize: 36.0)
    static let descriptionFont = UIFont.systemFont(ofSize: 14.0)
    
    let items = [
        OnboardingItemInfo(informationImage: UIImage(named: "image1")!,
                           title: "Title 1",
                           description: "Description 1",
                           pageIcon: UIImage(named: "icon1")!,
                           color: .systemBlue,
                           titleColor: .white,
                           descriptionColor: .white,
                           titleFont: titleFont,
                           descriptionFont: descriptionFont),
        
        OnboardingItemInfo(informationImage: UIImage(named: "image2")!,
                           title: "Title 2",
                           description: "Description 2",
                           pageIcon: UIImage(named: "icon2")!,
                           color: .systemGreen,
                           titleColor: .white,
                           descriptionColor: .white,
                           titleFont: titleFont,
                           descriptionFont: descriptionFont),
        
        OnboardingItemInfo(informationImage: UIImage(named: "image3")!,
                           title: "Title 3",
                           description: "Description 3",
                           pageIcon: UIImage(named: "icon3")!,
                           color: .systemRed,
                           titleColor: .white,
                           descriptionColor: .white,
                           titleFont: titleFont,
                           descriptionFont: descriptionFont),
    ]

    var body: some View {
        PaperOnboardingUIKit(items).edgesIgnoringSafeArea(.all)
    }
}

struct PaperOnboardingSwiftUI_Previews: PreviewProvider {
    static var previews: some View {
        PaperOnboardingSwiftUI().previewDevice(PreviewDevice(rawValue: "iPhone XS"))
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants