увеличить значение, в то время как Uibutton продолжает нажимать в iphone - PullRequest
1 голос
/ 23 марта 2012

Я пытаюсь увеличить значение переменной, пока нажата кнопка.Но когда пользователь оставляет кнопку, увеличение значения переменной будет остановлено.

Я пытался использовать потоки с касанием и касанием, но не смог заставить его работать.

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

Интересно, есть ли альтернативный способ сделать это или нет.Это кажется очень простым, но не может придумать никакого другого решения, кроме этого.

1 Ответ

2 голосов
/ 23 марта 2012

Гораздо проще использовать 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];
}

Примечание. Предыдущий код приведен только для справки, например, я не управлял памятью.

...