如何降低PickerView中选择行的速度? [英] How do I decrease the speed in which a row is selected in a PickerView?

查看:201
本文介绍了如何降低PickerView中选择行的速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是创建一个slotmachine,其中行逐个旋转,他们需要一个接一个地停止旋转。然而,为了使它看起来不错,行需要至少旋转3秒。我认为PickerView是最好的选择,因为我不知道如何以不同的方式使用它。

My goal is to create a slotmachine, in which the rows are spinning and one by one, they need to stop spinning, one by one. However, to make it look nice, the rows needs to spin atleast like 3 seconds. I think PickerView is the best option for this, since I have no idea on how to make this work in a different way.

当这是我的代码:

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

PickerView将进入第99行,但是在1秒内。如何控制第二个(并扩展选择行过程)?一个条件是它应该看起来不错,感觉你在玩老虎机。我试过这个:

The PickerView will go to row 99, but in 1 second. How can I control this second (and extend the selecting row process)? One condition is it should look nice and feel like you are playing a slot machine. I tried this:

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

但这不起作用。

谢谢。

推荐答案

Swift版本:

Swift version:

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

这篇关于如何降低PickerView中选择行的速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