2012-03-23 1 views
1

uibutton을 계속 누르고있는 동안 변수 값을 높이려고합니다. 그러나 사용자가 버튼을 떠날 때 변수 값의 증가는 멈 춥니 다.아이폰에서 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

UIStepper를 참조하십시오. 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]; 
} 

참고 : 이전 코드는 참조 용이며 예를 들어 메모리 관리를하지 않았습니다.