Skip to content

Commit

Permalink
add swiftui example
Browse files Browse the repository at this point in the history
  • Loading branch information
emrearmagan committed Jan 5, 2025
1 parent 06cab48 commit 8402934
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 12 deletions.
29 changes: 24 additions & 5 deletions MemojiViewDemo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="20037" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="23504" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="Oq1-8p-mav">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="20020"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="23506"/>
<capability name="Named colors" minToolsVersion="9.0"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<!--Example View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="MemojiViewDemo" customModuleProvider="target" sceneMemberID="viewController">
<viewController id="BYZ-38-t0r" customClass="ExampleViewController" customModule="MemojiViewDemo" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
<color key="backgroundColor" name="backgroundColor"/>
</view>
<navigationItem key="navigationItem" id="Rqa-x0-Ii4"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="139" y="129"/>
<point key="canvasLocation" x="1047.8260869565217" y="128.57142857142856"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="NgA-8Y-WWg">
<objects>
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="Oq1-8p-mav" sceneMemberID="viewController">
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" id="c3A-0t-anM">
<rect key="frame" x="0.0" y="48" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
<connections>
<segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="YLw-Qt-zKM"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="e7B-dI-COG" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="137.68115942028987" y="128.57142857142856"/>
</scene>
</scenes>
<resources>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// ViewController.swift
// ExampleViewController.swift
// MemojiViewDemo
//
// Created by Emre Armagan on 10.04.22.
Expand All @@ -8,22 +8,22 @@
import MemojiView
import UIKit

class ViewController: UIViewController, MemojiViewDelegate {
class ExampleViewController: UIViewController, MemojiViewDelegate {
let size = CGSize(width: 180, height: 180)
static let color = UIColor(red: 156 / 255, green: 130 / 255, blue: 255 / 255, alpha: 1)
static let defaultImage = UIImage(named: "user.image")

private let memojiView: MemojiView = {
let view = MemojiView(frame: .zero)
view.image = ViewController.defaultImage
view.backgroundColor = ViewController.color.withAlphaComponent(0.4)
view.image = ExampleViewController.defaultImage
view.backgroundColor = ExampleViewController.color.withAlphaComponent(0.4)
return view
}()

private let miniMemojiView: MemojiView = {
let view = MemojiView(frame: .zero)
view.image = ViewController.defaultImage
view.backgroundColor = ViewController.color
view.image = ExampleViewController.defaultImage
view.backgroundColor = ExampleViewController.color
view.isEditable = false
return view
}()
Expand All @@ -48,12 +48,19 @@ class ViewController: UIViewController, MemojiViewDelegate {
let padding: CGFloat = (type == .memoji) ? 15 : 35
memojiView.layoutMargins = UIEdgeInsets(top: padding, left: padding, bottom: padding, right: padding)
}

@objc private func showSwiftUIExample() {
navigationController?.pushViewController(SwiftUIExampleViewController(), animated: true)
}
}

// MARK: - UI

extension ViewController {
extension ExampleViewController {
private func setupView() {
title = "MemojiView"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "SwiftUI", style: .done, target: self, action: #selector(showSwiftUIExample))

// Adding some content to view for demonstration purposes
let rect = CGRect(origin: CGPoint(x: 0, y: 100 + size.height + 18), size: .init(width: view.frame.width, height: 30))
let nameLabel = UILabel(frame: rect)
Expand Down
54 changes: 54 additions & 0 deletions MemojiViewDemo/SwiftUI/MemojiContentView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// MemojiContentView.swift
// MemojiView
//
// Created by Emre Armagan on 05.01.25.
// Copyright © 2025 Emre Armagan. All rights reserved.
//

import MemojiView
import SwiftUI

struct MemojiContentView: View {
@State private var displayedImage: UIImage?
@State private var displayedType: MemojiImageType?
@State private var isEditable: Bool = true

var body: some View {
VStack(spacing: 20) {
Text("Memoji Editor").font(.headline)

MemojiViewRepresentable(
image: $displayedImage,
memojiType: $displayedType,
isEditable: $isEditable,
maxLetters: 10,
textColor: .blue
) { updatedImage, updatedType in
print("Updated image: \(updatedImage?.description ?? "nil")")
print("Updated type: \(updatedType)")
}
.frame(height: 200)
.border(Color.gray, width: 1)

Toggle("Enable Editing", isOn: $isEditable)
.padding()

if let image = displayedImage {
Image(uiImage: image)
.resizable()
.scaledToFit()
.frame(height: 100)
}
}
.padding()
}
}

struct MemojiContentView_Previews: PreviewProvider {
static var previews: some View {
MemojiContentView()
.previewDevice("iPhone 14 Pro")
.previewDisplayName("ContentView Preview")
}
}
35 changes: 35 additions & 0 deletions MemojiViewDemo/SwiftUI/SwiftUIExampleViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// SwiftUIExampleViewController.swift
// MemojiView
//
// Created by Emre Armagan on 05.01.25.
// Copyright © 2025 Emre Armagan. All rights reserved.
//

import MemojiView
import SwiftUI
import UIKit

class SwiftUIExampleViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupSwiftUIContent()
}

private func setupSwiftUIContent() {
let hostingController = UIHostingController(rootView: MemojiContentView())
addChild(hostingController)

hostingController.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(hostingController.view)

NSLayoutConstraint.activate([
hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
hostingController.view.topAnchor.constraint(equalTo: view.topAnchor),
hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])

hostingController.didMove(toParent: self)
}
}

0 comments on commit 8402934

Please sign in to comment.