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;
}