2012-03-23 10 views
1

uibuttonを押し続けている間に変数の値を増やそうとしています。しかし、ユーザーがボタンを離れると、変数の値の増加が停止します。iphoneを押したままuibuttonを押している間に値を増やす

私はタッチダウンとタッチアップでスレッドを使用しようとしましたが、それを動作させることはできませんでした。

-(void) changeValueOfDepthFields:(UIButton *)sender { 
    if (pressing) 
     pressing = NO; 
    else 
     pressing = YES; 

    pressingTag = 0; 

    while (pressing) { 

    [NSThread detachNewThreadSelector:@selector(increaseValue) toTarget:self withObject:nil]; 
    } 
} 

- (void) stopValueChange:(UIButton *)sender { 

    pressing = NO; 
} 


[fStopUp addTarget:self action:@selector(changeValueOfDepthFields:) forControlEvents:UIControlEventTouchDown]; 
[fStopUp addTarget:self action:@selector(stopValueChange:) forControlEvents:UIControlEventTouchUpInside]; 


- (void) increaseValue { 

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

    fstopVal = fstopVal + 0.1; 

    [self performSelectorOnMainThread:@selector(changeTextOfValues) withObject:nil waitUntilDone:YES]; 
    [pool release]; 
} 


- (void) changeTextOfValues { 
    fStopField.text = [NSString stringWithFormat:@"%.02f", fstopVal]; 
} 

これに代わる方法があるかどうかは疑問です。それは非常に単純なようですが、これ以外の解決策は考えられませんでした。

+0

以下の機能をチェックしてください。むしろそれほどコードを書くのを手伝って、他の最適化が必要かどうかを教えてください。 – Kuldeep

+0

「タッチアンドホールド」が正しいジェスチャーであるかどうかを検討する必要があります。 iOSユーザーは、上下左右にスワイプしたり、タッチして保持したり、マウスジェスチャーのほうがタッチして保持するのではなく、そのような値を調整するために上下にスライドするために使用されます。 –

+2

を参照してください。 AppleはUIControlを「タッチアンドホールド」しました。もっと正確にすることはできません。 –

答えて

2

NSTimerを使用する方がはるかに簡単です。

- (void)changeValueOfDepthFields:(UIButton *)sender 
{ 
    if (!self.timer) { 
     self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(increaseValue) userInfo:nil repeats:YES]; 
    } 
} 

- (void)stopValueChange:(UIButton *)sender 
{ 
    if (self.timer) { 
     [self.timer invalidate]; 
     self.timer = nil; 
    } 
} 

- (void)increaseValue 
{ 
    fstopVal = fstopVal + 0.1; 
    fStopField.text = [NSString stringWithFormat:@"%.02f", fstopVal]; 
} 

注:前のコードは参考用です。私は例としてメモリ管理を行いませんでした。

関連する問題