检测移动的UILabel中的触摸有问题

时间:2010-10-31 23:20:22

标签: iphone objective-c

我有一个UILabel(MYLabel)的子类,我将MYLabel的实例添加到我的主ViewController的视图中,然后使用[UIView beginAnimations:]和[UIView commitAnimations]动画它们。

如果我有一个MYLabel的静态实例,它可以被触摸并调用touchesBegan:但是当它们是动画时,它们无法识别触摸。

MYLabel有一个touchesBegan:方法应该做的事情,我的猜测是我的MainViewController需要覆盖touchesBegan来检查触摸是否与MYLabel的实例相交,如果是,请调用MYLabel的touchesBegan:明确。

听起来我正朝着正确的方向前进吗?这似乎有点过于复杂,我找不到任何试图做类似事情的好例子,尽管我相信许多应用程序正在做的事情很简单。

-Begin Updates

以下是我用来添加和动画MYLabel的代码。我想我将需要以不同的方式为它们制作动画......

- (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];

我希望这有助于澄清事情。我想我可能只是改变了在这个应用程序中处理交互的方式......

1 个答案:

答案 0 :(得分:1)

我认为 - 正如我记得的那样 - 它可能与UIViews“框架”的价值有关,并不是你在动画正在进行时所期望的那样 - 这可能会弄乱触摸检测

如果你真的需要这样做 - 你可能做的一件事就是在NSTimer调用中手动完成动画移动。这将确保UIView框架始终处于可预测的,一致的状态(即永远不会在UIView“Animiation”的中间) - 并且触摸应该起作用。

相关问题