I'm making a tweak that involves popping an alert whenever the user double taps an icon in editing mode. I've tried hooking on to
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
of SBIcon and then
{
%orig;
UITouch *touch = [touches anyObject];
if (touch.tapCount == 2 && [[objc_getClass("SBIconController") sharedInstance] isEditing])
{
//pop an alert and do stuff
}
}
But this doesn't seem to work at all. Could anyone tell me what is wrong with the above and suggest alternative ways to achieve this?
EDIT: I'm using theos, if it matters.
I will suggest you to use Tap gesture recognizer.It is more specific and works very accurately.
For more info you can check out this link:
http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizers/GestureRecognizers.html
Alternative ways for you
If your icon is a button then you can easily detect the Double tab by adding UIControlEventTouchDownRepeat event
[yourIconButton addTarget:self action:#selector(multipleTap:withEvent:)
forControlEvents:UIControlEventTouchDownRepeat];
-(IBAction)multipleTap:(id)sender withEvent:(UIEvent*)event
{
UITouch* touch = [[event allTouches] anyObject];
if (touch.tapCount == 2) {
// Do all your actions here
}
}
If you are considering it for a whole view then use UITapGesture
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction:)];
[tap setNumberOfTapsRequired:2];
[yourIconView addGestureRecognizer:tap];
- (void)tapAction:(UIGestureRecognizer *)gestureRecognizer
{
//Do your action
}
Related
when i double tap on image then the image will show in complete view and at the same time i have to drag the image from one place to another in the same view.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
Using the above methods the drag and drop of image is implemented successfully but the UITapGesture is not working now. So, how can I implement both ? ``
By just using UILongPressGrstureRecognizer, it is surprising that it is able to implement tap and drag.
As you just drag first Action will be Tap by default.
You have to:
set the numberOfTapsRequired to 1 to detect the initial tap.
set the minimumDuration something smaller to detect drags quicker without waiting
e.g.:
UILongPressGestureRecognizer *mouseDrag = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleDrag:)];
mouseDrag.numberOfTapsRequired=1;
mouseDrag.minimumPressDuration=0.05;
[clickLeft requireGestureRecognizerToFail:mouseDrag];
to handle the drag, you must determine the state to handle it appropriately as a continuous gesture.
For tap gesture you can simply use
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapFrom:)];
[self.imageView addGestureRecognizer:tapGestureRecognizer];
tapGestureRecognizer.delegate = self;
While you tap on image this method invoke
- (void) handleTapFrom: (UITapGestureRecognizer *)recognizer
{
//Code to handle the gesture
}
Did you try to add the simultaneous recognition?
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
For Single Tap in viewDidLoad method,first set the tapGesture Recognizer
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap)];
singleTap.numberOfTapsRequired = 1;
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:singleTap];
Then method
-(void)handleSingleTap
{
NSLog(#"The single tap happened");
}
Touch Events
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if (touch.tapCount == 1) {
NSLog(#"The single tap happened now");
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
NSLog(#"The tap count is - %lu",(unsigned long)touch.tapCount);
if (touch.tapCount == 1)
{
NSLog(#"The single tap Ended now");
}
}
Hello I'm trying to have two of my UITextFields' keyboards hide when the user clicks off of the keyboard. I need both of my textfields to have the same method. How can I do this without duplicating them and giving Xcode and error?
Thanks!
Here's the method I'm using for my first textfield I need the same for my other.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([text1 isFirstResponder] && [touch view] != text1) {
[text1 resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
Apple strongly recommends to use gesture recognizers, not touchesBegan and other "touches" methods if you do not really have to use them.
Try this code. It will work for any numbers of text views inside view controller.
- (void)viewDidLoad
{
[super viewDidLoad];
UIGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(onBackgroundTapped)];
[self.view addGestureRecognizer:tapRecognizer];
... another initialization code
}
- (void)onBackgroundTapped
{
[self.view endEditing:YES];
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Dismiss keyboard on touch anywhere outside UITextField
I am using UIScrollView in my app. I am putting few text fields and button. I want to hide my keyboard when they touch outside of the textbox. (I mean they will be taping on the uiscrollview). I have tried a lot of things... I use UIGestureRecognizer but its not working... help please
Here's the solution just tried and it works.. Hope helps
Add the following code to your viewDidLoad;
-(void)viewDidLoad {
//create a tapGesture which calls a removeKeyboard method
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(removeKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.yourScrollView addGestureRecognizer:tapGesture];
}
-(void)removeKeyboard {
[self.yourTextField resignFirstResponder];
}
Hope helps....
Try the following code in your view...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([yourTextView isFirstResponder] && [touch view] != yourTextView) {
[yourTextView resignFirstResponder];
}
}
In my view I use three methods (touchbegan, touchmoved and touchended), because I color in a view;
my problem is that I want to add in this view a UITapGestureRecognizer with 2 taps;
is it possible? or touchbegan don't allow this gesture?
Ya its possible
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
if([touch tapCount] == 2){
NSLog("2 taps");
}
}
May this will help u
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected)];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
[doubleTap release];
-(void)tapDetected{
NSLog(#"Double Tap");
}
I'm currently working on a project in iOS where I want the UItouch command to only send co-ordinates out when it's over a certain "image". I've got the co-ordinates outputting using UItouch but am not able to do this for just one specific area.
To my knowledge, the only way to do this is with views. So I've made a new view within my mainview, and from there I have problems and can't seem to get it working.
Has anyone done this before/can give me any advice on this?
PS - I'm using Xcode 4.
Thanks in advance.
Try this out
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch.view==yourImageView) {
//Coordinate code
}
}
EDIT: Or try using the UITapGestureRecognizer
In your interface add the UIGestureRecognizerDelegate
#interface ViewController : UIViewController <UIGestureRecognizerDelegate> {
then in your viewDidLoad add this
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapMethod)];
tapped.delegate=self;
tapped.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapped];
then in your viewController add these 2 methods
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (touch.view==yourImageView) {
return YES;
}
return NO;
}
-(void)tapMethod {
//Coordinate code
}
And make sure you have [yourImageView setUserInteractionEnabled:YES];