2017-05-02 1 views
8

나의 목표는 행이 회전하고 하나씩 회전하는 슬롯 머신을 만드는 것입니다. 회전을 하나씩 중단해야합니다. 그러나보기 좋게하려면 행을 3 초 이상 움직여야합니다. 필자는 PickerView가 다른 방법으로이 작업을 수행하는 방법을 모르기 때문에 PickerView가 가장 좋은 옵션이라고 생각합니다.PickerView에서 행이 선택되는 속도를 줄이려면 어떻게해야합니까?

이 내 코드 인 경우 :

self.slotMachine.selectRow(99, inComponent: 1, animated: true) 

PickerView은 99 행으로 이동하지만, 1 초 것입니다. 이 두 번째를 제어하고 행 선택 프로세스를 확장하는 방법은 무엇입니까? 하나의 조건은 멋져 보이며 슬롯 머신을 플레이하고있는 것처럼 느껴져야합니다. 나는 이것을 시도했다 :

UIView.animate(withDuration: 3.0, delay: 0, animations: {() -> Void in 
     self.slotMachine.selectRow(99, inComponent: 1, animated: true) 
    }, completion: nil) 

그러나 이것은 작동하지 않았다.

감사합니다.

+0

체크 아웃 HTTP : //stackoverflow.com/questions/3832474/uitableview-row-animation-duration-and-completion-callback 도움이 될만한 것을 찾을 수 있습니다. – rmaddy

+0

* "그러나 이것은 효과가 없었습니다."* 분명히 말하면서,하지만 ... 무슨 일이 있었습니까? 왜 작동하지 않았습니까? 그리고 "3 초가 아니라 1 초"라고 말할 예정이라면 아마도 UIPickerView를 하위 클래스로 분류해야하고 최악의 경우 자신의 설계를해야합니다. – dfd

+0

이 cocoacontrol을 보시오 : https://www.cocoacontrols.com/controls/zcslotmachine –

답변

1

당신은 타이머를 사용하고 같은 하나를 사용하여 세포 하나를 선택할 수 있습니다 :

var timer = Timer() 
var currentRow = 0 

override func viewDidLoad() { 
    super.viewDidLoad() 
    timer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true) 
} 

func timerAction() { 
    currentRow += 1 
    self.slotMachine.selectRow(currentRow, inComponent: 1, animated: true) 
    if(currentRow == 99){ 
     timer.invalidate() 
    } 
} 
+0

FYI - 이것은'UITickView'가 아니라'UIPickerView'에 관한 것입니다. – rmaddy

+0

감사합니다. 고쳤다. –

+0

네,이 방법이 효과가있을 수는 있지만 ...별로 좋지 않습니다. 나는 내 질문을 편집 할 것이다. –

1

스위프트 버전 :

import UIKit 

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { 

    var picker: UIPickerView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     picker = UIPickerView(frame: CGRect(x: 0, y: 100, width: 100, height: 375)); 
     view.addSubview(picker) 
     picker.dataSource = self 
     picker.delegate = self 

     let button = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 100)) 
     button.backgroundColor = .red 
     button.addTarget(self, action: #selector(trigger), for: .touchUpInside) 
     view.addSubview(button) 
    } 

    func trigger() { 
     let timer = Timer.scheduledTimer(timeInterval: 0.25, target: self, selector: #selector(scrollRandomly), userInfo: nil, repeats: true); 
     //call the block 3 seconds later 
     DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(3*NSEC_PER_SEC))/Double(NSEC_PER_SEC)) { 
      timer.invalidate() 
      //always select 500 finally 
      self.picker.selectRow(500, inComponent: 0, animated: true) 
     } 
    } 

    func scrollRandomly() { 
     let row:Int = Int(arc4random() % 1000); 
     picker.selectRow(row, inComponent: 0, animated: true) 
    } 


    func numberOfComponents(in pickerView: UIPickerView) -> Int { 
     return 1 
    } 

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
     return 1000 
    } 

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
     return row.description 
    } 
} 

OC 버전 :

#import "ViewController.h" 

@interface ViewController() <UIPickerViewDelegate, UIPickerViewDataSource> 
@property (weak, nonatomic) UIPickerView *picker; 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, 100, 375)]; 
    self.picker = picker; 
    [self.view addSubview:picker]; 
    picker.delegate = self; 
    picker.showsSelectionIndicator = true; 

    UIButton *b = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 100, 100)]; 
    b.backgroundColor = [UIColor redColor]; 
    [self.view addSubview:b]; 
    [b addTarget:self action:@selector(bbbb) forControlEvents:UIControlEventTouchUpInside]; 
} 

- (void)bbbb { 


    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.25 repeats:true block:^(NSTimer * _Nonnull timer) { 
     NSInteger row = arc4random()%1000; 
     [self.picker selectRow:row inComponent:0 animated:true]; 
    }]; 

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
     [timer invalidate]; 
     [self.picker selectRow:500 inComponent:0 animated:true]; 
    }); 

} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 
    return 1000; 
} 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 
    return 1; 
} 


- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 
    return [NSString stringWithFormat:@"%ld",row]; 
} 

@end 
관련 문제