Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 对“触摸”事件执行几乎相同的操作(针对不同的图像)?_Objective C_Ios_Touch - Fatal编程技术网

Objective c 对“触摸”事件执行几乎相同的操作(针对不同的图像)?

Objective c 对“触摸”事件执行几乎相同的操作(针对不同的图像)?,objective-c,ios,touch,Objective C,Ios,Touch,我希望有更好的方法来实现以下目标。我正在创建一个jigsaw类型的应用程序,这是我正在使用的当前代码: -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; //location of current touch CGPoint location = [touch locationInView:self.view]; if ([touch v

我希望有更好的方法来实现以下目标。我正在创建一个jigsaw类型的应用程序,这是我正在使用的当前代码:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//location of current touch
CGPoint location = [touch locationInView:self.view];
if ([touch view] == img1) {
    [self animateFirstTouch:img1 withLocation:location];
} else if ([touch view] == img2) {
    [self animateFirstTouch:img2 withLocation:location];
} else if ([touch view] == img3) {
    [self animateFirstTouch:img3 withLocation:location];
} else if ([touch view] == img4) {
    [self animateFirstTouch:img4 withLocation:location];
} else if {
......
......
} else if ([touch view] == img40) {
    [self animateFirstTouch:img40 withLocation:location];
    return;
}
}
我希望有一种更好、更有效的方法来做到这一点,而不是为每一张图片命名。我的想法是,如果触摸视图等于UIImageView,那么执行一些任务。touchesEnded的情况也一样:

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//location of current touch
CGPoint location = [touch locationInView:self.view];
if ([touch view] == image1) {
    [self animateReleaseTouch:image1 withLocation:location];
} else if ([touch view] == image2) {
    [self animateReleaseTouch:image2 withLocation:location];
} else if ([touch view] == image3) {
    [self animateReleaseTouch:image3 withLocation:location];
} else if ([touch view] == image4) {
    [self animateReleaseTouch:image4 withLocation:location];
} else if{
......
......
} else if ([touch view] == image40) {
    [self animateReleaseTouch:image40 withLocation:location];
}
    return;
}

有什么帮助吗?

请稍等,如果您总是为触摸的视图设置动画,那么为什么不这样做呢

[self animateFirstTouch:[touch view] withLocation:location];
... 
[self animateReleaseTouch:[touch view] withLocation:location];

等一下,如果您总是为触摸的视图设置动画,那么为什么不这样做呢

[self animateFirstTouch:[touch view] withLocation:location];
... 
[self animateReleaseTouch:[touch view] withLocation:location];

我对你想要达到的目标有点困惑

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
{
    UITouch *touch = [touches anyObject];
    //location of current touch
    CGPoint location = [touch locationInView:self.view];
    if ([touch view] == img1) {
        [self animateFirstTouch:img1 withLocation:location];
    } else if ([touch view] == img2) {
        [self animateFirstTouch:img2 withLocation:location];
    } else if ([touch view] == img3) {
        [self animateFirstTouch:img3 withLocation:location];
    } else if ([touch view] == img4) {
        [self animateFirstTouch:img4 withLocation:location];
    } else if {
        ......
        ......
    } else if ([touch view] == img40) {
        [self animateFirstTouch:img40 withLocation:location];
        return;
    }
}
似乎你在每种情况下都在做完全相同的动作,为什么不这样做呢

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
{
    UITouch *touch = [touches anyObject];
    //location of current touch
    CGPoint location = [touch locationInView:self.view];

    [self animateFirstTouch:[touch view] withLocation:location];
}

我对你想要达到的目标有点困惑

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
{
    UITouch *touch = [touches anyObject];
    //location of current touch
    CGPoint location = [touch locationInView:self.view];
    if ([touch view] == img1) {
        [self animateFirstTouch:img1 withLocation:location];
    } else if ([touch view] == img2) {
        [self animateFirstTouch:img2 withLocation:location];
    } else if ([touch view] == img3) {
        [self animateFirstTouch:img3 withLocation:location];
    } else if ([touch view] == img4) {
        [self animateFirstTouch:img4 withLocation:location];
    } else if {
        ......
        ......
    } else if ([touch view] == img40) {
        [self animateFirstTouch:img40 withLocation:location];
        return;
    }
}
似乎你在每种情况下都在做完全相同的动作,为什么不这样做呢

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
{
    UITouch *touch = [touches anyObject];
    //location of current touch
    CGPoint location = [touch locationInView:self.view];

    [self animateFirstTouch:[touch view] withLocation:location];
}

