使用touchesBegan完全执行操作

时间:2012-05-12 19:30:46

标签: objective-c uiimageview touchesbegan

我正在使用 - (void)touchesBegan ...告诉用户是否在屏幕上的任何位置点按了。一旦用户点击屏幕,就执行一个动作。然而,每当该人举起手指时,动作就会停止。具体来说,当用户点击时,我正在运行imageView.animateImages。我想这样做,如果他们点击并放手,动画将继续运行(我将repeatCount设置为1)。有谁知道如何做到这一点?提前谢谢!

1 个答案:

答案 0 :(得分:0)

您应该使用NSNotifications。尝试这样的事情:

...

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; 
[nc addObserver:self selector:@selector(nameOfAnimationMethod) name:@"touched" object:nil];

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [nc postNotificationName:@"touched" object:self]; 
} 

让我告诉你这是做什么的。它创建一个通知中心,并将当前对象作为观察者添加到通知中心,监听“触摸”通知。当通知中心发布所述通知时,该对象运行方法(选择器)“nameOfAnimationMethod”。如果你看一下touchesBegan方法覆盖,你会看到我发布了一个名为“被触摸”的通知。这将触发对象运行您想要的方法。