Skip to content

Commit d64b4bf

Browse files
committed
Added observer modifier and rotate modifier.
1 parent 6b4222f commit d64b4bf

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import SwiftUI
2+
3+
extension View {
4+
5+
public func onReceive(notification name: Notification.Name, perform action: @escaping (NotificationCenter.Publisher.Output) -> Void) -> some View {
6+
self.onReceive(NotificationCenter.default.publisher(for: name), perform: action)
7+
}
8+
9+
public func onRotate(perform action: @escaping (UIDeviceOrientation) -> Void) -> some View {
10+
self.modifier(DeviceRotationViewModifier(action: action))
11+
}
12+
}
13+
14+
struct DeviceRotationViewModifier: ViewModifier {
15+
16+
let action: (UIDeviceOrientation) -> Void
17+
18+
func body(content: Content) -> some View {
19+
content
20+
.onAppear()
21+
.onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
22+
action(UIDevice.current.orientation)
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)