git ssb

1+

yi / localnative



Tree: 2c1932822a8e06b5876eb875f19924a15ae91539

Files: 2c1932822a8e06b5876eb875f19924a15ae91539 / localnative-ios / ln-ios / SceneDelegate.swift

4078 bytesRaw
1//
2// SceneDelegate.swift
3// localnative-ios
4//
5// Created by Yi Wang on 4/12/20.
6// Copyright © 2020 Yi Wang. All rights reserved.
7//
8
9import UIKit
10import SwiftUI
11
12class SceneDelegate: UIResponder, UIWindowSceneDelegate {
13
14 var window: UIWindow?
15
16
17 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
18 // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
19 // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
20 // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
21
22 // Create the SwiftUI view that provides the window contents.
23
24 let contentView = ContentView().environmentObject(AppState.getEnv())
25
26 // Use a UIHostingController as window root view controller.
27
28 if let windowScene = scene as? UIWindowScene {
29 let window = UIWindow(windowScene: windowScene)
30 window.rootViewController = UIHostingController(rootView: contentView)
31 self.window = window
32 window.makeKeyAndVisible()
33 }
34
35 AppState.search(input: "", offset: 0)
36 let tapGesture = AnyGestureRecognizer(target: window, action:#selector(UIView.endEditing))
37 tapGesture.requiresExclusiveTouchType = false
38 tapGesture.cancelsTouchesInView = false
39 tapGesture.delegate = self //I don't use window as delegate to minimize possible side effects
40 window?.addGestureRecognizer(tapGesture)
41 }
42
43 func sceneDidDisconnect(_ scene: UIScene) {
44 // Called as the scene is being released by the system.
45 // This occurs shortly after the scene enters the background, or when its session is discarded.
46 // Release any resources associated with this scene that can be re-created the next time the scene connects.
47 // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
48 }
49
50 func sceneDidBecomeActive(_ scene: UIScene) {
51 // Called when the scene has moved from an inactive state to an active state.
52 // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
53 }
54
55 func sceneWillResignActive(_ scene: UIScene) {
56 // Called when the scene will move from an active state to an inactive state.
57 // This may occur due to temporary interruptions (ex. an incoming phone call).
58 }
59
60 func sceneWillEnterForeground(_ scene: UIScene) {
61 // Called as the scene transitions from the background to the foreground.
62 // Use this method to undo the changes made on entering the background.
63 }
64
65 func sceneDidEnterBackground(_ scene: UIScene) {
66 // Called as the scene transitions from the foreground to the background.
67 // Use this method to save data, release shared resources, and store enough scene-specific state information
68 // to restore the scene back to its current state.
69 }
70
71
72}
73
74class AnyGestureRecognizer: UIGestureRecognizer {
75 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
76 //To prevent keyboard hide and show when switching from one textfield to another
77 if let textField = touches.first?.view, textField is UITextField {
78 state = .failed
79 } else {
80 state = .began
81 }
82 }
83
84 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
85 state = .ended
86 }
87
88 override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
89 state = .cancelled
90 }
91}
92
93extension SceneDelegate: UIGestureRecognizerDelegate {
94 func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
95 return true
96 }
97}
98

Built with git-ssb-web