2012-12-14 36 views
0

我正在開發一個應用程序,其中我的任務是從我創建滾動視圖並將圖像放在滾動上的數據庫中獲取url-links。
我已經寫了一些代碼,當我點擊圖像時,我必須在webview中顯示url頁面,並將其放在滾動視圖上方。
爲此,我寫了觸摸代碼獲取網址,當我點擊圖像,但我得到的問題,像我的點擊不工作的圖像和滾動視圖也,但它是工作的圖像和scrollview這是一個normalview 。 U可以在下面找到scrollView上的觸摸功能

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch * touch = [[event allTouches] anyObject]; 
    for(int index=0;index<[array count];index++) 
{ 

    UIImageView *imgView = [array objectAtIndex:index]; 
     if(CGRectContainsPoint([imgView frame], [touch locationInView:scrollView])) 
     { 
      NSString *strgetcontenturl = [[NSString alloc]initWithString:[arrayvideoFile objectAtIndex:index]]; 
      NSURL *url = [NSURL URLWithString:strgetcontenturl]; 
      [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strgetcontenturl]]]; 
      NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
      [webview loadRequest:requestObj]; 

     }}} 

這裏陣列我的觸動代碼是我保存的所有圖像init和arrayvideoFile是我保存所有的URL。

請幫忙。

+0

如何在滾動視圖上添加圖像? –

+0

雅我做@ankityadav – shaan

回答

0

你是如何將你的圖像放到scollview的? ImageView的?

我會將它們放在一個UIButton(CustomType)中,然後您可以將該目標添加到該按鈕並且不需要檢測觸摸。因爲UIButton已經檢測到觸摸事件。

UIButton *button = [UIButton butttonWithType:UIButtonTypeCustom]; 
UIImage * buttonImage = .... 
[button setImage:buttonImage forState:UIControlStateNormal]; 
button.frame = CGRectMake(x, y, buttonImage.size.width, buttonImage.size.height); 
[button addTarget:self 
      action:@selector(buttonAction:) 
forControlEvents:UIControlEventTouchUpInside]; 
[scrollview addSubview:button]; 

編輯:添加代碼

+0

我忘了提及,我從數據庫中獲取圖像,我試圖給圖像的按鈕背景,但它沒有奏效 – shaan

+0

我更新了我的答案並添加了代碼。 – Mert

+0

請在我剛纔發現的問題中找到變化@mert – shaan

0

剛剛成立imgView.userInteractionEnabled=YES;它可以幫助你。

否則使用此代碼下面的代碼

UIScrollView *scrollview4preview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-44)]; 
    scrollview4preview.backgroundColor=[UIColor clearColor]; 
    [self.view addSubview:scrollview4preview]; 

    int yval=0; 
    for (int i=0; i<[arrary count]; i++) { 
     UIButton *btn4preview=[[UIButton alloc]init]; 
     btn4preview.tag=i; 
     btn4preview.frame=CGRectMake(scrollview4preview.frame.origin.x+10, i*yval, 150, 150); 
     [btn4preview setImage:[UIImage imageNamed:@"YourImageName.png"] forState:UIControlStateNormal]; 
     [btn4preview addTarget:self action:@selector(btnPreviewSelect:) forControlEvents:UIControlEventTouchUpInside]; 
     btn4preview.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin; 
     [scrollview4preview addSubview:btn4preview]; 

    } 

    scrollview4preview.contentSize=CGSizeMake(scrollview4preview.frame.size.width, yval+160); 


-(void)btnPreviewSelect:(id)sender 
{ 
    UIButton *btn=(UIButton *)sender; 

NSString *strgetcontenturl = [[NSString alloc]initWithString:[arrary objectAtIndex:btn.tag]]; 
NSURL *url = [NSURL URLWithString:strgetcontenturl]; 
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strgetcontenturl]]]; 
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
[webview loadRequest:requestObj]; 

} 
+0

不工作的人@Senthilkumar – shaan

+0

已更新我的代碼檢查 – Senthilkumar

+0

[btn4preview setImage:[UIImage imageNamed:@「YourImageName.png」] forState:UIControlStateNormal];這裏可以我把我的數組放在我存儲所有來自數據庫的圖像,因爲我有多個圖像 – shaan

0

使用。

for(int Tag =0; Tag < [array count]; Tag++) { 
    UIButton *btn_URL = [UIButton butttonWithType:UIButtonTypeCustom]; 

    UIImage * btn_URL = [btn_URL setImage:buttonImage forState:UIControlStateNormal]; 

    btn_URL.frame = CGRectMake(x, y, buttonImage.size.width, buttonImage.size.height); 

    [btn_URL addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 

    btn_URL.tag = Tag; 

    [scrollview addSubview:btn_URL]; 
} 

-(void)buttonAction:(id)sender { 
    NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[array objectAtIndex:[sender Tag]]]]; 

} 

在這裏,您可以將用戶重定向到URL。

如果您需要任何幫助,請告訴我。

謝謝,

Hemang。

+0

我可以粘貼此代碼之間的觸摸代碼或我宣佈圖像 – shaan

+0

沒有....不要使用觸摸方法。粘貼循環你想在哪裏添加滾動視圖中有圖像的按鈕,然後是下面的方法來檢測動作。 – Hemang