查找了网上资料,循环效果绝大部分都是增加行数来制造循环的错觉,延时滚动就是利用NSTimer间隔出发滚动事件来制造滚动效果。

代码:

#import <UIKit/UIKit.h>
#import "UserEntity.h"
 
@interface PickerViewController : UIViewController
{
    NSTimer *times;
    int j;//设置延时滚动的行数
}
 
@property (retain,nonatomic) UserEntity *userEntity;
@property (strong,nonatomic) NSMutableArray *pickerData;//设置可变数组存放数据
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
 
- (IBAction)backButton:(id)sender;
 
@end

-------------------------------------------------------------------------------------------------------------------

@implementation PickerViewController
@synthesize pickerView;
@synthesize pickerData;
 
@synthesize userEntity;
 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
       return self;
}
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"min:%d,max:%d",self.userEntity.minNum,self.userEntity.maxNum);
    //设置滚动初始位置 等于 扩大行数后的位置(1000/2)
      j=500;
    int startNum= self.userEntity.minNum;
    int endNum =self.userEntity.maxNum;
    int value=endNum-startNum;
    
    NSMutableArray *numArray =[NSMutableArray arrayWithCapacity:value+1];
    for(int i=startNum;i<=endNum;i++)
    {
        NSString* str = [NSString stringWithFormat:@"%d",i];
        [numArray addObject:str];
        
    }
    self.pickerData = numArray;
    [pickerView selectRow:1000/2 inComponent:0 animated:NO];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
-(int)getRandomNumber:(int)from to:(int)to
{
    return (int)(from + (arc4random() % (to - from + 1)));
}
 
- (IBAction)backButton:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
 
- (IBAction)selectButton:(id)sender {
    times=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(scollPicker) userInfo:nil repeats:YES];
}
 
-(void)scollPicker
{
    j++;
    if(j%100==0)
    {
        
        [times invalidate];
        times=nil;
        
        int startNum= self.userEntity.minNum;
        int endNum =self.userEntity.maxNum;
        int value=endNum-startNum;
        int getNum = [self getRandomNumber:0 to:1000];
        
        [pickerView selectRow:getNum%value inComponent:0 animated:YES];
        [pickerView reloadComponent:0];
        
        
        NSInteger row = [self.pickerView selectedRowInComponent:0];
        NSString *selected = [self.pickerData objectAtIndex:row];
        
        NSString *title=[[NSString alloc] initWithFormat:@"You selected %@!", selected];
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:title message:@"Thanks for choosing." delegate:self cancelButtonTitle:@"You're Welcome" otherButtonTitles:nil,nil];
        [alert show];
        
       
        j=0;
      //  button.enabled=YES;
}

@implementation PickerViewController
@synthesize pickerView;
@synthesize pickerData;
 
@synthesize userEntity;
 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
       return self;
}
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"min:%d,max:%d",self.userEntity.minNum,self.userEntity.maxNum);
    //设置滚动初始位置 等于 扩大行数后的位置(1000/2)
    j=500;
    获取随机数范围
    int startNum= self.userEntity.minNum;
    int endNum =self.userEntity.maxNum;
    int value=endNum-startNum;
     
   //初始话数组并赋值
    NSMutableArray *numArray =[NSMutableArray arrayWithCapacity:value+1];
    for(int i=startNum;i<=endNum;i++)
    {
        NSString* str = [NSString stringWithFormat:@"%d",i];
        [numArray addObject:str];        
    }
   //绑定数据
    self.pickerData = numArray;
   //初始Picker位置
    [pickerView selectRow:1000/2 inComponent:0 animated:NO];
}
 
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
//获取随机数
-(int)getRandomNumber:(int)from to:(int)to
{
    return (int)(from + (arc4random() % (to - from + 1)));
}

@implementation PickerViewController
@synthesize pickerView;
@synthesize pickerData;
 
@synthesize userEntity;
 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
       return self;
}
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"min:%d,max:%d",self.userEntity.minNum,self.userEntity.maxNum);
    //设置滚动初始位置 等于 扩大行数后的位置(1000/2)
    j=500;
    获取随机数范围
    int startNum= self.userEntity.minNum;
    int endNum =self.userEntity.maxNum;
    int value=endNum-startNum;
     
   //初始话数组并赋值
    NSMutableArray *numArray =[NSMutableArray arrayWithCapacity:value+1];
    for(int i=startNum;i<=endNum;i++)
    {
        NSString* str = [NSString stringWithFormat:@"%d",i];
        [numArray addObject:str];        
    }
   //绑定数据
    self.pickerData = numArray;
   //初始Picker位置
    [pickerView selectRow:1000/2 inComponent:0 animated:NO];
}
 
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
//获取随机数
-(int)getRandomNumber:(int)from to:(int)to
{
    return (int)(from + (arc4random() % (to - from + 1)));
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
 
 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
   //扩大行数制造循环
    return 1000;
}
 
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    //求余赋值使扩大后的数与数组值 相同 ,制造循环效果
    return [pickerData objectAtIndex:(row%(self.userEntity.maxNum-self.userEntity.minNum+1))];
}
        
    }
    else
    {for (int i=0; i<3; i++) {
        [pickerView selectRow:j inComponent:0 animated:YES];
        [pickerView reloadComponent:0];
     //   button.enabled=NO;
    }
    }
 
}
 
 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
 
 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return 1000;
}
 
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [pickerData objectAtIndex:(row%(self.userEntity.maxNum-self.userEntity.minNum+1))];
}
可能有些理解有错,希望求指出指点。