请稍候,以便您正在测试[touch view]是否等于特定视图,以便您可以将该特定视图传递给其他方法?如果是这样的话,它是什么观点并不重要,重要的是它触及了哪一种观点

因此,与此相反:

if ([touch view] == image1) {
    [self animateReleaseTouch:image1 withLocation:location];
} else if ([touch view] == image2) {
    [self animateReleaseTouch:image2 withLocation:location];
} else if ([touch view] == image3) {
    [self animateReleaseTouch:image3 withLocation:location];
}
您应该只需要以下内容:

[self animateReleaseTouch:[touch view] withLocation:location];
或者,如果您想确保您只能对您的作品的图像视图执行此操作,请将它们粘贴在一个数组中,并确保此视图包含在该数组中

// do this during setup somewhere
NSArray *imageViews = [NSArray arrayWithObjects:image1, image2, ..., nil];

// do this on touch
UIView *touchedView = [touch view];
if ([imageViews indexOfObject:touchedView] != NSNotFound) {
    // not not found means found!
    [self animateReleaseTouch:touchedView withLocation:location];
}

99.99%的情况下,如果有大量按顺序命名的变量,那么就错了。您真正想要的是一个数组。

等等,您正在测试[touch view]是否等于一个特定视图,以便您可以将该特定视图传递给另一个方法?如果是这样的话,它是什么观点并不重要,重要的是它触及了哪一种观点

因此,与此相反:

if ([touch view] == image1) {
    [self animateReleaseTouch:image1 withLocation:location];
} else if ([touch view] == image2) {
    [self animateReleaseTouch:image2 withLocation:location];
} else if ([touch view] == image3) {
    [self animateReleaseTouch:image3 withLocation:location];
}
您应该只需要以下内容:

[self animateReleaseTouch:[touch view] withLocation:location];
或者,如果您想确保您只能对您的作品的图像视图执行此操作,请将它们粘贴在一个数组中,并确保此视图包含在该数组中

// do this during setup somewhere
NSArray *imageViews = [NSArray arrayWithObjects:image1, image2, ..., nil];

// do this on touch
UIView *touchedView = [touch view];
if ([imageViews indexOfObject:touchedView] != NSNotFound) {
    // not not found means found!
    [self animateReleaseTouch:touchedView withLocation:location];
}

99.99%的情况下,如果有大量按顺序命名的变量,那么就错了。您真正想要的是一个数组。

如果您想确保只对UIImageView设置动画,那么为什么不使用[touch view]

然后这样做:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
{
    UITouch *touch = [touches anyObject];
    //location of current touch
    CGPoint location = [touch locationInView:self.view];
    if ([[touch view] isKindOfClass:[UIImageView class]]) {
        [self animateFirstTouch:[touch view] withLocation:location];
    }
}
因此,如果正在触摸的视图不是UIImageView,则它将不会设置动画

请注意,如果接收器即[touch view]是UIImageView的子类,isKindOfClass将返回yes

如果您只希望该语句在恰好是UIImageView时为true,如果它是UIImageView的子类则返回false,请使用isMemberOfClass:而不是isKindOfClass:


还值得指出的是,如果在父视图中除了img1…img40之外还有其他UIImageView,那么这不是您要寻找的答案。如果您希望为父视图中的任何UIImageView调用animateFirstTouch:withLocation:。

如果您希望确保仅为UIImageView设置动画,那么这就是为什么不使用[touch view]

然后这样做:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
{
    UITouch *touch = [touches anyObject];
    //location of current touch
    CGPoint location = [touch locationInView:self.view];
    if ([[touch view] isKindOfClass:[UIImageView class]]) {
        [self animateFirstTouch:[touch view] withLocation:location];
    }
}
因此,如果正在触摸的视图不是UIImageView,则它将不会设置动画

请注意,如果接收器即[touch view]是UIImageView的子类,isKindOfClass将返回yes

如果您只希望该语句在恰好是UIImageView时为true,如果它是UIImageView的子类则返回false,请使用isMemberOfClass:而不是isKindOfClass:


还值得指出的是,如果在父视图中除了img1…img40之外还有其他UIImageView,那么这不是您要寻找的答案。如果您希望为父视图中的任何UIImageView调用animateFirstTouch:withLocation:,则这是答案。

我会鹦鹉学舌地回答上面的问题:

