Actually i want to implement swipe left and right in UIScrollview. i have scrollview with content size (768,1500). i have tried this but problem is that sometimes its not detecting swipe and perform scrolling there. so now i want to disable scrolling on 2 finger touch.
swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(nextswipedScreen:)] autorelease];
swipeGesture.numberOfTouchesRequired=2;
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self addGestureRecognizer:swipeGesture];
swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(previousswipedScreen:)] autorelease];
swipeGesture.numberOfTouchesRequired=2;
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self addGestureRecognizer:swipeGesture];
i have tried custom scrollview for that but i have problem with touchesBegan method. its not calling every time. even i tried this but not able to stop two finger scroll in UIScrollview.
for (UIGestureRecognizer *mgestureRecognizer in _scrollView.gestureRecognizers) {
if ([mgestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
{
UIPanGestureRecognizer *mpanGR = (UIPanGestureRecognizer *) mgestureRecognizer;
mpanGR.minimumNumberOfTouches = 1;
mpanGR.maximumNumberOfTouches = 1;
}
}
Let me know if you have any solution or alternative for that.
I had the same problem; I needed to disable two-finger scrolling so that I could detect a two-finger swipe to the left or right. Here's what I did to set up my scroll view:
- (void) setUpGestureHandlersOnScrollView:(UIScrollView *)scrollView {
// set up a two-finger pan recognizer as a dummy to steal two-finger scrolls from the scroll view
// we initialize without a target or action because we don't want the two-finger pan to be handled
UIPanGestureRecognizer *twoFingerPan = [[UIPanGestureRecognizer alloc] init];
twoFingerPan.minimumNumberOfTouches = 2;
twoFingerPan.maximumNumberOfTouches = 2;
[scrollView addGestureRecognizer:twoFingerPan];
// set up the two-finger left and right swipe recognizers
UISwipeGestureRecognizer *twoFingerSwipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGestureFrom:)];
twoFingerSwipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
twoFingerSwipeLeft.numberOfTouchesRequired = 2;
[scrollView addGestureRecognizer:twoFingerSwipeLeft];
UISwipeGestureRecognizer *twoFingerSwipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGestureFrom:)];
twoFingerSwipeRight.direction = UISwipeGestureRecognizerDirectionRight;
twoFingerSwipeRight.numberOfTouchesRequired = 2;
[scrollView addGestureRecognizer:twoFingerSwipeRight];
// prevent the two-finger pan recognizer from stealing the two-finger swipe gestures
// this is essential for the swipe recognizers to work
[twoFingerPan requireGestureRecognizerToFail:twoFingerSwipeLeft];
[twoFingerPan requireGestureRecognizerToFail:twoFingerSwipeRight];
}
The handler method should look something like this:
- (void)handleGestureFrom:(UISwipeGestureRecognizer *)recognizer {
if ([recognizer numberOfTouches] == 2) {
// do whatever you need to do
}
}
You can create two gesture recognizers, one for single tap and one for double tap:
UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTouchesOne:)];
singleTapGesture.cancelsTouchesInView = NO;
singleTapGesture.delaysTouchesEnded = NO;
singleTapGesture.numberOfTouchesRequired = 1; // One finger single tap
singleTapGesture.numberOfTapsRequired = 1;
[Scroll_view addGestureRecognizer:singleTapGesture];
[singleTapGesture release];
UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTouchesTwo:)];
doubleTapGesture.cancelsTouchesInView = NO;
doubleTapGesture.delaysTouchesEnded = NO;
doubleTapGesture.numberOfTouchesRequired = 1; // One finger double tap
doubleTapGesture.numberOfTapsRequired = 2;
[Scroll_view addGestureRecognizer:doubleTapGesture];
[doubleTapGesture release];
And then, here comes the punch:
[singleTapGesture requireGestureRecognizerToFail : doubleTapGesture];
requireGestureRecognizerToFail Reference
The last line, makes your single tap handler work only if the double tap fails. So, you get both single tap and double tap in your application.
And So you can do like, in "doubleTapGesture" method you just specified the scrollview with content size (0,0), In "singleTapGesture" method you specified the scrollview with content size (768,1500).
Source knowledge
Related
I have a tutorial for my app, which should display only the first time the app is opened and should be tapped to dismiss.
I am initializing a UITapGestureRecognizer in my viewDidLoad:
tapper_tut = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
tapper_tut.cancelsTouchesInView = FALSE;
[self.view addGestureRecognizer:tapper_tut];
and I have an IBAction to detect the tap and set the tutorial to hidden:
- (IBAction)dismiss_tut{
if (????????????????) {
_tutorial.hidden = YES;
}
}
But I have no idea what to put in the if statement condition, or if this is even that right way to go about this.
How would I dismiss a UIImageView on a tap?
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
[self.view addGestureRecognizer:gr];
// if not using ARC, you should [gr release];
// mySensitiveRect coords are in the coordinate system of self.view
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
CGPoint p = [gestureRecognizer locationInView:self.view];
if (CGRectContainsPoint(mySensitiveRect, p)) {
NSLog(#"got a tap in the region i care about");
} else {
NSLog(#"got a tap, but not where i need it");
}
}
You can make viewDidLoad like this
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
/* Create the Tap Gesture Recognizer */
self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleTaps:)];
/* The number of fingers that must be on the screen */
self.tapGestureRecognizer.numberOfTouchesRequired = 1;
/* The total number of taps to be performed before the gesture is recognized */
self.tapGestureRecognizer.numberOfTapsRequired = 1;
/* Add this gesture recognizer to the view */
[self.view addGestureRecognizer:self.tapGestureRecognizer];
}
To detect the taps you can make the method like this.
- (void) handleTaps:(UITapGestureRecognizer*)paramSender
{
NSUInteger touchCounter = 0;
for (touchCounter = 0;touchCounter < paramSender.numberOfTouchesRequired;touchCounter++)
{
CGPoint touchPoint =[paramSender locationOfTouch:touchCounter inView:paramSender.view];
NSLog(#"Touch #%lu: %#",(unsigned long)touchCounter+1, NSStringFromCGPoint(touchPoint));
}
}
you have to declare .h file as "UIGestureRecognizerDelegate"
you have getting tap of gesture as two way as given below steps.
1) Call delegate method of GestureRecognizer (not given action )
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil]; // not given action.
recognizer.numberOfTouchesRequired=1;// here how many tap you want set it
[self.view addGestureRecognizer:recognizer];
recognizer.delegate = self;
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
//whatever you want write code here
return NO;
}
2) given action
UITapGestureRecognizer *oneTouch=[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(Addphoto)];
[oneTouch setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:oneTouch];
-(IBAction)Addphoto
{
//whatever you want write code here
}
may be it will help .
I think u need to detect the first time launch of the application which u can do with following
![[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunchedOnce"]
Put this in your if statement .
In viewDidLoad I set:
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[swipeGesture setDirection:(UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:swipeGesture];
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"Swipe received.");
UISwipeGestureRecognizerDirection temp = recognizer.direction;
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
{
[self backCalendarPressed:nil];
}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
{
[self nextCalendarPressed:nil];
}
}
but recognizer.direction is always equal to '3'.
And that's why I can't determine if it's left or right swipe.
you have to set a separate gesture recognizer for each direction if you want to discern between left and right gestures. the direction property only gives you what you set as allowed direction (3 = both directions).
You can give the same target method to both, and in that method ask for the direction of the recognizer as you are doing.
You should try this. It is working fine.
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(backCalendarPressed:)];
[swipeLeft setDirection: UISwipeGestureRecognizerDirectionLeft ];
[self.view addGestureRecognizer:swipeLeft];
swipeLeft=nil;
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(nextCalendarPressed:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipeRight];
Basically you cannot set multiple directions to a single Swipe gesture recognizer. Use separate gesture recognizers for Left and Right directions.
try this:
The direction property only defines the allowed directions that are recognized as swipes, not the actual direction of a particular swipe.
The easiest would be to use two separate gesture recognizers instead.
If you want to capture swipes left and right that you can differentiate between, you'll have to set up a separate recognizer for each. Apple does this in their Simple Gesture Recognizers
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFromLeft:)];
[swipeGestureLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:swipeGestureLeft];
UISwipeGestureRecognizer *swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFromRight:)];
[swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipeGestureRight];
-(void)handleSwipeFromLeft:(UISwipeGestureRecognizer *)recognizer {
[self backCalendarPressed:nil];
}
-(void)handleSwipeFromRight:(UISwipeGestureRecognizer *)recognizer {
[self nextCalendarPressed:nil];
}
You should implement all the UIGestureRecognizer states in this handler method. Thus you can handle all the cases. I am stating this because I suspect the gesture recognizer has not yet properly recognized it's gesture. Try looking for the direction in the UIGestureRecognizerStateChanged case.
I am new to iPhone developer,
I made epub reader and loaded each page of epub in my webview
What i want to is, when user does right swipe gesture 2nd time then i want to navigate to new page, i do not want to do anything when user does right swipe gesture for first time.
UISwipeGestureRecognizer *swipeRight
Is there any method something like,
if(swipeRight.touch.count > 2)
{
// do this
}
Any help will be appriciated.
Thanks In Advance !
EDIT
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
float scrollViewHeight = scrollView.frame.size.height;
float scrollContentSizeHeight = scrollView.contentSize.height;
float scrollOffset = scrollView.contentOffset.y;
if (scrollOffset == 0)
{
swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeftAction:)];
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
swipeUp.numberOfTouchesRequired=2;
swipeUp.delegate = (id<UIGestureRecognizerDelegate>)self;
swipeUp.cancelsTouchesInView=YES;
[_webview addGestureRecognizer:swipeUp];
}
else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight)
{
swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRightAction:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
swipeDown.numberOfTouchesRequired=2;
swipeDown.delegate = (id<UIGestureRecognizerDelegate>)self;
swipeDown.cancelsTouchesInView=YES;
[_webview addGestureRecognizer:swipeDown];
}
You can tell the UIWebView's UIScrollView that its UIPanGestureRecognizer should only fire when your own UISwipeGestureRecognizer has failed.
This is how you do it:
UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGesture:)];
UISwipeGestureRecognizer *leftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGesture:)];
rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:rightSwipeGesture];
[self.view addGestureRecognizer:leftSwipeGesture];
[_webView.scrollView.panGestureRecognizer requireGestureRecognizerToFail:rightSwipeGesture];
[_webView.scrollView.panGestureRecognizer requireGestureRecognizerToFail:leftSwipeGesture];
That should do the trick for you.
Just attach UIGestureRecognizer subclass to that view and hold on for calls...
UISwipeGestureRecognizer* rightSwipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(someAction)];
rightSwipeRecognizer.numberOfTouchesRequired = 2;
rightSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
rightSwipeRecognizer.cancelsTouchesInView = YES;
[self.webView addGestureRecognizer:rightSwipeRecognizer]; // add in your webviewrightSwipeRecognizer
Try like below it will help you
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
rightRecognizer.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
I don't think that swipe gestures offer support for the kind of behavior you are aiming at, but you can easily accomplish it by doing the following:
on the first swipe, set a flag and start a timer; for the rest do nothing;
on the second swipe,
a. if the timer has fired (when firing, the timer reset the flag), do as per point 1.
b. is the timer has not fired (the flag is still set), then do you action and cancel the timer.
You might event think of defining a subclass of UISwipeGestureRecognizer to encapsulate all this behavior.
I added this code in cellForRowAtIndexPath
UISwipeGestureRecognizer *gestureR = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSwipeFrom:)];
[gestureR setDirection:UISwipeGestureRecognizerDirectionRight];//|UISwipeGestureRecognizerDirectionRight)];
[cell addGestureRecognizer:gestureR];
it works fine. But I want UISwipeGestureRecognizerDirectionLeft so Added like this
[gestureR setDirection:UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight)];
When I check with direction and state I am always getting 3 = 3
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"%d = %d",recognizer.direction,recognizer.state);
}
if I apply only one Gesture it works fine. I tried to add two gestures one by one. but it will responding for only one gesture.
How to add second gestures. I added directly to one gesture to TableView another one to cell but now use.
Try this
UISwipeGestureRecognizer* gestureR;
gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom)] autorelease];
gestureR.direction = UISwipeGestureRecognizerDirectionLeft;
[view addGestureRecognizer:gestureR];
gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom)] autorelease];
gestureR.direction = UISwipeGestureRecognizerDirectionRight; // default
[view addGestureRecognizer:gestureR];
If you want to handle different functionalities on left and right swipes, just change the selectors.
Instead of two times alloc, it would be better if you use
UISwipeGestureRecognizer* recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionLeft+UISwipeGestureRecognizerDirectionRight];
[cell addGestureRecognizer:recognizer];
And get the direction of swipe in the action as :
-(void)handleSwipe:(UISwipeGestureRecognizer *) sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
{
//do something
}
else //if (sender.direction == UISwipeGestureRecognizerDirectionRight)
{
//do something
}
}
I know its been ages since you asked this.
But try and read following line again in your question.
[gestureR setDirection:UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionRight)];
Did you realise you added UISwipeGestureRecognizerDirectionRight. Twice!!
:D
In the UIView, there is several sub views of UIImageView objects.
Since I need to detect the touch events from the UIImageView and from the remaining area of the UIView.
Can I implement touch events in UIView and UIImagView simultaneously ?
Thanks.
Based on your comment, I think you should consider gesture recognizers. Use the UITapGestureRecognizer to identify and respond to taps on a view.
UITapGestureRecognizer *tapGesture;
tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapOnView:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[view addGesture:tapGesture];
[tapGesture release];
...
tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapOnImage:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[imageView addGesture:tapGesture];
[tapGesture release];
...
define -(void)handleTapOnView:(UITapGestureRecognizer*)gesture and -(void)handleTapOnImage:(UITapGestureRecognizer*)gestureand handle the touches there.
There are other kinds of gestures too. You can read more about them here.