如何在iPhone中调用触摸事件上的图像

发布于 2024-11-03 20:06:22 字数 1212 浏览 0 评论 0原文

我创建了一个应用程序,其中有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情释 2024-11-10 20:06:22

以下更改应该可以解决问题:

- (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; 
}

您的上述代码无法正常工作,因为 if(CGRectContainsPoint(firstImage.frame, location) == YES,它永远不会到达 if(CGRectContainsPoint( secImage.frame, location)

代码也可以清理一下。不确定这是实现这两种方法的理想方法,但无论如何它都可以工作。

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)

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.

椵侞 2024-11-10 20:06:22

可能是你的代码有错误?试试这个

- (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; 
    }
}

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; 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文