2009-03-31 2 views
2

UIPickerView가 있는데 selectRow 애니메이션이 완료되면 알림을 받고 싶습니다. selectRow 애니메이션이 완료되면 UIPickerView에서 콜백을 얻는 방법은 무엇입니까?

내가 UIPickerView에 대한 참조가 내보기 컨트롤러에 다음과 같은 접근 방식을 시도하고 작동하지 않습니다 :

[picker selectRow:random() % pickerDataCount inComponent:0 animated:YES]; 
:

-(void)viewDidLoad 
{ 
    ... 
    [UIPickerView setAnimationDelegate:self]; 
    [UIPickerView setAnimationDidStopSelector:@selector(animationFin ished:finished:context]; 
    ... 
} 

- (void)animationFinishedNSString *)animationID finishedBOOL)finished contextvoid *)context 
{ 
    if (finished) { 

    } 

} 

을 그리고 어딘가에 내 코드에서, 나는 애니메이션을 시작합니다

답변

2

메소드 호출을 beginAnimations/commitAnimation 블록에 중첩시켜야합니다. 당신이 관심이 구성 요소 & 행에 대한보기를 요청할 때

- (void) animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context { 
    NSLog(@"Here I am"); 
} 


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 

    [UIView beginAnimations:@"1" context:nil]; // nil = dummy 
    [UIPickerView setAnimationDelegate:self]; 
    [UIPickerView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)]; 
    [myPickerView selectRow:0 inComponent:0 animated:YES]; // jump with any swipe in picker always to row=0 as dummy to initiate animation 
    [UIView commitAnimations]; 

    //...whatever comes in addition... 
} 
+0

적어도 iOS8에서 작동하지 않습니다. – jomafer

1

당신은 viewForRow 에서 자기에게 알림을 게시 할 수 있습니다.

행 & 구성 요소를 속성 으로 잡고 selectRow를 호출하기 전에 설정해야합니다. 그리고 viewForRow에

((구성 == 자기 성분] & & (행 == [자기 열]) 제가 언급 다른 대답 중에서 혼합하여 그것을 해결자가

1

에 통지를 게시하는 경우 여기. 동작이는 스크롤 완료 될 때까지 기다린 후 선택된 값을 절약 할 수 있다는 것입니다.

  1. 가 스크롤 상태를 저장하고이 상태를 저장해야 두 개의 변수를 만듭니다. didSet에서 확인할 것 선택 도구가 스크롤되는 동안 저장 버튼을 누른 경우 ing. 그렇다면 선택기가 스크롤을 마친 후에 저장을 호출하십시오.

    var shouldSave = false 
    var pickerViewIsScrolling = false { 
        didSet { 
         if !pickerViewIsScrolling && shouldSave { 
          save() 
         } 
        } 
    } 
    
  2. 는 픽커 스크롤되면 인식 피커의 viewForRow 방법 pickerViewIsScrolling = true을 추가한다. 피커가 피커의 didSelectRowpickerViewIsScrolling = false를 추가 스크롤을 중지 한 경우
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { 
        pickerViewIsScrolling = true 
        ... 
    } 
    
  3. 인식합니다. 당신의 save() 기능에

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
        pickerViewIsScrolling = false 
        ... 
    } 
    
  4. 스크롤되는 피커 어떠했는지를 확인하기 위해 다음을 추가 (및 저장이 중지 후) 또는 스크롤되지 않고 직접 저장합니다. 마지막으로

    func save() { 
        if(pickerViewIsScrolling){ 
          shouldSave = true 
          return 
        } 
    
        // Save the Data... 
    
    } 
    
  5. 그리고

    viewDidAppear 기능 초기 생성 된 뷰의 pickerViewIsScrolling = true을 잡기 위해이 줄을 추가합니다.

    override func viewDidAppear(_ animated: Bool) { 
    
        super.viewDidAppear(animated) 
    
        pickerViewIsScrolling = false 
    
        ... 
    
    } 
    

이 나를 위해 잘 작동합니다. 또한 저장을 누른 상태에서 스크롤이 끝나기를 기다리는 동안 버튼의 비활성화를 구현했습니다. 따라서 사용자가 스크롤이 멈출 때까지 아무 일도 일어나지 않는 이유를 혼동하지 않습니다.

관련 문제