问题描述
如果您在ios 7中看到safari应用程序左右拖动以查看网页历史记录。它还会在拖动时显示上一页的内容。我想要使用UIWebiview的类似功能。
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?
推荐答案
注意:这只是iOS 7。对于iOS 6,你应该使用PanGestureRecognizer并检测它是左还是右。
Note: this is iOS 7 only. For iOS 6 you should use the PanGestureRecognizer and detect if it's left or right.
你需要两个属性:
@property (nonatomic, strong) UIImageView * imgvcChild1;
@property (retain, strong) IBOutlet UIWebView *webView;
首先,将手势识别器添加到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];
控制手势:
- (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一样)
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
)
如果你想继续前进,你需要另一个右手势识别器和另一个 UIImageViews
阵列。
You'll need another gesture recognizer for the right and another array of UIImageViews
if you want to go forward too.
这篇关于IOS 7 Safari就像历史使用uiwebview拖动手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!