我的应用有些问题。因此,当手指移出被触摸的物体(按钮)时,我需要停止NStimer。所以有一个代码:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
for (UITouch *touch in allTouches)
{
CGPoint location = [touch locationInView:touch.view];
if ([testButton.layer.presentationLayer hitTest:location]) {
timer = 60;
time = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(randomVoid) userInfo:nil repeats:YES];
} else if (![testButton.layer.presentationLayer hitTest:location]){
[time invalidate];
}
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[time invalidate];
}
感谢您的帮助!
答案 0 :(得分:0)
touchesBegan
事件。如果你移动手指,每次移动都会调用touchesMoved
,当你抬起手指时,会调用touchEnd
。
因此,基本上你应该添加用于测试的代码,如果需要按钮被touchMoved
方法触及。
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (![testButton.layer.presentationLayer hitTest:location]){
[time invalidate];
time = nil;
}
}