I have a UIImageView
which is in an animation loop. I want to detect if it has been touched and print out a message with NSLog
. The idea will be to perform another animation if it has been touched, but for the moment i can't detect if it has been touched in the first place. User interaction has been enabled. Here is the code:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
UIView *touchedView = [touch view];
if (touchedView == imgSun) {
NSLog(@"Sun touched");
[self spinSun];
} else if (touchedView == imgBee) {
NSLog(@"Bee touched");
} else if (touchedView == imgClouds) {
NSLog(@"Clouds touched");
}
}
The animation method:
-(void) beeBobbing
{
[UIView animateWithDuration:1.0f delay:0 options:(UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) animations:^{
CGPoint bottomPoint = CGPointMake(215.0, 380.0);
imgBee.center = bottomPoint;
} completion:^(BOOL finished) {
}];
}
This is probably because user integration is disabled by default during an animation.
See animateWithDuration:delay:options:animations:completion: documentation:
During an animation, user interactions are temporarily disabled for the views being animated. (Prior to iOS 5, user interactions are disabled for the entire application.) If you want users to be able to interact with the views, include the UIViewAnimationOptionAllowUserInteraction constant in the options parameter.
http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html1
You can probability just add UIViewAnimationOptionAllowUserInteraction to the value passed to options in your method 'beeBobbing'.