2011-04-27 96 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 ../
} 
相关问题