[ios]新手笔记-。-UIPickerView 关于伪造循环效果和延时滚动效果的更多相关文章

  1. IOS UIScrollView + UIButton 实现segemet页面和顶部标签页水平滚动效果

    很长一段时间没有写博客了,最近在学习iOS开发,看了不少的代码,自己用UIScrollView和UIButton实现了水平滚动的效果,有点类似于今日头条的主界面框架,效果如下: 代码如下: MyScr ...

  2. [ios]利用alertView 插入数据都数据库。笔记

    利用alertView 插入数据都数据库 -(void)addItemToList { UIAlertView *alter=[[UIAlertViewalloc]initWithTitle:@&qu ...

  3. [IOS]Swift 遍历预制的本地资源文件

    我事先放了一堆svg文件,但是我是批量使用的,想要直接遍历他们加入到一个list中来,那我直接就遍历他们的名称,把他们的名字组成一个array. var ss:NSString = NSBundle. ...

  4. [ios]关于用FMDB 操作数据库 删除 tableView 后刷新

    刚了解使用fmdb,从数据库获取数据 绑定到一个可变数组classNameItems //从ClassList表取得数据 FMResultSet *classInfo=[db executeQuery ...

  5. 代码规范[iOS]

    一.文件系统 二.代码结构 三.常量  宏 四.布局方式  数据持久化 五.网络 六.类目.延展 七.内存.线程 八.其他 九.版本控制.上线相关 十.附录1(公共库) 十一.附录2(公共类目) 十二 ...

  6. [ios]quartz2d画板功功能实现核心代码

    //触摸开始 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //    1,获取对 ...

  7. iOS10收集IDFA,植入第三方广告[终结]--ADMob

    [PS: 前段时间,公司做ASO推广,需要在应用中收集IDFA值,跟广告平台做交互!于是有了这个需求--] 1.首先,考虑了一下情况(自己懒 -_-#),就直接在首页上写了一个Banner,循环加载广 ...

  8. Java基础 之软引用、弱引用、虚引用 ·[转载]

    Java基础 之软引用.弱引用.虚引用 ·[转载] 2011-11-24 14:43:41 Java基础 之软引用.弱引用.虚引用 浏览(509)|评论(1)   交流分类:Java|笔记分类: Ja ...

  9. 给iOS开发新手送点福利,简述文本属性Attributes的用法

    给iOS开发新手送点福利,简述文本属性Attributes的用法   文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSF ...

随机推荐

  1. Content-disposition

    今天查看Struts2的文件上传部分 发现有个例子开头打印的信息中有Content-Disposition,一时好奇,所以了解了一下.顺便学习一下文件上传所需要的注意事项. Content-dispo ...

  2. 你不知道的Spring配置文件

    Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的"图纸".Java EE程序员必须学会并灵活应用这份"图纸&quo ...

  3. [转]阿里巴巴数据库连接池 druid配置详解

    一.背景 java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池.数据库连接池有很多选择,c3p.dhcp.proxool等,druid作为一名后起之秀,凭借其出色 ...

  4. iOS推送小结(证书的生成、客户端的开发、服务端的开发)

    1.推送过程简介 1.1.App启动过程中,使用UIApplication::registerForRemoteNotificationTypes函数与苹果的APNS服务器通信,发出注册远程推送的申请 ...

  5. JPA快速入门(自用)

    该案例为JPA的实体映射案例,将该项目解压导入到myeclipse中即可使用,然后直接使用hibernate即可! 文件地址:files.cnblogs.com/mrwangblog/JPA-Hibe ...

  6. PHP之session与cookie

    1.session与cookie的关系 众所周知,session是存储在服务器端,cookie是存储在客户端,如果禁用了浏览器的cookie功能,很多时候(除非进行了特殊配置)服务器端就无法再读取se ...

  7. IOS __ 面试题

    1.下面四种内部排序算法中哪一种在最差的情况下时间复杂度最高:(B) A.快速排序 B.冒泡排序 C.堆排序 D.归并排序   2.Shell中,将command1的输出作为command2的输入应该 ...

  8. 用markdown简化书写

    引言 markdown是什么, 谁在使用markdown, 以及为什么我应该/不应该使用它? 我能从这篇文章中了解到什么, 怎么开始使用? markdown有两个含义: 一套标记语法(类比html,w ...

  9. ubuntu 14.04下spark简易安装

    java安装 首先需要上oracle的网站下载 在lib目录下建立一个jvm文件夹 sudo mkdir /usr/lib/jvm 然后解压文件到这个文件夹 sudo tar zxvf jdk-8u4 ...

  10. Java 之 常用类(一)

    1.字符串: a.分类:String.StringBuffer.StringBuilder b.特殊:①String是唯一一个可以直接用常量赋值的引用数据类型 ②String的常量也是一个对象 (即 ...