ä¸å³ã®ããã« ç»åãé¸æ â ç»åã®åãåºã(ã¯ãããã³ã°) ã¨è¤æ°ã®ç»é¢ãé£ç¶ãã¦ã¢ã¼ãã«è¡¨ç¤ºããã¤ã¤é·ç§»ããããã
ã¢ã¼ãã«A â ã¢ã¼ãã«B ã¨é£ç¶ãã¦é·ç§»ã§ããããã¢ã¼ãã«Bããªããéãããªããªã£ã¦ãã¾ãã
å®è¡ç°å¢
- Xcode 11.6
- iOS 13.6
åé¡ã®æå
ã¢ã¼ãã«A â ã¢ã¼ãã«B ã¨é£ç¶ããããããããå®è£ ããã¨ã¢ã¼ãã«Bãéãããªããªã£ã¦ãã¾ãä¸å ·åãçºçããã
import SwiftUI struct SampleTwoModalView : View { enum SheetType { case imagePick case imageCrop } @State private var currentSheet: SheetType = .imagePick @State private var sheetIsPresented = false var body: some View { VStack { Button(action: { self.currentSheet = .imagePick self.sheetIsPresented = true }, label: { Text("select images & crop images") }) } .sheet(isPresented: $sheetIsPresented) { if (self.currentSheet == .imagePick) { ImagePickerView(sourceType: .photoLibrary, onCanceled: { // on cancel }) { (image) in //ç»åãé¸æããã¨ã¯ãããã³ã°ç»é¢ã¸é·ç§»ããã DispatchQueue.main.async { self.currentSheet = .imageCrop self.sheetIsPresented = true } } } else if (self.currentSheet == .imageCrop) { ImageCropView(originalImage: UIImage(), onCanceled: { // on cancel }) { (image) in // on success } } } } }
é£ç¶ãã¦ã¢ã¼ãã«è¡¨ç¤ºããããå ´åã«ã¯ã©ãããã°ããã®ãï¼
解決編
ã¢ã¼ãã«Aãéãããã¨ã1ç§å¾ ã£ã¦ããã¢ã¼ãã«Bã表示ãããããã¨ãã¢ã¼ãã«Bãéããããããã«ãªãã
import SwiftUI struct SampleTwoModalView : View { enum SheetType { case imagePick case imageCrop } @State private var currentSheet: SheetType = .imagePick @State private var sheetIsPresented = false @State private var image: UIImage? = nil var body: some View { VStack { Button(action: { self.currentSheet = .imagePick self.sheetIsPresented = true }, label: { Text("select images & crop images") }) } .sheet(isPresented: $sheetIsPresented) { if (self.currentSheet == .imagePick) { ImagePickerView(sourceType: .photoLibrary, onCanceled: { // on cancel }) { (image) in self.image = image //ç»åãé¸æããã¨ã¯ãããã³ã°ç»é¢ã¸é·ç§»ããã DispatchQueue.main.asyncAfter(wallDeadline: .now() + .milliseconds(1000)) { self.currentSheet = .imageCrop self.sheetIsPresented = true } } } else if (self.currentSheet == .imageCrop) { ImageCropView(originalImage: self.image!, onCanceled: { // on cancel }) { (image) in // on success } } } } }
ãã ããæ£ããæ£è§£ã¨ã¯æããªãã®ã§ãä»ã«å¯¾å¿æ¹æ³ãããã°æãã¦æ¬²ããã
(2020/07/29追è¨) 解決編ãã®2
dismissã¢ãã¡ã¼ã·ã§ã³ä¸ã«æ¬¡ã®ç»é¢ãã¢ã¼ãã«è¡¨ç¤ºããããã¨ã§ãSwiftUIå é¨ã®ã¹ãã¼ã¿ã¹ç®¡çãä¸æ£ãªç¶æ ã«ãªã£ã¦ãã¾ãã®ãåå ã§ã¯ãªããã¨èãã¦ãã¾ããä»å¾ã®SwiftUIã®ä¿®æ£ã«æå¾ ãããé¨åã§ããã¾ãSwiftUIå´ã§ã¯dismissãå®äºããã¿ã¤ãã³ã°ãåå¾ã§ããªããããdismissãçµäºãã¦ããã«æ¬¡ã®ç»é¢ã¸é·ç§»ã§ãã¾ããã
ç§ã®çµè«ã¨ãã¦ã¯ãXcode11.6æç¹ã§ã¯UIImagePickerControllerãªã©ã®UIKitã使ã£ã¦ãã以ä¸ãSwiftUIã§é£ç¶ãã¦ã¹ã ã¼ãºã«ã¢ã¼ãã«ç»é¢ã表示ããã®ã¯é£ããã®ã§ãUIKitå´ã§dismissã®ã¿ã¤ãã³ã°ãåããã§ããã
æ¬è³ªé¨åã§ã¯ãªãã®ã§ãããã§ã¯è©²å½é¨åã®ã¿ã転è¨ãããã¨æãã¾ããã¤ã¡ã¼ã¸ã¨ãã¦ã¯ä»¥ä¸ã®éãã§ãã
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) { guard let image = info[.originalImage] as? UIImage else { picker.dismiss(animated: true) { self.onImagePicked(nil) } return } // UIKitå´ã§éããã®ãå¾ ã£ã¦ããSwiftUIå´ã«ç»åã渡ã picker.dismiss(animated: true) { self.onImagePicked(image) } }
ã½ã¼ã¹ã³ã¼ãã¯ä¸è¨ã®ãªãã¸ããªã§åç §å¯è½ã§ãã
åèè¨äº
ã²ã¨ã¤ã®Viewããè¤æ°ã®ç»é¢ãã¢ã¼ãã«ã§è¡¨ç¤ºãããæ¹æ³ã¯ã以ä¸ã®è¨äºã«æ¸ããã