2014-02-06 53 views
-1

我正在写一个iOS应用程序,我似乎无法弄清楚如何做连续触摸事件。我尝试使用“touchesBegan”和“touchesEnd”功能,但这些功能不适用于连续触摸。连续触摸的iOS事件

所以基本上我现在所拥有的是如下:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesBegan:touches withEvent:event]; 

    UITouch *touch = [touches anyObject]; 
    if([touch view] == [self viewWithTag:kTag]) 
    { 
     CGFloat yOffset = contentView.contentOffset.y; 
     yOffset ++; 
     [contentView setContentOffset:CGPointMake(0, yOffset)]; 
    } 
} 

但是我想要的内容偏移无限期继续,只要转移我的手指触摸给定的视图。现在它在一次迭代之后停止。

+2

你在找什么?如果你碰到触碰,那么它是连续的,直到你收到touchesEnded。 – rmaddy

+0

^看看我上面的编辑 – user1855952

回答

1

看来你需要设置在touchesBegan重复的计时器。每次定时器触发时,更新偏移量。取消touchesEndedtouchesCanceled方法中的计时器。

+0

我从来没有听说过iOS中的重复计时器。你能详细说明吗?我将如何去实施一个? – user1855952

+0

查看“NSTimer”的文档。有些参数用于在创建计时器时设置重复。 – rmaddy

+0

很酷,谢谢我会检查出来 – user1855952

3

您是否在寻找touchesMoved方法?有这样一种方法,你可以使用它。

UPDATE

马迪的解决方案应该与触摸工作。

或者,你可能想看看下面的控制事件的方法:根据您的更新问题

- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event

+0

OP不希望'touchesMoved'方法。这个想法是用户只需将手指放在屏幕上,手指保持联系即可移动。无需移动手指。 – rmaddy

+0

谢谢Maddy。我已经同意我的回答听起来更像是一个评论,这本质上是试图了解需求。我只有1个声望,因此无法发表评论。话虽如此,我们可以在长按手势识别器中指定持续时间并利用它。更新了我的答案。 – Rajiv

+0

长按手势也不起作用。只有在达到长按持续时间后才会发送事件,然后随着手指的移动,手指抬起时发出事件。OP需要一整套手指在一个地方放置的事件。 – rmaddy

1

感谢Maddy的提示。在触摸有关NSTimer的信息时,我得到了重复的操作。

我确实有它的每个点击屏幕的时间做一个动作:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
[rocket.physicsBody applyForce:CGVectorMake(0,200)]; 
} 

但这只会每点击触发。我希望它在触摸屏幕时继续施加力量。

计时器添加到类:

@interface TPMyScene() 
@property (nonatomic, retain, readwrite) NSTimer * touchTimer; 
@end 

移动我的行动的方法:

-(void)boost { 
    NSLog(@"Boosting"); 
    [rocket.physicsBody applyForce:CGVectorMake(0,200)]; 
} 

触发计时器在的touchesBegan:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    // First we need to trigger a boost in case the screen was just touched. 
    [rocket boost]; 

    //set a timer to keep boosting if the touch continues. 
    //Also check there isn't already a timer running for this. 
    if (!self.touchTimer.isValid) { 
     self.touchTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:rocket selector:@selector(boost) userInfo:nil repeats:YES]; 
    } 
} 

取消计时器触摸时以结束或取消结束:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self.touchTimer invalidate]; 
} 

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self.touchTimer invalidate]; 
} 
+0

我的第一次触动开始只有计时器有一个错误,一旦我开始测试... 1.计时器只在0.1秒后开火,所以触摸/在任何跑步之前取消。 2.多点触摸:第二次触摸会创建一个新的定时器,并失去原来的链接。无效只会阻止新的一个,旧的永远持续发射! – TPot