I am new to iPhone developer,
I made epub reader and loaded each page of epub in my webview, when i see my application in portrait mode scrolling is not there, bcoz i have used,
webview.scalesPageToFit=TRUE;
But when i see my application in landscape mode page fits properly but it occurs with scrolling, so i want to add swiperight gesture when scrolling end is reached so that user can navigate to a next page, on doing rightswipe.
is there any method which tells webview has completed scrolling ?
_ in short how to detect UIWebView reaching the top or bottom ?_
Thanks In Advance !
EDIT
written in did load:
swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRightAction:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
swipeRight.delegate = (id<UIGestureRecognizerDelegate>)self;
[_webview addGestureRecognizer:swipeRight];
swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeftAction:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.delegate = (id<UIGestureRecognizerDelegate>)self;
[_webview addGestureRecognizer:swipeLeft];
and then while loading each page i am checking orientation if it is portrait then adding two more gesture up and down gesture if landscape then removing up and down gesture.
if([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortraitUpsideDown) {
swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeftAction:)];
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
swipeUp.delegate = (id<UIGestureRecognizerDelegate>)self;
[_webview addGestureRecognizer:swipeUp];
swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRightAction:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
swipeDown.delegate = (id<UIGestureRecognizerDelegate>)self;
[_webview addGestureRecognizer:swipeDown];
counterPort++;
counterLand=0;
}
}
if (counterLand==0) {
if([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationLandscapeRight) {
[_webview removeGestureRecognizer:swipeDown];
[swipeDown release];
swipeDown=nil;
[_webview removeGestureRecognizer:swipeUp];
[swipeUp release];
swipeUp=nil;
counterPort=0;
counterLand++;
}
after your suggestion:
-(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.delegate = (id<UIGestureRecognizerDelegate>)self;
[_webview addGestureRecognizer:swipeUp];
}
else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight)
{
swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRightAction:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
swipeDown.delegate = (id<UIGestureRecognizerDelegate>)self;
[_webview addGestureRecognizer:swipeDown];
}
}
In ios 5.0 and later, you can access the UIWebView's scrollView property. If you set that scrollView's delegate to your object, you can find out when it reaches the top or bottom. Just make sure you hold a reference to the previous scrollview delegate before changing.
_defaultDelegate=[webview.scrollView.delegate retain];
webview.scrollView.delegate=myController;
Your _defaultDelegate declaration will be
id <UIScrollViewDelegate> _defaultDelegate;
Now, in myController's implementation of scrollViewDidScroll:, you can find out if the scrolling has reached the end or beginning of the page.
The tricky thing is that you must implement other UIScrollViewDelegate Methods and pass on to the default delegate so that the webview does not lose its default behaviour. So, you implement like so
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
[_defaultDelegate scrollViewDidScrollToTop:scrollView];
}
Refer to this answer for finding out if scrollView scrolls to top or bottom:
iPhone - knowing if a UIScrollView reached the top or bottom
EDIT: After reading your code, I don't think you should be adding and removing your gesture recognizers like that. Create 4 gesture recognizers which covers all the possible swipe directions and add it to the webview in viewDidLoad. You can control if the gesture recognizer should respond by implementing the method :
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
Don't forget to set the delegate for each gesture recognizer and also checkout the other UIGestureRecognizerDelegate Methods. The view Controller has a property interfaceOrientation that you can access also.
To solve the scrolling issue, try having a BOOL iVar to indicate if you are at the bottom in the code.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
float scrollViewHeight = scrollView.frame.size.height;
float scrollContentSizeHeight = scrollView.contentSize.height;
float scrollOffset = scrollView.contentOffset.y;
_atBottom=NO;
if (scrollOffset == 0)
{
swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeftAction:)];
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
swipeUp.delegate = (id<UIGestureRecognizerDelegate>)self;
[_webview addGestureRecognizer:swipeUp];
}
else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight)
{
swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRightAction:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
swipeDown.delegate = (id<UIGestureRecognizerDelegate>)self;
[_webview addGestureRecognizer:swipeDown];
_atBottom=YES;
}
}
and implement the UIGestureRecognizerDelegate method
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return _atBottom;
}
Related
I am trying to add 2 UITapGestureRecognizers to a view, one for single tap and one for double tap events. The single tap recognizer is working as expected (on its own). But I don't seem to be able to get the double tap recognizer working.
Have tried to experiment with properties like : cancelsTouchesInView, delaysTouchesBegan and delaysTouchesEnded but still doesn't work.
When I double tap, the single tap recognizer would always be activated and the double tap event would also be sent to the super view. But the custom double tap recognizer does not seem to be notified at all.
Documentations seem to suggest that the 3 properties mentioned above could be used for the purpose. But I am just not sure what values should be set and on which recognizer(s) (single, double or both). Hope somebody familiar with this could help.
The following is the latest updated code block.
// ****** gesture recognizers ******
- (void)addSingleAndDoubleTapGestureRecognizersToView:(UIView *)view
{
// single tap
UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: tableViewController action: #selector(handleSingleTapOnView:)];
[singleTapRecognizer setNumberOfTouchesRequired:1];
[view addGestureRecognizer: singleTapRecognizer];
// double tap
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: tableViewController action: #selector (handleDoubleTapOnView:)];
[doubleTapRecognizer setNumberOfTouchesRequired:2];
[singleTapRecognizer requireGestureRecognizerToFail: doubleTapRecognizer];
[view addGestureRecognizer: doubleTapRecognizer];
}
- (void)handleSingleTapOnView:(id)sender
{
}
- (void)handleDoubleTapOnView:(id)sender
{
}
UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doSingleTap)] autorelease];
singleTap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTap];
UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doDoubleTap)] autorelease];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
Note: If you are using numberOfTouchesRequired it has to be .numberOfTouchesRequired = 1;
For Swift
let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didPressPartButton))
singleTapGesture.numberOfTapsRequired = 1
view.addGestureRecognizer(singleTapGesture)
let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didDoubleTap))
doubleTapGesture.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTapGesture)
singleTapGesture.require(toFail: doubleTapGesture)
Swift 3 solution:
let singleTap = UITapGestureRecognizer(target: self, action:#selector(self.singleTapAction(_:)))
singleTap.numberOfTapsRequired = 1
view.addGestureRecognizer(singleTap)
let doubleTap = UITapGestureRecognizer(target: self, action:#selector(self.doubleTapAction(_:)))
doubleTap.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTap)
singleTap.require(toFail: doubleTap)
In the code line singleTap.require(toFail: doubleTap) we are forcing the single tap to wait and ensure that the tap event is not a double tap.
You need to use the requireGestureRecognizerToFail: method. Something like this:
[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
//----firstly you have to alloc the double and single tap gesture-------//
UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : #selector (handleDoubleTap:)];
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : #selector (handleSingleTap:)];
[singleTap requireGestureRecognizerToFail : doubleTap];
[doubleTap setDelaysTouchesBegan : YES];
[singleTap setDelaysTouchesBegan : YES];
//-----------------------number of tap----------------//
[doubleTap setNumberOfTapsRequired : 2];
[singleTap setNumberOfTapsRequired : 1];
//------- add double tap and single tap gesture on the view or button--------//
[self.view addGestureRecognizer : doubleTap];
[self.view addGestureRecognizer : singleTap];
Not sure if that's exactly what are you looking for, but I did single/double taps without gesture recognizers. I'm using it in a UITableView, so I used that code in the didSelectRowAtIndexPath method
tapCount++;
switch (tapCount)
{
case 1: //single tap
[self performSelector:#selector(singleTap:) withObject: indexPath afterDelay: 0.2];
break;
case 2: //double tap
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(singleTap:) object:indexPath];
[self performSelector:#selector(doubleTap:) withObject: indexPath];
break;
default:
break;
}
if (tapCount>2) tapCount=0;
Methods singleTap and doubleTap are just void with NSIndexPath as a parameter:
- (void)singleTap:(NSIndexPath *)indexPath {
//do your stuff for a single tap
}
- (void)doubleTap:(NSIndexPath *)indexPath {
//do your stuff for a double tap
}
Hope it helps
Solution for Swift 2:
let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap))
singleTapGesture.numberOfTapsRequired = 1 // Optional for single tap
view.addGestureRecognizer(singleTapGesture)
let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
doubleTapGesture.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTapGesture)
singleTapGesture.requireGestureRecognizerToFail(doubleTapGesture)
I implemented UIGestureRecognizerDelegate methods to detect both singleTap and doubleTap.
Just do this .
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleDoubleTapGesture:)];
[doubleTap setDelegate:self];
doubleTap.numberOfTapsRequired = 2;
[self.headerView addGestureRecognizer:doubleTap];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleSingleTapGesture:)];
singleTap.numberOfTapsRequired = 1;
[singleTap setDelegate:self];
[doubleTap setDelaysTouchesBegan:YES];
[singleTap setDelaysTouchesBegan:YES];
[singleTap requireGestureRecognizerToFail:doubleTap];
[self.headerView addGestureRecognizer:singleTap];
Then implement these delegate methods.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
Some view have there own double tap recognizers built in (MKMapView being an example). To get around this you will need to implement UIGestureRecognizerDelegate method shouldRecognizeSimultaneouslyWithGestureRecognizer and return YES:
First implement your double and single recognizers:
// setup gesture recognizers
UITapGestureRecognizer* singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(mapViewTapped:)];
singleTapRecognizer.delegate = self;
singleTapRecognizer.numberOfTapsRequired = 1;
UITapGestureRecognizer* doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(mapViewDoubleTapped:)];
doubleTapRecognizer.delegate = self; // this allows
doubleTapRecognizer.numberOfTapsRequired = 2;
[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
And then implement:
#pragma mark UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer
*)otherGestureRecognizer { return YES; }
In reference to #stanley's comment -
Don't try and use tap gestures to row selections to work in UITableView as it already has full tap handling....
But you must set 'Cancels Touches in View' to 'NO' on your single tap gesture recognizers or it will never get the tap events.
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 .
For example I have a view where I want two different gestures:
tap to do action A.
double tap to do action B.
The problem is with UITapGestureRecognizer I can only set minimum required tap count. The single tap gesture recognizer recognizes a tap before the double tap gesture recognizer recognizes the double tap.
Example:
_tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGestureRecognized:)];
_tapGestureRecognizer.numberOfTouchesRequired = 1;
_tapGestureRecognizer.numberOfTapsRequired = 1;
[self addGestureRecognizer:_tapGestureRecognizer];
_doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTapGestureRecognized:)];
_doubleTapGestureRecognizer.numberOfTouchesRequired = 1;
_doubleTapGestureRecognizer.numberOfTapsRequired = 2;
[self addGestureRecognizer:_doubleTapGestureRecognizer];
It always recognizes the single tap even if I do double tap very fast. How can I set it up so the tap gesture recognizer waits and sees if the double tap gesture recognizer recognizes?
Here is what I have used in one of my old projects, I hope it helps you out man.
UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget: self action:#selector(doSingleTap)] autorelease];
singleTap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTap];
UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget: self action:#selector(doDoubleTap)] autorelease];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
On swift 3 and swift 4:
let singleTap = UITapGestureRecognizer(target: self, action: #selector(doSingleTap))
singleTap.numberOfTapsRequired = 1
self.view.addGestureRecognizer(singleTap)
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(doDoubleTap))
doubleTap.numberOfTapsRequired = 2
self.view.addGestureRecognizer(doubleTap)
singleTap.require(toFail: doubleTap)
{ UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleDoubleTapGesture:)];
[doubleTap setDelegate:self];
doubleTap.numberOfTapsRequired = 2;
[self.headerView addGestureRecognizer:doubleTap];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleSingleTapGesture:)];
singleTap.numberOfTapsRequired = 1;
[singleTap setDelegate:self];
[doubleTap setDelaysTouchesBegan:YES];
[singleTap setDelaysTouchesBegan:YES];
[self.headerView addGestureRecognizer:singleTap];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
Create the gesture recognizers inside any method you want. For example if you want tap gestures for your headerView in tableView create those tap gestures inside
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
And your class should implement UIGestureRecognizerDelegate.
Above mentioned are the 2 methods of that delegate class.
Another option is to use UIGestureRecognizerDelegate, especially if you do not have easy access to the other recognizer.
let singleTap = UITapGestureRecognizer(target: self, action: #selector(doSingleTap))
singleTap.numberOfTapsRequired = 1
singleTap.delegate = self
self.view.addGestureRecognizer(singleTap)
Implement the delegate:
extension ViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if let tapGesture = gestureRecognizer as? UITapGestureRecognizer, let otherTapGesture = otherGestureRecognizer as? UITapGestureRecognizer {
return tapGesture.numberOfTapsRequired == 1 && otherTapGesture.numberOfTapsRequired == 2
}
return false
}
}
I found a solution to the delay of the single tap using XCode Monkey's answer. Using this approach, you can configure in mili-seconds the threshold between a single and a double tap. This technique uses a simple UIButton and a NSTimer to differentiate between single and double tap.
#property NSTimer *tapTimer;
#property BOOL isDoubleTap;
UIButton *yourButton = [UIButton new];
yourButton.frame = // set your frame
[yourButton addTarget:self action:#selector(handleTap:) forControlEvents:UIControlEventTouchUpInside];
[self.view yourButton];
-(void)handleTap:(UIButton*)button
{
[self.tapTimer invalidate];
if (self.isDoubleTap)
{
[self doDoubleTap];
return;
}
self.isDoubleTap = YES;
// CHANGE THE 0.3 SECOND THRESHOLD BETWEEN SINGLE/DOUBLE TAP TO YOUR HEART'S CONTENT
self.tapTimer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:#selector(doSingleTap) userInfo:nil repeats:NO];
}
-(void)doSingleTap
{
self.isDoubleTap = NO;
}
-(void)doDoubleTap
{
self.isDoubleTap = NO;
}
How do I recognise a UISwipeGestureRecognizer with UISwipeGestureRecognizerDirectionUp and then directly after that, WITHOUT lifting my finger, recognise a UISwipeGestureRecognizerDirectionDown recogniser?
Basically I want multiple UISwipeGestureRecognizers being recognised without lifting my finger when I change direction.
My code so far...
- (void)viewDidLoad {
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(screenSwipedUp)];
swipeUp.numberOfTouchesRequired = 1;
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
swipeUp.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:swipeUp];
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(screenSwipedDown)];
swipeDown.numberOfTouchesRequired = 1;
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
swipeDown.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:swipeDown];
}
- (void)screenSwipedUp {
NSLog(#"SW-Up");
}
- (void)screenSwipedDown {
NSLog(#"SW-Down");
}
I don't think you can do it with UISwipeGestureRecognizer. You can instead use UIPanGestureRecognizer and implement the action OR create a subclass of UIGestureRecognizer and Override the methods to recognize your special gesture.
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.