How to stop the UITextField from responding to the shake gesture? - iphone

By default, if you shake the iPhone while entering text in a UITextField, a little box will appear asking if you want to undo typing. How can I prevent this from happening? I looked through the UITextField, UITextFieldDelegate, and UITextInputTraits docs and found nothing relating to shaking.
Presumably I could subclass UITextField to ignore the shake event, but I'm not exactly confident in my ability to not screw other things up. I'd love something like
textField.respondsToShake = FALSE;
What's the best way to do this?

[UIApplication sharedApplication].applicationSupportsShakeToEdit = NO;

You could try to subclass UITextField and override canPerformAction:withSender: in such a way that it will disallow the undo: action. But then you lose undo both by shaking and through the context menu.

Related

Stop automatic dismissal of keyboard on iOS/disablesAutomaticKeyboardDismissal not called

I would like to be able to have another control become the first responder yet keep the keyboard showing to the user.
I found disablesAutomaticKeyboardDismissal in UIViewController and overrode it but it never get's called (iOS 5.0)
Is there a reason this method would not get called? Is there another way to keep the keyboard showing even though it's not required by the first responder?
Though it felt a bit like a hack what I did was make override canBecomeFirstResponder on UIView to return YES and then implement the UIKeyProtocol
https://developer.apple.com/library/IOS/documentation/UIKit/Reference/UIKeyInput_Protocol/Reference/Reference.html
But did nothing on key press. This way the keyboard stays open but the keys don't matter.

Detecting a finger being held on an object

I am trying to have an image that when the user touches it, it wiggles and as soon as the user lifts their finger it stops.
Is there a gesture that I can use to detect when the finger is down, not just on the initial touch, or when the user moves there finger?
I have tried a LongPress gesture, but that does not get called the entire time the finger is on the view. Can anyone help me with the best way to active this. Right now i am doing it using touchesBegin, touchesMoved, touchesEnd, but i was wondering if there is a better way.
Any suggestions are greatly appreciated.
Thanks
EDIT
Based on the comments, I slightly misunderstood the original question, so I edit my answer to a different solution, which hopefully is a bit more clear (and answers the actual question - not the one that was in my head).
A LongPress gesture is continuous (where a tap gesture is not). That means, the recognizer callback will continue to be invoked until the gesture is complete - which does not happen until the "longpress" is released. So, the following should do what you want. NOTE: I think you want to "start shaking" a view when the long-press is recognized, then "stop shaking" the view when the fingers are released. I just pretended you have functions for that. Substitute appropriately.
- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
StartShakingView(gestureRecognizer.view);
} else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
StopShakingView(gestureRecognizer.view);
}
}
The Apple Touches sample includes code that demonstrate using both UIResponder and UIGestureRecognizer methods.
Either should work for what you're doing.
Simple answer - you could make the image a UIButton, and start the wiggle on TouchDown, and stop it on TouchUpInside or TouchUpOutside
It sounds like you want to subclass UIGestureRecognizer, which, as I recall, gets the touchesBegan:... and associated methods. Read the notes on subclassing in the UIGestureRecognizer reference. Or use a UIButton as SomaMan suggests.

how do I know when UIKeyboard Join button is tapped?

How do I know when a user taps on the Join button on the UIKeyboard?
Also can this text be changed? I know there are several built options but I nee done that says 'Login' if I can.
I'm not a big fan of using UITextField delegate just for knowing when keyboard entry is completed by pressing the "return" on the keyboard (whatever it's labeled). UITextField actually works well with a Target/Action pattern which is much simpler and far more direct than using the delegate for this.
I've pointed this out before in an answer to "How to call a method when the Done Button in the KeyBoard is Clicked?". Please take a look at that for the details of using the traditional target/action pattern to know when keyboard entry is complete. Unfortunately, Apple's documentation isn't as clear is it could be on this subject.
UITextField delegate has its place, but if all you need is to know when entry is complete then using the delegate likely isn't the best approach. Use target/action.
For the UIReturnKeyType choices, see the returnKeyType property in the UITextInputTraits Protocol Reference.
You should set your delegate of your UITextField (I'm assuming that's a UITextField) and check for that delegate call :
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Also, you return a BOOL to tell the UITextField to do its default behavior or not. You can check the documentation here.
Also, the options Apple gives you for the UIReturnKeyType are your only options.

How to pull up a UIKeyboard without a UITextField or UITextView?

I'm currently developing an OpenGL ES game for the iPhone and iPod touch.
I was wondering how I can easily pull up the UIKeyboard?
Is there an official, documented possibility to pull up a UIKeyboard without using a UITextField of UITextView?
If you subclass UIResponder, and declare the UIKeyInput protocol, the keyboard will appear when you become the firstResponder.
See the UIKeyInput protocol here.
One thing that tripped me up is that you'll need to override the canBecomeFirstResponder message.
It's not "officially" possible - you can't access the UIKeyboard object at all while staying within Apple's guidelines.
Creating an invisible UITextField, then calling [textField becomeFirstResponder] would do the job - you could even subclass UITextField first, then override the delegate method textField:shouldChangeCharactersInRange: to redirect the text input to where you want it to go.
It is indeed possible. I had myself struggled a lot with this. UIKeyInput protocol can be used to pull a keyboard without using a UITextField or UITextView. However it is only available in iOS 3.2 and above. Here is a tutorial on that.
Hope that helps.
I displayed the keyboard without any visible UITextField by positioning my textfield's frame out of the visible screen:
#define TEXT_FRAME -50,-50,0,0
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(TEXT_FRAME)];
When I set the "first responder" the keyboard becomes visible without any visible input area:
// show the keyboard
[self.textField becomeFirstResponder];
I later dropped the idea. However, I don't know its conformance to the Apple guidelines.
I haven't tried it but http://www.cocoadev.com/index.pl?UIKeyboard shows some code that looks like it should work.

Disable Magnifying Glass in UITextField

Is there a way to prevent the user from moving the cursor in a UITextField? I'd like it to stay at the end of the string.
This is an old question, but I was looking for an answer to the same question, and found a solution.
It is actually quite simple to prevent the user from moving the cursor. Just subclass UITextField and provide the following implementation of caretRectForPosition:
- (CGRect)caretRectForPosition:(UITextPosition *)position
{
return [super caretRectForPosition:self.endOfDocument];
}
NO SUBCLASS needed.
You Could use UITextFieldDelegate. It will make disable magnifying glass & text selection.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
textField.userInteractionEnabled = NO;
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
textField.userInteractionEnabled = YES;
}
NB: This is just a bypass.
There's no way to prevent them from moving the cursor. You can, however, prevent them from editing the text except at the end by implementing the
– textField:shouldChangeCharactersInRange:replacementString:
method in your text field's delegate.
Edit: you can also set userInteractionEnabled to NO so that the user can't tap the field. Call becomeFirstResponder manually so that the field gets focus since the user can't tap to focus.
I'd suggest to check the gestureRecognizers property.
You will find a lot of them in the array and might want to either remove them all or to find the ones that triggers the event you want to intercept and remove/replace it.
I used it to remove copy/paste and magnifying glass functionalities from an UITextField
I haven't check if you can disable the magnifying glass, but the recommended way to selectively disable editing behavior in a UIResponder (and thus a UITextField) is to implement canPerformAction:withSender of UIResponder.
See UIResponder documentation.
Maybe if you return "NO" for the select and selectAll action you can disable it.
Another, more brutal, way is to intercept any touch event and reset the cursor to the end.