views:

23

answers:

1

I have a subclass of UILabel (MYLabel), and I'm adding instances of MYLabel to my Main ViewController's View and then animating them (using [UIView beginAnimations:] and [UIView commitAnimations].

If I have a static instance of my MYLabel, it has no problem being touched and calling touchesBegan: but while they are animating, they aren't recognizing touches.

MYLabel has a touchesBegan: method that is supposed to do things, and my guess is that my MainViewController needs to override touchesBegan to check to see if the touch is intersecting an instance of MYLabel, and if it is, call MYLabel's touchesBegan: explicitly.

Does it sound like I'm going in the right direction? It seems a little too complicated, and I'm not finding any good examples that are trying to do something similar, despite my belief that this is a pretty simple thing to do that a lot of apps are doing.

-Begin Updates

Here is the code I'm using to add and animate MYLabels. I'm thinking I'm going to need to do animate them in a different way...

- (void)onTimer
{
label = [[MYLabel alloc] initWithFrame:CGRectZero];

int xValue = 380;
int yValue = (arc4random() % 400) +1;
double speed = 4.0;

int randomWord = (arc4random() % [[dictionarySingleton wordArray] count]);

UIFont *font = [UIFont systemFontOfSize:22];

[label setText:[[dictionarySingleton wordArray] objectAtIndex:randomWord]];
[label setFont:font];
[label sizeToFit];
[label setFrame:CGRectMake(xValue, yValue, [label frame].size.width, [label frame].size.width)];
[label setBackgroundColor:[UIColor clearColor]];
[label setIsAnimating:YES];
NSLog(@"Is Animating");
[[self view] addSubview:label];



[UIView beginAnimations:nil context:label];
[UIView setAnimationDuration:speed];
[label setFrame:CGRectMake(-120, yValue, [label frame].size.width, [label frame].size.width)];
[UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
  • (void)onTimer gets called inside viewDidAppear: like this:

    [NSTimer scheduledTimerWithTimeInterval:(0.7) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    

I hope that helps clarify things. I'm thinking I might just have change the way interaction is handled in this app...

Any help is greatly appreciated.

Thank you,

-Kagi

A: 

I think - as I recall - it could have to do with the value of a UIViews "frame" isn't quite what you'd expect it to be while an animation is in progress - and that could be messing up touch detection.

If you really need to do this - one thing you might do is to manually do the animation moves yourself in NSTimer calls. This would assure that the UIView frames were in a predictable, consistent state at all times (i.e. Never in the middle of a UIView "Animiation") - and the touches should work.

Brad