PDA

View Full Version : UIWebView redraw after gesture handling




frijos
Jul 26, 2011, 11:54 AM
I am struggling with this problem for a very long time, so I will be very grateful if someone told me anything helpful with that. Bascially, I have UIView with three UIWebViews inside. Only one webview is visible: Image :

http://wstaw.org/m/2011/07/26/img1.png
And! I want to change these webviews, loaded with consecutive pages, by animation. So when I made a gesture left, all webviews are moving towards left, and this one on the far left should return to a initial position on the right. Maybe image will make this clear:

http://wstaw.org/m/2011/07/26/img2.png
After return to the position near right it should be loaded with next page in the array.

Where's the problem?
When I make a gesture on the UIView (this area with dots) - webviews are loading fine. When I click a neighbour dot, webviews are animating and loaded with right website. OK.
BUT!
I am making gesture inside a webview. Animation is done, everything is starting to load new websites, but... In our example - after animation, and loading, I am going right again, to see a website4. webview having website1, after being loaded with website4 (webViewDidFinishLoad) is showing old content! (so it's showing page1, having page4. not always, but sometimes... )
When I touch it, and scroll a little down it's showing a right content (???). I tried with setNeedsDisplay on this, triggering hidden = YES/NO, but without any effects.

When I am reloading webview after sending load request it's often showing white,empty area and I need to scroll it down and up few times to make things better.

What should I do? Is it connected with multi threading, animation, wrong gesture used? Should I show some other parts of code? Really THANKS for any ideas... Here's code:

Code:

-(void) initGestures {
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
// swipeRight.numberOfTouchesRequired = 2;
swipeRight.delegate = self;
[self.mainWidgetWithDotsView addGestureRecognizer:swipeRight];

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
// swipeLeft.numberOfTouchesRequired = 2;
swipeLeft.delegate = self;
[self.mainWidgetWithDotsView addGestureRecognizer:swipeLeft];
}

- (void)slideToCurrentPage
{

int page = slider.currentPage;
NSLog(@"slide to current page");
//int page = dots.currentPage;
int trigger = 1; // forward
if (page < mainWidgetManager.currentPane )
trigger = -1; // backward
mainWidgetManager.currentPane = page;

float leftW = leftWebView.layer.position.x;
float centerW = webView.layer.position.x;
float rightW = rightWebView.layer.position.x;


[UIView animateWithDuration:ANIMATION_DURATION
delay:0
options: UIViewAnimationOptionCurveEaseOut
animations:^{ CGRect rc = mainWidgetView.frame;
float w = rc.size.width;
float h = rc.size.height;
switch (page)
{
default:
leftWebView.layer.position = CGPointMake(leftW-(trigger)*w, h/2);
webView.layer.position = CGPointMake(centerW-(trigger)*w, h/2);
rightWebView.layer.position = CGPointMake(rightW-(trigger)*w, h/2);
break;


}
}
completion:^(BOOL finished){ [self swipeEnd:finished direction:trigger]; }];


}

-(void) swipeEnd:(BOOL)finished direction:(int)dir
{

for (int i = 0 ; i < 3 ; i++ )
{
if (positionOfWebViews[i] == CENTER)
{
contents[i] = currentPane;
}

if (positionOfWebViews[i] == FAR_LEFT && dir == 1)
{
NSLog(@"Manager: left goes to right (from %d)", FAR_LEFT);


[self moveWebView:mainWebViews[i] toPos:NEAR_RIGHT];

if (currentPane != widgetsLoaded-1 && contents[i] != currentPane+1)
{
//[mainWebViews[i] stopLoading];
contents[i] = currentPane+1;

NSLog(@"Manager: and it's loaded with %d",currentPane+1);
[self loadToWebView:mainWebViews[i] subwidget:currentPane+1];
}


}
else if ( positionOfWebViews[i] == FAR_RIGHT && dir == -1)
{
NSLog(@"right (%d) goes to left", i);

[self moveWebView:mainWebViews[i] toPos:NEAR_LEFT];


if (currentPane != 0 && contents[i] != currentPane -1)
{

contents[i] = currentPane - 1;



NSLog(@"Manager: and it's loaded with %d",currentPane-1);

[self loadToWebView:mainWebViews[i] subwidget:currentPane-1];

}
}


}


}

-(void)loadToWebView:(UIWebView *)wv subwidget:(int)i
{
Subwidget *tmp = [history objectAtIndex:i];
NSString *a = [NSString stringWithString:tmp.name];


[wv stopLoading];

// [wv loadHTMLString:a baseURL:nil]; <--- no difference
[wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:a]]];

// [wv reload];




}



- (IBAction)handlePanGesture:(UISwipeGestureRecognizer *)sender {
NSLog(@"gesture");


if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {

//[mainWidgetManager getCenterWebView].userInteractionEnabled = NO;
NSLog(@"left");

if (slider.currentPage != slider.numberOfPages-1)
{

[slider setCurrentPage:slider.currentPage+1 animated:YES];
[slider sendActionsForControlEvents:UIControlEventValueChanged];

}
}
else
if (sender.direction == UISwipeGestureRecognizerDirectionRight)
{
// [mainWidgetManager getCenterWebView].userInteractionEnabled = NO;
NSLog(@"right");
if (slider.currentPage > 0)
{


[slider setCurrentPage:slider.currentPage-1 animated:YES];
[slider sendActionsForControlEvents:UIControlEventValueChanged];

}
}


}

- (void)onPageChanged:(id)sender
{
if ( abs( slider.currentPage - mainWidgetManager.currentPane ) > 1 )
{
NSLog(@"long jump");
[self jumpToPage:slider.currentPage];
}
else
{
if (mainWidgetManager.currentPane != slider.currentPage)
[self slideToCurrentPage:NO dir:NO];
}


}



frijos
Jul 28, 2011, 09:43 AM
Solution: I am using new uiwebview every time I want to load something offscreen.