IOS 7 Safari就像历史使用uiwebview拖动手势一样

时间:2013-10-24 05:04:25

标签: uiwebview ios7 mobile-safari

如果您在ios 7中看到safari应用程序左右拖动以查看网页历史记录。它还会在拖动时显示上一页的内容。我想要使​​用UIWebiview的类似功能。

我可以前进和后退页面但是如何实现显示以前内容的拖动功能?

2 个答案:

答案 0 :(得分:6)

注意:这只是iOS 7。对于iOS 6,您应该使用PanGestureRecognizer并检测它是左还是右。

您需要两个属性:

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

首先,将手势识别器添加到webView:

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

控制手势:

- (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];

    }
}

这是一个原型,它需要做很多工作,它只是为了回头而你回去的页面没有动画。如果你想在上一页中有动画,你需要在加载新页面之前创建一个图像并将其存储在NSMutableArray上,然后用不同的动画显示它(这个图像开始像-1/4屏幕和imgvcChild1的速度的1/4

如果你想继续前进,你还需要另一个手势识别器和UIImageViews的另一个阵列。

答案 1 :(得分:0)

几天前我也有类似的要求。我为此做了很多研究,但没有运气。我在某处读到这种手势(历史拖动)是由操作系统或浏览器本身管理的。

UIWebView 无法使用历史记录拖动手势。截至目前,您无法使用 UIWebView 执行此操作。

实现接近此功能的一个手势是 UIPanGestureRecognizer 但是,您仍然无法获得上一页(历史记录)的内容。