你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios 網頁中圖片點擊放大效果demo

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

編輯:IOS開發綜合

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

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

}

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved