如何在iPhone中调用触摸事件上的图像
我创建了一个应用程序,其中有 2 个图像视图,并且我在图像视图上添加了图像。我不知道当我单击图像时,应该选择图像并且该值应该保存在 sqlite 数据库中。因此,我创建了触摸方法,并为两个图像添加了标志,因此当选择特定图像时,它可以通过其标志来识别。 这是我的代码:
-(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;
}
}
}
但是我遇到了问题,当我选择第一个图像时,它正确地进入 if 部分并将值 1 存储到 var 1 中,但是当我单击 secimage 时,它不会进入 elseif 部分,它只是从循环。可能是什么问题。请帮我解决这个问题。谢谢
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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下更改应该可以解决问题:
您的上述代码无法正常工作,因为
if(CGRectContainsPoint(firstImage.frame, location) == YES
,它永远不会到达if(CGRectContainsPoint( secImage.frame, location)
代码也可以清理一下。不确定这是实现这两种方法的理想方法,但无论如何它都可以工作。
The following change should fix the problem:
Your above code wasn't working because
if(CGRectContainsPoint(firstImage.frame, location) == YES
, it will never reach the lineif(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.
可能是你的代码有错误?试试这个
May be your code has a mistake? Try this