2011-04-27 94 views
1

我有2個imageviews與它的圖像。我希望當我點擊第一個圖像的圖像應該被選中,如果它被選中它應該返回我的價值TRUE或1應該保存在sqlite數據庫。這是可能的。請問任何人都可以幫助我解決這個問題。 感謝我們如何檢查imageview中的哪個圖像被調用

+0

您可以使用觸摸方法通過觸摸方法U將得到該圖像視圖觸摸實施這一 – Gypsa 2011-04-27 10:14:34

+0

..並在選擇從圖像視圖獲取圖像並將圖像數據轉儲到文件而不是數據庫。 – santosh 2011-04-27 10:26:13

+0

其實我不能正確得到你可以請你詳細解釋我。謝謝 – Rani 2011-04-27 10:31:06

回答

0

你去觸摸events.Capture的接觸點,並執行actions.Here我會給UA樣本結構要做到這一點,

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [[event allTouches] anyObject]; 

    CGPoint location= [touch locationInView:self.view]; 

    if(CGRectContainsPoint(firstImage.frame, location)) { 
     //set some flag like 
     selectionFlag=1;  } 
    else if(CGRectContainsPoint(secImage.frame, location)){ 
     selectionFlag=2;  } 
    } 

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

     UITouch *touch = [[event allTouches] anyObject]; 
     CGPoint location = [touch locationInView:self.view]; 

     if(CGRectContainsPoint(firstImage.frame, location)) { 
      if(selectionflag==1) { 
       //do ur db actions    } 
     } 
     else if(CGRectContainsPoint(secImage.frame, location)) {  
      if(selectionflag==2) { 
      //do ur db actions    } 
     } 
     selectionflag=0; 
    } 
+0

嗨,我有一個懷疑第一個圖像和secImage是imageviews或圖像對象 – Rani 2011-04-28 08:47:49

+0

謝謝代碼正常工作 – Rani 2011-04-28 11:34:50

0

先做

[self.*yourimageViewname* setUserEnteractionEnabled:YES]; 
BOOL select1,secelect2; 
select1=NO; 
select2=NO; 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

UITouch *touch=[touches anyObject]; 
CGPoint touchLocation = [touch locationInView:touch.view]; 
//give the beginning and ending x and y points in condition to check which imageView is taped 

if(touchLocation.x>1 && touchLocation.x<116 && touchLocation.y>133 && touchLocation.y<233) 
{ 
select1=YES; 
} 
else if(touchLocation.x>120 && touchLocation.x<300 && touchLocation.y>133 && touchLocation.y<233) 
{ 
select2=YES; 
} 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    } 

在條件,布爾變量是真的,你可以保存或任何你想做的進一步編碼您的應用程序。

+0

你是正確的,但我想保存我的值在database.in sqlite數據庫布爾值不能binded.Thanks – Rani 2011-04-27 12:46:09

+0

現在不可以保存您的值在nsstring或其他一些其他基於您的布爾條件,如採取兩個整數,並通過1如果布爾是yes,如果布爾是no,則爲0。 – Gypsa 2011-04-28 03:45:26

+0

不能現在可以保存你的值在nsstring或其他一些基於你的布爾條件,如採取兩個整數,如果布爾是yes,則傳遞1,如果布爾值爲no,則傳遞0。 – Gypsa 2011-04-28 03:46:01

0

使用觸摸是下面答案中解釋的一種方法。如果您只有兩個imageview,您還可以嘗試在該imageview的頂部有兩個自定義透明按鈕,因此您可以根據您爲按鈕提供的標籤輕鬆知道哪個圖像被觸摸。

+0

,但如何保存選定的按鈕在sqlite數據庫的值。請你幫我。 – Rani 2011-04-27 11:23:32

+0

基於你可以保存圖像的按鈕 – visakh7 2011-04-27 11:26:12

1

通過使用UITouch類的方法,你會得到哪個圖像視圖觸摸..或者你可以把imageview裏面的按鈕,然後你會得到點擊事件。

0

你還可以做的是創建兩個按鈕(而不是UIImageViews)有圖像。他們應該顯示大致相同的(你甚至可以禁用觸摸狀態等)。您可以免費獲得UIResponder事件(如在;您可以將操作定位到選擇器)。

更新:這裏大概是如何(雖然沒有打擾配置按鈕)。

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button1 setImage:yourImage forState:UIControlStateNormal]; 
[button1 addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside]; 

// .. now create a second button .. // 

,觸碰到你的按鈕將進入下面的方法:

- (void)buttonTouched:(id)sender 
{ 
    // .. add your stuff to your database .. // 
    // .. you can identify your button by sender, or give the button a tag (number) to identify it ../
} 
相關問題