我希望有更好的方法来实现以下目标。我正在创建一个拼图类型的应用程序,这是我正在使用的当前代码:
-(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;
}
请帮忙吗?
答案 0 :(得分:6)
等等,所以您正在测试以查看[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%的时间你有大量的顺序命名变量,你做错了。你真正想要的是一个数组。
答案 1 :(得分:1)
我对你想要实现的目标感到有些困惑
-(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];
}
答案 2 :(得分:1)
如果你想确保只有UIImageViews被动画化(那么为什么你不只是使用[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,那么它将不会设置动画。
请注意,如果接收方(即isKindOfClass
)是UIImageView的子类,[touch view]
将返回yes。
如果你只希望那个语句是真正的UIImageView,那么如果它是UIImageView的子类,则返回false,使用isMemberOfClass:
而不是isKindOfClass:
值得指出的是,如果你在父视图中有其他UIImageView,除了img1 ... img40之外,这不是你正在寻找的答案。答案是,如果您希望在父视图中为任何UIImageView调用animateFirstTouch:withLocation:
。
答案 3 :(得分:0)
等一下,如果你总是为触摸的视图设置动画,那为什么不这样做呢?
[self animateFirstTouch:[touch view] withLocation:location];
...
[self animateReleaseTouch:[touch view] withLocation:location];
答案 4 :(得分:0)
我把上面的答案鹦鹉:
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];
然而,我会超越这一点,并建议你可能会避免明显的结论,即你采用的不是一个足够面向对象的设计。您可能想要做的是将这些动画的责任下放到视图本身,并让它们为touchesBegan:
等实现适当的捕获。然后,事件传播的内置机制将生效。
先例将是UIButton
,UISlider
等等。它们都会直接和内部响应触摸,并在适当时更新其显示。您可能想要创建自定义UIView
子类。