- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_showWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460-44)];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://news.163.com/special/qingsongyike"]];
[_showWebView loadRequest:urlRequest];
[self.view addSubview:_showWebView];
[self addTapOnWebView];
}
-(void)addTapOnWebView
{
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[self.showWebView addGestureRecognizer:singleTap];
singleTap.delegate = self;
singleTap.cancelsTouchesInView = NO;
}
#pragma mark- TapGestureRecognizer
/**
* 3.允许多个手势识别器共同识别
默认情况下,两个gesture recognizers不会同时识别它们的手势,但是你可以实现UIGestureRecognizerDelegate协议中的
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:方法对其进行控制。这个方法一般在一个手 势接收者要阻止另外一个手势接收自己的消息的时候调用,如果返回YES,则两个gesture recognizers可同时识别,如果返回NO,则 并不保证两个gesture recognizers必不能同时识别,因为另外一个gesture recognizer的此方法可能返回YES。也就是说两个
gesture recognizers的delegate方法只要任意一个返回YES,则这两个就可以同时识别;只有两个都返回NO的时候,才是互斥的。默认情况下是返回NO。
* 如果不写这句话,会导致自己创建的手势不会响应事件,因为webView里的子控件自带很多手势。
* @param gestureRecognizer 手势
* @param otherGestureRecognizer 其他手势
*
* @return YES代表可以多个手势同时识别,默认是NO,不可以多个手势同时识别
*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
-(void)handleSingleTap:(UITapGestureRecognizer *)sender
{
CGPoint pt = [sender locationInView:self.showWebView];
NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", pt.x, pt.y];
NSString *urlToSave = [self.showWebView stringByEvaluatingJavaScriptFromString:imgURL];
NSLog(@"image url=%@", urlToSave);
if (urlToSave.length > 0) {
[self showImageURL:urlToSave point:pt];
}
}
//呈现图片
-(void)showImageURL:(NSString *)url point:(CGPoint)point
{
UIImageView *showView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 470)];
showView.center = point;
[UIView animateWithDuration:0.5f animations:^{
CGPoint newPoint = self.view.center;
newPoint.y += 20;
showView.center = newPoint;
}];
showView.backgroundColor = [UIColor blackColor];
showView.alpha = 0.9;
showView.userInteractionEnabled = YES;
[self.view addSubview:showView];
[showView setImageWithURL:[NSURL URLWithString:url]];
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleViewTap:)];
[showView addGestureRecognizer:singleTap];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
//移除图片查看视图
-(void)handleSingleViewTap:(UITapGestureRecognizer *)sender
{
for (id obj in self.view.subviews) {
if ([obj isKindOfClass:[UIImageView class]]) {
[obj removeFromSuperview];
}
}
[self.navigationController setNavigationBarHidden:NO animated:YES];
}