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");
}
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];
}
I have a customized UIView
#interface EColumn : UIView
I have many instances of this EColumn in it's super view.
How can i detect when the finger hold and moves in the area of this UIView, and when it move out.
I don't mean a tap Gesture, i can detect tap Gesture by using this:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(taped:)];
[self addGestureRecognizer:tapGesture];
#implementation EColumn
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UIView *view = [self touchedViewWithTouches:touches andEvent:event];
NSLog(#"%#",view);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UIView *view = [self touchedViewWithTouches:touches andEvent:event];
NSLog(#"%#",view);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UIView *view = [self touchedViewWithTouches:touches andEvent:event];
NSLog(#"%#",view);
}
- (UIView *)touchedViewWithTouches:(NSSet *)touches andEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
UIView *touchedView;
for (UIView *view in self.subviews)
{
if(CGRectContainsPoint(view.frame, touchLocation))
{
touchedView = view;
break;
}
}
return touchedView;
}
#end
You can detect the finger hold for specific time with the UILongPressGestureRecognizer . For this, you can also specify minimumPressDuration and numberOfTouchesRequired
UILongPressGestureRecognizer *longPressRecognizer =
[[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(longPressDetected:)];
longPressRecognizer.minimumPressDuration = 3;
longPressRecognizer.numberOfTouchesRequired = 1;
[self addGestureRecognizer:longPressRecognizer];
For detecting moves, you can use UIPanGestureRecognizer
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[self addGestureRecognizer:panRecognizer];
As I have CustomView where i want to detect Touch Event like when user do Touch Down/Up and release or visa versa as in this Image.
What I tried
Swipe But in Which i Get only coordinate where user releasing the finger. like tap on X = 100 and releasing on 10 and m getting 10 only.
What i am looking for
Want to get whole coordinate like user tap on X = 100 and release on X = 80 then looking for on every single coordinate changed like 100,99,98,97,96,95,94.......80. equally as finger moving.
Please if anyone have any idea about it or something i forget to do.
Please Review.
Change this to your .h file
#interface ViewController : UIViewController{
CGFloat touchStartPoint;
CGFloat touchOffsetPoint;
CGFloat tempTouchOffsetPoint;
}
#end
And this to your .m file
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch=[touches anyObject];
touchStartPoint=[touch locationInView:self.view].y;
touchOffsetPoint = 0;
tempTouchOffsetPoint = 0;
// NSLog(#"touchStartPoint = %f",touchStartPoint);
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch=[touches anyObject];
touchOffsetPoint = [touch locationInView:self.view].y - touchStartPoint;
if (touchOffsetPoint>tempTouchOffsetPoint) {
NSLog(#"touch down");
}else{
NSLog(#"touch up");
}
tempTouchOffsetPoint = touchOffsetPoint;
}
Here is how I have done it:
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown;
[self.gestureAreaView addGestureRecognizer:swipeGesture];
[swipeGesture release];
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender
{
//Gesture detect - swipe up/down , can't be recognized direction
}
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeGesture];
[swipeGesture release];
UISwipeGestureRecognizer *swipeGesture2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGesture:)];
swipeGesture2.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeGesture2];
[swipeGesture2 release];
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender
{
//Gesture detect - swipe up/down , can be recognized direction
if(sender.direction == UISwipeGestureRecognizerDirectionUp)
{
}
else if(sender.direction == UISwipeGestureRecognizerDirectionDown)
{
}
}
Hope this helps out
Try this method
// declared somewhere
#property (nonatomic, retain) NSMutableArray *touchPositions;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch = [[event allTouches] anyObject];
CGPoint loc = [touch locationInView:self];
[self.touchPositions addObject:[NSValue valueWithCGPoint:loc]];
}
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
}