I've created a UIButton and detect the touch event by:
[myButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
Everything works fine when I press exactly on the button.
My problem is that if I press with my finger part on the button and part outside - The event is not detected.
Is there another event I should use?
Am I doing something wrong here?
I'm sure people will say that you have to press exactly in the button but I'm trying to give the best user experience and resolve this issue.
Thanks!
You may want to enlarge your button, making perhaps a transparent area around your image.
Or maybe use UIControlEventTouchDown/UIControlEventTouchUpOutside instead of UIControlEventTouchUpInside ?
Related
So I'm writing my first tvOS App (Objective C) and am having some fun with the "Focus Engine". My app is a 2 page app with a Tab Bar controller, on the main page I have a few UIButtons. On app startup if I hide the tab bar, the buttons look like I am wanting them to with them all deselected, when I swipe down one of the UIButtons obviously gets focus, and I can swipe between my various buttons, and after a specified amount of inactivity time I want it to go back to them all being unfocussed.
I start (and reset) a NSTimer when each UIButton gets focus and my intention is to remove the UIButton focus after say 10 seconds (there is a good reason for this, and it makes sense in my app / ui).
I've tried issuing a "UIButton resignFirstResponder" I've also tried to move focus back to the hidden TabBar, I even tried "preferredFocusEnvironments" but I cannot get the button highlight to come away. I also tried cycling though the buttons setting them all to "userInteractionEnabled = NO" then back again but the button retains focus. I have log lines showing the timer starting and it triggering my un-focus method when it expires, but no matter what I put in there I can't seem to get the focus to disappear.
Any ideas on how to drop the focus from a UIButton, I think part of the problem is I don't want to move it to somewhere else. I want to remove all button focus which I guess is an unusual thing to do.
Thanks in advance.
Plasma
I discovered a way to do it, its a bit primitive but it works and achieves the desired effect.
When the idle timer expires I create a UIButton (Custom Type) at 0,0 that is 1px high and the width of the screen. I then tell the view it needs a focus update, and to update the focus. This takes the focus from any of the main buttons and up to my 1px high button along the top of the screen.
[focusButton removeFromSuperview];
CGRect frame = CGRectMake(0,0,(self.view.frame.size.width),1);
focusButton = [UIButton buttonWithType:UIButtonTypeCustom];
focusButton.frame = frame;
focusButton.tag = 99;
[self.view addSubview:focusButton];
[self.view setNeedsFocusUpdate];
[self.view updateFocusIfNeeded];
I then use 'didUpdateFocusInContext' to know when my 1px button has been given focus and set the button to disabled.
if(context.nextFocusedView.tag == 99){
NSLog(#"Focus Button Has Focus");
focusButton.enabled = NO;
}
This leaves the focus on that button which allows someone to swipe down to get to the main buttons, or up to get to the Tab Bar, once they swipe off it the 1px button is no longer selectable because is not enabled! I had to use Custom button type because system button type showed a white line over the top of the screen and tainted my labels.
Plasma
I'm kind of pulling my hair out here. I have a single window application with a NSscrollView and custom NSViews inside of the scroll view. The custom NSViews are registering mouseUP and mouseDown events but my problem is that when the app/window is inactive and you click on it anywhere to make it active the mouseUP and mouseDown events are being triggered in the NSView that you click on.
I overrode the '(BOOL)acceptsFirstMouse:(NSEvent *)theEvent' to return NO just to be sure (i know this is the default.
I can't figure out what I'm doing wrong. I'm principally an iOS developer so my OS X experience is not super extensive. Any input helps. Thanks!
Found the issue. I had a NSTexField on the subview that was capturing the first mouseDown event. Just overlooked it.
I am making an Iphone game, and I would like a button to stop being clickable when the character moves in front of it. Do you know how this can be done?
You need to identify button's frame whenever character enters. As soon as they hit the frame you need to do [button setEnabled:NO];
I created custom navigation buttons like this:
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"some.png"] forState:UIControlStateNormal];
....
[[current navigationItem] setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:button]];
Where current is UIViewController * type.
All works fine and button created, but it's clickable area outside the button, very close to the middle of the navigation bar. Is it possible to limit clickable area?
I believe thats a "feature" of iOS. THe Navigation Buttons on left and right are smaller than the minimum touch area allowed in iOS. As a result the actual hit zone is much larger than the physical NavigationButton. Including just under the bar, and like you're noticing to the left and right of the button. Its to allow quick touches without having "look" where you're touching. Its one of the key reasons iPhones are more natural to use than most android phones in the early days.
My best guess is that the button is set to center the image and not scale it, so the frame of the button is way too big.
button.frame=CGRectMake(x,y,w,h)
Set the frame to what you want the clickable area to be.
I have initially thought about subclassing the UIBarButtonItem and override -touchInside:.
This does not work though, since UIBarButtonItem is not a subclass of UIView.
What you are trying to achieve is therefore not possible without overriding some private API.
I have a scrollview in my app, in the scrollview there is a lot of buttons without any space between them... When I run the app, I can't scroll because I "of course" have to touch a button first, and ehen I try to scroll, the button that I touched gets highlighted, and when I release.. The button is pressed! So I can't reach the buttons that is further down in the scrollview!
All button has the setting "Touch up inside"..
I guess it is kinda easy to fix, but I don't know how! :P
/Noob
Setting the property canCancelContentTouches on the UIScrollView to YES should allow it to cancel the touch event already sent to the button once it detects a scroll. That should fix your problem.
You can do what ksoderstrom said or you can auto scroll using this method for your scrolview
[self.scrollView scrollRectToVisible:textField.frame animated:YES];
textField can be a UIButton as well. This method will just move the scrollView to whatever component you specify.
Hoep it helps.
I had the same problem and i found out that it's really hard to test it in the simulator. When the code run on the actual device, the scroll view was working as it was supposed to...