0

I am doing a project where i am creating a view with animation. While view is being created, i want to disable the touch in every view in view controller. so i did the following code.

private func addInScrollView(){
    self.view.userInteractionEnabled = false
    createCardView()
    self.view.userInteractionEnabled = true
}

But the touch is not being disabled. How to accomplished this task?

Sangharsha
  • 88
  • 11
  • See Ixt's answer of this stackoverflow [question][1] [1]: http://stackoverflow.com/questions/5404856/how-to-disable-touch-input-to-all-views-except-the-top-most-view – GaétanZ Aug 12 '15 at 10:20

4 Answers4

3

This is a good solution for your task.

self.view.subviews.map { $0.userInteractionEnabled = false }
Candost
  • 1,029
  • 1
  • 12
  • 28
  • how can i do this in swift? – Sangharsha Aug 12 '15 at 10:52
  • ew, upvotes on this? it definitely deserves a downvote instead, as it is wrong and incorrect pattern to mix _objects_ with _primitives_ randomly in `–makeObjectsPerformSelector:withObject:` method's parameters. – holex Aug 12 '15 at 11:33
  • @holex All subviews are UIView objects and makeObjectsPerformSelector:withObject: method is working fine. I didn't understand what you are saying actually. – Candost Aug 12 '15 at 11:42
  • probably because you don't know the difference between _primitives_ and _objects_, which is a kinda unfortunate situation, but the hint is that the `TRUE` __is not__ equal to `@(TRUE)` as the `NO` __is not__ equal to `@(NO)` either. I hope it helps, if not, please remove this incorrect snippet immediately. – holex Aug 12 '15 at 13:02
  • Of course I know they are not equal. But method handles these situations. BTW, I edited the question because question is for Swift. – Candost Aug 12 '15 at 13:50
  • 1
    it is much better, __by the way__ the method will not handle the situation at all (at least not how _you_ assume it handles!), it simple forwards the _pointer_ of the object as a _value_, which pointer (as number) can be equivalent to either `TRUE` or `FALSE`, because the _pointer_ actually depends on which memory address was allocated for the object in runtime. that is always undetermined. – holex Aug 12 '15 at 14:28
  • how do i remove this warning in above code? "Result of call to 'map' is unused" – Sptibo Jun 01 '22 at 14:17
1

You can create a new UIView on top of your existing view(obj-c code, but hopefully will give you an idea)

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
UIView *newView = [[UIView all]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
[self.view addSubview: newView];

When your animation is done, remove it

[newView removeFromSuperView];
Dovahkiin
  • 1,239
  • 11
  • 23
  • This is a much better way than cycling through all subviews and setting their `userInteractionEnabled` to `false`. At least this way, when you want to reinstate the interaction, all you need to do is remove the new view. – jowie May 14 '21 at 11:38
0

Hope this helps you:

Disable user interactions:

  UIApplication.sharedApplication().beginIgnoringInteractionEvents()

Enable user interactions:

   UIApplication.sharedApplication().endIgnoringInteractionEvents()
soumya
  • 3,801
  • 9
  • 35
  • 69
  • this didnot solve my problem. when i put code for disabling view, the view will be disabled. but when put both code, the view will be enabled before createcardview() function is finished. – Sangharsha Aug 12 '15 at 10:56
0

Try this code

 override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    let tCount = touches.count
    var touch : UITouch!
    touch = touches.first as? UITouch
    var touchLocation = touch.locationInView(self.view)
    if CGRectContainsPoint(yoursubview.frame, touchLocation)
    {
      println("view clicked")
        return
    }
Memon Irshad
  • 972
  • 1
  • 8
  • 17