[英]how to call image on touch event in iphone
i have created an application in which there are 2 imageviews and i have added image on image view.i wnt that when i click on my image the image should get selected and the value should be saved in sqlite database.So for that i have created touches method,and added flags for both the images so when particular image is selected it is identified by its flag.我创建了一个应用程序,其中有 2 个图像视图,并且我在图像视图上添加了图像。我希望当我单击我的图像时,图像应该被选中并且值应该保存在 sqlite 数据库中。所以我创建了touches method,and added flags for both the images so when particular image is selected it is identified by its flag. this is my code:
这是我的代码:
-(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
select=1;
}
else if(CGRectContainsPoint(secImage.frame, location))
{
select=2;
}
[mComment resignFirstResponder];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if(CGRectContainsPoint(firstImage.frame, location)) {
if(select==1) {
var=1;
}
else if(CGRectContainsPoint(secImage.frame, location)) {
if(select==2) {
vars=2;
}
select=0;
}
}
}
But i am having a problem,when i select my first image it properly gets into the if part and stores value 1 into var 1 but when i click secimage it does not enter into elseif part,it just comes out of the loop.What may be the problem.Please help me in solving this problem.thanks但是我遇到了一个问题,当我 select 我的第一个图像正确进入 if 部分并将值 1 存储到 var 1 但是当我单击 secimage 它没有进入 elseif 部分时,它只是退出了循环。可能会发生什么是问题。请帮我解决这个问题。谢谢
May be your code has a mistake?可能是您的代码有错误? Try this
尝试这个
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if(CGRectContainsPoint(firstImage.frame, location)) {
if(select==1)
{
var=1;
}
}
else if(CGRectContainsPoint(secImage.frame, location))
{
if(select==2)
{
vars=2;
}
select=0;
}
}
The following change should fix the problem:以下更改应该可以解决问题:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if(CGRectContainsPoint(firstImage.frame, location)) {
if(select==1) {
var=1;
}}
if(CGRectContainsPoint(secImage.frame, location)) {
if(select==2) {
vars=2;
}}
select=0;
}
Your above code wasn't working because if(CGRectContainsPoint(firstImage.frame, location) == YES
, it will never reach the line if(CGRectContainsPoint(secImage.frame, location)
您上面的代码不起作用,因为
if(CGRectContainsPoint(firstImage.frame, location) == YES
,它永远不会到达这条线if(CGRectContainsPoint(secImage.frame, location)
The code could also be cleaned up a bit.代码也可以稍微清理一下。 Not sure this is the ideal way of implementing these two methods, but it could work anyway.
不确定这是实现这两种方法的理想方式,但无论如何它都可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.