Disable background userinteraction - Cocos2d - ios

I have a custom CCNode that I am using for providing notifications on a popup basis. I am trying to disable the background when the CCNode is popped up but allow user interaction on the new CCNode.
I can disable the parent UIView by using the following when the popup is called
[[[CCDirector sharedDirector] view] setUserInteractionEnabled:NO];
In the class for my custom CCNode notification I have the button set to the following to try and override the previous code so that the button is the only touchable event
_buttonOK.userInteractionEnabled = TRUE;
However this is not working and the whole view's user interaction is disabled. How would I override this to enable only the _buttonOK to be touchable?

Thought it's not the prettiest way to do it, you can create a CCMenuItem with 100% width&height without any method. Everything under this CCMenuItem won't be clickable and you can add what you want as enabled on top of the CCMenuItem.
so... you would have something like:
Interaction Enabled Nodes > CCMenuItem without any method > Background without interaction.
Hope it helps :)

Related

How can you programmatically bring up keyboard with CCTextField?

I have a CCTextField that is hidden until the user taps on an object, which then adds it to the node tree and it's visible.
Currently, the user must then tap the CCTextField to activate the iOS keyboard and begin entering text.
Is there a way to programmatically initiate the keyboard appearing ?
becomeFirstResponder isn't working.
I dug a little deeper into the CCTextField class code, and cocos2D adds the UITextField property as a subview of the cocos2d view.
For some reason though, this didn't work :
[myCCTextField.textField becomeFirstResponder];
This is what I did, and it worked :
[[CCDirector sharedDirector] view]subviews[0] becomeFirstResponder];
If you have multiple CCTextFields you are obviously going to have to determine the index of your desired text field that you'd like to edit.

iOS - How To Make my view so that it doesn't respond to events

i have asked this question Changing UIView To be instance from UIControl Programmatically 24 minutes ago but i he didn't benefit me , so i typing this question in another form hoping that you can help me
i have a view that its class is from UIControl (so of course it will receive events)
and i have a button that i want it to change my view to normal UIView instance (so of course it will receive events)
so how to make that
i want the code if changing the view to UIView here
-(IBAction)pauseButton:(id)sender
{
//here
}
i want the code of changing the view to UIControl here
-(IBAction)playButton:(id)sender
{
}
Firstly make sure you have an IBOUTLET for that view or object and make sure it is connected.
For example we will say the view is called the_view
Now you can over uncheck the UserInteractionEnabled checkbox within the interface builders property tab of that object or type a line of code. The second option is better if you want the button to raise the event of stopping touches being allowed.
For the first option I have uploaded a video showing the basic steps:
Click Here For tutorial
For the second on option you can use this code:
the_view.userInteractionEnabled = NO;
Or if you are trying to hide your default view simply use:
self.view.userInteractionEnabled = NO;
or as noted in the answer above:
[self setUserInteractionEnabled:NO]
You can not change an object's class in objective-c. If you just want the control to stop receiving touches, you can use:
[self setUserInteractionEnabled:NO]

MBProgressHUD disabling interaction with UITableViewController

I'm using an MBProgressHUD inside of a UITableViewController. While I am able to get the HUD to display successfully, the HUD intercepts all of the screen's touch notifications and prevents scrolling in my UITableView.
I know that the intended functionality of MBProgressHUD might be to lock up the interface (say, during a blocking operation), but I want to know how to forward the touch events to the proper places regardless.
Any ideas?
The MBProgressHUD is a subclass of an UIView so you have to disable the user interaction with it. So:
youMBProgressHUDPointer.userInteractionEnabled=NO;
Figured it out. The following code needs to be added to MBProgressHUD's code:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
// UIView will be "transparent" for touch events if we return NO
return (NO);
}
Check my answer here. The MBProgressHUD blocks interactions at application level. You need to devise a way to get around that by putting some custom logic.

Using a UIView as a button

I'm trying to use a UIView I've created in Storyboard as a button. I assumed it would be possible to use a UIButton, setting the type to custom. However I was unable to add subviews to a custom UIButton in Storyboard.
As such I've just spent the last hour reinventing the wheel by making my own custom gesture recoginizers to reimplement button functionality.
Surely this isn't the best way of doing it though, so my question - to more experienced iOS developers than myself - is what is the best way to make a custom button?
To be clear it needs to:
Use the UIView I've created as it's hittable area.
Be able to show a
different state depending on whether is currently highlighted or not
(i.e. touch down).
Perform some action when actually tapped.
Thank you for your help.
You can use a UIButton, set the type to custom, and then programmatically add your subviews...
Change your UIView into a UIControl in the storyboard. Then use the method [controlViewName addTarget:self action:#selector(*click handler method*) forControlEvents:UIControlEventTouchDown];. click handler method is a placeholder for the method name of your handler. Use this method except change out the UIControlEventTouchDown for UIControlEventTouchInside and UIControlEventTouchDragExit to call a method when the user finishes their click and drags their finger out of the view respectively. I used this for something I'm working on now and it works great.
In Touch down you will want to: highlight all subviews
In Touch up inside you will want to: unhighlight all subviews and perform segue or do whatever the button is supposed to do
In Touch Drag Exit you will want to: unhighlight all subviews
See second answer by LiCheng in this similiar SO post.
Subclass UIControl instead. You can add subviews to it and it can respond to actions
Why are you implementing your own GestureRecognizer? I recommend using the UIView so you can add subviews in the interface builder and adding a UITapGestureRecognizer. You can even do this graphically since you don't care about IOS4 support.

Managing events on a custom UIControl

I am subclassing UIControl to compose a custom control that contains different standard controls.
For this discussion let's assume that my custom UIControl contains a UIButton only.
What I would like to achieve is that clicking anywhere in the custom UIControl generates a click event for that custom UIControl. The standard behavior is that the UIButton will process and consume (i.e. not forward) the click event.
As subclassing UIButton is discouraged, I can't really find a straightforward way of achieving this.
Any suggestions?
I came up with a simple solution that doesn't need subclassing of the UIButton.
In the action method defined for the UIButton's TouchUpInside control event, I have added the following line of code:
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
This results in the TouchUpInside control event being called, when clicking anywhere in the custom UIControl.
UIViews have a method called -hitTest:withEvent: that the event system uses to crawl the view hierarchy and dispatch events to subviews. If you want a parent view to gobble up all events that might otherwise be dispatched to its subviews, just override the parent's -hitTest:withEvent: with something like the following:
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
if(CGRectContainsPoint([self bounds], point)){
return self;
}
return [super hitTest:point withEvent:event];
}
UIButton is designed to process touch events. You can either set userInteractionEnabled to NO to have the button not accept any touches, or you can use addTarget:action:forControlEvents: on the button to have it call a method on your class when the button is touched.
BTW, where is subclassing UIButton discouraged?
Often I find useful user interaction related tasks in UIResponder class, which is the super class of UIControl - UIButton.
Read about – touchesBegan:withEvent:, – touchesMoved:withEvent:, – touchesEnded:withEvent:, – touchesCancelled:withEvent: in http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIResponder_Class/Reference/Reference.html You may find ways to customize user interaction for your UIButton. By the way, I don't think there will be any problem subclassing UIButton, no matter what you've heard, as long as your implementation is correctly added to the super class implementation, or does responsibly override it altogether.

Resources