ios 網頁中圖片點擊放大效果demo


demo功能:點擊網頁中的圖片,圖片放大效果的demo。iphone6.1 測試通過。

demo說明:通過webview的委托事件shouldStartLoadWithRequest來實現。

demo截屏:


demo主要代碼:

 

#pragma mark -
#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    //將url轉換為string
	NSString *picName = [[request URL] absoluteString];
	NSLog(@"picName is %@",picName);
    //hasPrefix 判斷創建的字符串內容是否以pic:字符開始
	if ([picName hasPrefix:@"pic:"]) {
		[self showBigImage:[picName substringFromIndex:4]];
		return NO;
	}else {
		return YES;
	}
}
#pragma mark -
//顯示大圖片
-(void)showBigImage:(NSString *)imageName{
	//創建灰色透明背景,使其背后內容不可操作
	UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
	[bgView setBackgroundColor:[UIColor colorWithRed:0.3 
											   green:0.3 
												blue:0.3 
											   alpha:0.7]];
	[self.view addSubview:bgView];
	[bgView release];
	
	//創建邊框視圖
	UIView *borderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, BIG_IMG_WIDTH+16, BIG_IMG_HEIGHT+16)];
	//將圖層的邊框設置為圓腳 
    borderView.layer.cornerRadius = 8; 
	borderView.layer.masksToBounds = YES; 
    //給圖層添加一個有色邊框 
	borderView.layer.borderWidth = 8; 
	borderView.layer.borderColor = [[UIColor colorWithRed:0.9
													green:0.9
													 blue:0.9
													alpha:0.7] CGColor];
	[borderView setCenter:bgView.center];
	[bgView addSubview:borderView];
	[borderView release];
	
	//創建關閉按鈕
	UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	[closeBtn setImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal];
	[closeBtn addTarget:self action:@selector(removeBigImage:) forControlEvents:UIControlEventTouchUpInside];
	NSLog(@"borderview is %@",borderView);
	[closeBtn setFrame:CGRectMake(borderView.frame.origin.x+borderView.frame.size.width-20, borderView.frame.origin.y-6, 26, 27)];
	[bgView addSubview:closeBtn];
	
	//創建顯示圖像視圖
	UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 8, BIG_IMG_WIDTH, BIG_IMG_HEIGHT)];
	[imgView setImage:[UIImage imageNamed:imageName]];
	[borderView addSubview:imgView];
	[imgView release];

}


demo下載地址: http://download.csdn.net/download/donny_zhang/5679395

 



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM