Notice
Recent Posts
Recent Comments
Link
ยซ   2024/07   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

lgvv98

[iOS] ํ™”๋ฉด ์Šคํฌ๋ฆฐ์ƒท ๋ฐ ๋…นํ™” ๊ฐ์ง€ (feat. SwiftUI) ๋ณธ๋ฌธ

apple/๐Ÿš SwiftUI & Combine

[iOS] ํ™”๋ฉด ์Šคํฌ๋ฆฐ์ƒท ๋ฐ ๋…นํ™” ๊ฐ์ง€ (feat. SwiftUI)

๐Ÿฅ• ์บ๋Ÿฟ๋งจ 2022. 6. 2. 16:22

ํ™”๋ฉด ์Šคํฌ๋ฆฐ์ƒท ๋ฐ ๋…นํ™” ๊ฐ์ง€

 

โœ… ํ™”๋ฉด ๋…นํ™” ๋ฐ ์Šคํฌ๋ฆฐ์ƒท ๊ฐ์ง€์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…์‹œ๋‹ค.

 

์šฐ์„  ํ™”๋ฉด ๋…นํ™” ๋ฐ ์Šคํฌ๋ฆฐ์ƒท์€ NotificationCenter๋ฅผ ํ†ตํ•ด ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

// ํ™”๋ฉด์บก์ณ
NotificationCenter.default.publisher(for: UIScreen.capturedDidChangeNotification) 
// ํ™”๋ฉด๋…นํ™”
NotificationCenter.default.publisher(for: UIApplication.userDidTakeScreenshotNotification)

 

โœ… ์ฝ”๋“œ

import SwiftUI

struct ContentView: View {
    var body: some View {
        
        ZStack {
            MainView() // ๋ฉ”์ธ ์ปจํ…ํŠธ ๋ทฐ
            BlockView() // ๋ธ”๋Ÿญ ๋ทฐ
        }
        
    }
}

// ์–ผ๋Ÿฟ์ฐฝ ๋ฐ์ดํ„ฐ
struct AlertData: Identifiable {
    var id: UUID
    let title: String
    let message: String
    
    init(title: String = "์•ˆ๋…•ํ•˜์„ธ์š”!" , message : String = "์Šคํฌ๋ฆฐ ์บก์ณ๋ฅผ ํ•˜์…จ๊ตฐ์š”! ๐Ÿ”ฆ") {
        
        self.id = UUID()
        self.title = title
        self.message = message
    }
}

/// ๋ธ”๋Ÿญ ์ฒ˜๋ฆฌ ๋ทฐ
struct BlockView: View {
    
    // ios ์‹œ์Šคํ…œ ์—์„œ๋Š” ์‹œ์Šคํ…œ ์ด๋ฒคํŠธ๋ฅผ ์•Œ๋ ค์ค€๋‹ค. - ๋…ธํ‹ฐํ”ผ์ผ€์ด์…˜ ์„ผํ„ฐ
    // ๋…ธํ‹ฐํ”ผ์ผ€์ด์…˜ -> publisher ๋กœ ๋ฐ›์„์ˆ˜ ์žˆ๋‹ค.
    // SwiftUI ์—์„œ๋Š” publisher ์ด๋ฒคํŠธ๋ฅผ onReceive ๋กœ ๋ฐ›๋Š”๋‹ค.
    
    @State private var alertData: AlertData?
    
    // ๋…นํ™”์ค‘ ์—ฌ๋ถ€
    @State var isRecordingScreen = false
    
    var body: some View {
        ZStack {
            if isRecordingScreen {
                Color.white
                Text("ํ™”๋ฉด ๋…นํ™”์ค‘์ž…๋‹ˆ๋‹ค! ๐ŸŽฅ")
                    .font(.largeTitle)
                    .padding()
            }
        }
        .onReceive(NotificationCenter.default.publisher(for: UIScreen.capturedDidChangeNotification), perform: { _ in
            print("UIScreen.main.isCaptured : \(UIScreen.main.isCaptured)")
            isRecordingScreen = UIScreen.main.isCaptured
            print(isRecordingScreen ? "๋…นํ™” ์‹œ์ž‘" : "๋…นํ™” ์ค‘์ง€")
        })
        .onReceive(NotificationCenter.default.publisher(for: UIApplication.userDidTakeScreenshotNotification), perform: { _ in
            
            print("์Šคํฌ๋ฆฐ์ƒท์ด ์ฐ์–ด์กŒ๋‹ค.")
            alertData = AlertData()
            
        })
        .edgesIgnoringSafeArea(.all)
        .alert(item: $alertData, content: { alertData in
            Alert(title: Text(alertData.title),
                  message: Text(alertData.message),
                  dismissButton: Alert.Button.cancel(Text("๋‹ซ๊ธฐ")))
        })
    }
}



struct MainView: View {
    var body: some View {
        VStack {
            Text("์•ˆ๋…•ํ•˜์„ธ์š”\n๊ฐœ๋ฐœํ•˜๋Š” ์ •๋Œ€๋ฆฌ ์ž…๋‹ˆ๋‹ค! ๐Ÿถ")
                .font(.largeTitle)
                .foregroundColor(.white)
                .padding()
        }
        .frame(minWidth: 0, maxWidth: .infinity,
                minHeight: 0, maxHeight: .infinity,
                alignment: .center)
        .background(Color.blue)
        .edgesIgnoringSafeArea(.all)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

 

 

 

(์ฐธ๊ณ )

https://green1229.tistory.com/176

 

SwiftUI - NotificationCenter

์•ˆ๋…•ํ•˜์„ธ์š”. ๊ทธ๋ฆฐ์ž…๋‹ˆ๋‹ค๐ŸŸข ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” SwiftUI์—์„œ NotificationCenter๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ํ•™์Šตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค๐Ÿง‘๐Ÿป‍๐Ÿ’ป UIKit์—์„œ๋Š” ์ž˜ ์•Œ๊ฒ ๋Š”๋ฐ ๋Œ€์ฒด SwiftUI์—์„œ๋Š” ์–ด๋–ป๊ฒŒ ๋…ธํ”ผํ‹ฐ์ผ€์ด์…˜ ์„ผ

green1229.tistory.com

 

'apple > ๐Ÿš SwiftUI & Combine' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[SwiftUI] @StateObject  (0) 2022.06.02
[SwiftUI] State and Data Flow  (0) 2022.06.02
[SwiftUI] ViewModifier  (0) 2022.06.02
[SwiftUI] lazyVGrid  (0) 2022.05.25
[SwiftUI] menu (feat. Picker)  (0) 2022.05.25
Comments