if ([touch view] == image1) {
    [self animateReleaseTouch:image1 withLocation:location];
} else if ([touch view] == image2) {
    [self animateReleaseTouch:image2 withLocation:location];
} else if ([touch view] == image3) {
    [self animateReleaseTouch:image3 withLocation:location];
} else if ([touch view] == image4) {
    [self animateReleaseTouch:image4 withLocation:location];
} else if{
......
......
只是一个冗长的版本:

[self animateReleaseTouch:[touch view] withLocation:location];
然而,我会超越这一点,并建议您可能正在避免一个明显的结论,即您所采用的不是一个充分面向对象的设计。您可能想做的是将这些动画的责任转移到视图本身,并让它们为touchsbegind:等实现适当的捕捉。然后事件传播的内置机制将生效


先例是UIButton、UISlider等。它们都直接在内部响应触摸,并根据需要更新显示。您可能想创建一个自定义UIView子类。

我会重复上面的答案:

if ([touch view] == image1) {
    [self animateReleaseTouch:image1 withLocation:location];
} else if ([touch view] == image2) {
    [self animateReleaseTouch:image2 withLocation:location];
} else if ([touch view] == image3) {
    [self animateReleaseTouch:image3 withLocation:location];
} else if ([touch view] == image4) {
    [self animateReleaseTouch:image4 withLocation:location];
} else if{
......
......
只是一个冗长的版本:

[self animateReleaseTouch:[touch view] withLocation:location];
然而,我会超越这一点,并建议您可能正在避免一个明显的结论,即您所采用的不是一个充分面向对象的设计。您可能想做的是将这些动画的责任转移到视图本身,并让它们为touchsbegind:等实现适当的捕捉。然后事件传播的内置机制将生效

先例是UIButton、UISlider等

y所有人都直接和在内部响应触摸,并根据需要更新其显示。你可能想创建一个自定义UIView子类。

-1,我的答案的直接副本,比你的答案早20秒:@RichardJ.RossIII,这太不公平了,写答案需要20秒以上,这就是为什么有“加载新答案”按钮。你应该在发布之前阅读所有当前的答案。你真的因为我们得出了相同的结论而否决了投票吗?格式化一个答案需要20多秒。…。@RichardJ.RossIII我想你忘了这是一个社区驱动的网站,人们提供他们的个人时间和经验来帮助其他需要帮助的开发者。当你有一份全职工作和一个家庭时,个人时间就不像你十几岁时那么容易获得了。我知道有一个评分系统,但它不是MMOG-声誉是社区对你信任程度的粗略衡量,所以不要做一个混蛋,让人们放弃贡献…-1,直接复制我在你回答之前20秒的答案:@RichardJ.RossIII,这太不公平了,写一个答案需要超过20秒,这就是为什么有“加载新答案”按钮的原因。你应该在发布之前阅读所有当前的答案。你真的因为我们得出了相同的结论而否决了投票吗?格式化一个答案需要20多秒。…。@RichardJ.RossIII我想你忘了这是一个社区驱动的网站,人们提供他们的个人时间和经验来帮助其他需要帮助的开发者。当你有一份全职工作和一个家庭时,个人时间就不像你十几岁时那么容易获得了。我知道有一个评分系统,但它不是一个MMOG-声誉是一个粗略的衡量,社区有多信任你,所以不要做一个混蛋,让人们放弃贡献…谢谢你的解释。我有一些错误。对于我的'NSArray*imageViews=[NSArray arrayWithObjects:image1,image2,…,nil];',我得到“使用未声明的标识符img1、img2,…”。对于“[self-animateReleaseTouch:touchedView with location:location];”,我得到“不兼容的指针类型正在将uiview发送到uiimageview”。该行无法编译。如果没有更多的代码知识,我无法假设您需要如何构建该数组。查看NSArray上的文档,了解如何创建一个以及如何使用它。谢谢Alex。我已经解决了NSArray问题,但仍然存在touchedView的不兼容指针类型错误。它编译并似乎运行正常though@garethdn还有另外一个问题:谢谢你的解释。我有一些错误。对于我的'NSArray*imageViews=[NSArray arrayWithObjects:image1,image2,…,nil];',我得到“使用未声明的标识符img1、img2,…”。对于“[self-animateReleaseTouch:touchedView with location:location];”,我得到“不兼容的指针类型正在将uiview发送到uiimageview”。该行无法编译。如果没有更多的代码知识,我无法假设您需要如何构建该数组。查看NSArray上的文档,了解如何创建一个以及如何使用它。谢谢Alex。我已经解决了NSArray问题,但仍然存在touchedView的不兼容指针类型错误。它编译并似乎运行正常though@garethdn还有另外一个问题: