Reputation: 12351
I'm hoping there is a better way to the following. I'm creating a jigsaw-type application and this is the current code i'm using:
-(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;
}
}
I'm hoping that there is a better, more efficieny way to do this, rather than naming every image. I'm thinking something like, if touch view is equal to a UIImageView, then perform some task. The same for 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;
}
Any help please?
Upvotes: 1
Views: 224
Reputation: 100632
I'd parrot the answers above that:
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{
......
......
Is just a long-winded version of:
[self animateReleaseTouch:[touch view] withLocation:location];
However I'd go beyond that and suggest that you may be avoiding the obvious conclusion that what you've adopted isn't a sufficiently object oriented design. What you probably want to do is devolve responsibility for these animations to the views themselves, and have them implement appropriate catches for touchesBegan:
, etc. Then the built-in mechanisms for event propagation will take effect.
The precedent would be things like UIButton
, UISlider
, etc. They all respond to touch directly and internally, updating their display as appropriate. You likely want to create a custom UIView
subclass.
Upvotes: 0
Reputation: 55554
If you want to make sure only UIImageViews are animated (hence why you are not just using [touch view]
)
Then do this:
-(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];
}
}
So if the view being touched is not a UIImageView then it will not animate.
Note that isKindOfClass
will return yes if the receiver (i.e. [touch view]
) is a subclass of UIImageView.
If you only wanted that statement to be true when it is exactly a UIImageView, and so return false if it's a subclass of UIImageView, use isMemberOfClass:
instead of isKindOfClass:
It's also worth pointing out that if you have other UIImageView in the parent view, besides the img1...img40 then this is not the answer you are looking for. This is answer is if you want animateFirstTouch:withLocation:
to be called for any UIImageView in the parent view.
Upvotes: 1
Reputation: 187034
Wait so you are testing to see if the [touch view]
is equal to a specific view, so you can pass that specific view to that other method? If so, it doesn't matter what view it was, and it only matters which one it touched.
So instead of this:
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];
}
You should only need this:
[self animateReleaseTouch:[touch view] withLocation:location];
Or if you want to ensure that you can only do this with your image views for your pieces, stick them in an array and ensure this view is included in that array.
// 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% percent of the time you have a ton of sequentially named variables, you are doing it wrong. What you really want instead is an array.
Upvotes: 6
Reputation: 38728
I'm slightly confused about what you are trying to achieve
-(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;
}
}
Seems as you are doing the exact same action in each case why not just do
-(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];
}
Upvotes: 1
Reputation: 55563
Wait a minute, if you are always animated the view that the touch is for, then why not just do this?
[self animateFirstTouch:[touch view] withLocation:location];
...
[self animateReleaseTouch:[touch view] withLocation:location];
Upvotes: 0