Swift: how to disable user interaction while touch action is being carried out?

后端 未结 8 2445
执念已碎
执念已碎 2021-02-12 03:27

I\'m working with sprite kit and if the user touches the screen, the actions within

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
            


        
相关标签:
8条回答
  • 2021-02-12 04:05

    You can use boolean class variable to stop interaction while method is performing, and after that you can just change value of boolean, at the end of the method.

    Use UIApplication.shared.beginIgnoringInteractionEvents() at the end of the first method, then change value of boolean and then use another method with start line UIApplication.shared.endIgnoringInteractionEvents().

    0 讨论(0)
  • 2021-02-12 04:14

    For SwiftUI, expanding on @mojtaba-hosseini answer. You wanna make sure you fill your view with some color, except Clear color (you can always change opacity). I've also added blur to hide elements. Here's what worked for me:

    SwiftUI:

    ZStack{
        SomeView().blur(radius: 12)
        Rectangle()
            .fill(Color.white.opacity(0))
            .allowsHitTesting(false)
    }
    

    Another way to disable user interactions like scroll or button taps, but attach an action to user taps (for example a message to users that this feature is coming or behind a paywall):

    SwiftUI:

    VStack{
        SomeView().blur(radius: 12)
    }
    .contentShape(Rectangle())
    .onTapGesture {
        print("No access!")
    }
    
    0 讨论(0)
  • 2021-02-12 04:19

    - SwiftUI

    SomeView()
    .allowsHitTesting(false)
    
    0 讨论(0)
  • 2021-02-12 04:21

    In Swift 3.0 is:

    self.view.isUserInteractionEnabled = false
    
    0 讨论(0)
  • 2021-02-12 04:24

    If you want to disable a Buttons user's interaction, just do this when the screen loads.

    self.btnname.isUserInteractionEnabled = false
    

    If you want to disable the user interaction as a view, it will be same just remove the button name and add the view's name.

    If for some reasons, you want to enable interaction, just change "false" to "true".

    0 讨论(0)
  • 2021-02-12 04:25

    just go to storyboard and uncheck this option in any object which u want to disable the interaction of the user

    0 讨论(0)
提交回复
热议问题