IOS 7 Safari like History dragging gesture using u

2019-03-22 05:58发布

问题:

If you see in ios 7 safari app have left and right dragging to see history of webpages. It also show content of previous page while dragging . I want similar functionality using UIWebiview.

I can get forward and backward page but how that dragging functionality with displaying previous content will be implemented?

回答1:

Note: this is iOS 7 only. For iOS 6 you should use the PanGestureRecognizer and detect if it's left or right.

Your need two properties:

@property (nonatomic, strong) UIImageView * imgvcChild1;
@property (retain, strong) IBOutlet UIWebView *webView;

First of all, add the gesture recognizer to the webView:

[[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(EdgeLeftPanDetected:)];
panLeft.edges = UIRectEdgeLeft;
[self.webView addGestureRecognizer:panLeft];

Control the gesture:

- (void) EdgeLeftPanDetected:(UIScreenEdgePanGestureRecognizer*)gesture {

    if (gesture.state == UIGestureRecognizerStateBegan) {

        UIGraphicsBeginImageContext ( self.webView.frame.size );
        [self.webView.layer renderInContext:UIGraphicsGetCurrentContext() ];

        UIImage *grab = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        if ( _imgvcChild1 ) [ _imgvcChild1 removeFromSuperview ];
        _imgvcChild1 = [[ UIImageView alloc ] initWithImage:grab ];
        _imgvcChild1.frame = self.webView.frame;
        _imgvcChild1.userInteractionEnabled = YES;
        _imgvcChild2 = [self.arrayImagenes lastObject];
        if ([self.webView canGoBack]) {

            [self.webView goBack];

        } 

        [ self.view addSubview:_imgvcChild1 ];

    }

    if (gesture.state == UIGestureRecognizerStateChanged) {
        _imgvcChild1.frame = CGRectMake([ gesture locationInView:_imgvcChild1.superview ].x, _imgvcChild1.frame.origin.y, _imgvcChild1.frame.size.width, _imgvcChild1.frame.size.height);

         }

    }

    if (gesture.state == UIGestureRecognizerStateEnded) {

        [_imgvcChild1 removeFromSuperview];

    }
}

This a prototype, it needs a lot of work, it's just for going back and the page where you go back doesn't have animation. If you want to have animation in the previous page you'll need to create a image before loading the new page and store it on an NSMutableArray and then show it with a different animation (this image starts like -1/4 of screen and goes like 1/4 of the speed of imgvcChild1)

You'll need another gesture recognizer for the right and another array of UIImageViews if you want to go forward too.



回答2:

Few days back i too have similar kind of requirement. I did lot of research for this, but no luck. I read somewhere that this kind of gesture (History dragging) are managed by OS or the browser itself.

History dragging gesture are not available for UIWebView. As of now you cannot do this with UIWebView.

The one gesture will implement to achieve near to this functionality is UIPanGestureRecognizer but, you still not get the content of the previous page(history).