ios UIPickerView 技巧集锦

重新实现 UIPickerView

参考资料:

http://www.cocoachina.com/bbs/read.php?tid=85374

http://www.cocoachina.com/iphonedev/toolthain/2011/1205/3663.html

 

设置循环滚动

设置 UIPickerView 的数据源数量为很大的规模, 取数据时对行数进行取模, 从而实现循环滚动的效果.

在每次滚动结束后, 把当前选这个行无动画滚动到数据的中间, 减少用户滚动到数据边界的可能.

参考资料:

http://www.cocoachina.com/bbs/read.php?tid=27023

http://code4app.com/codesample/4f67fbb96803fa833f000002

http://blog.csdn.net/ipromiseu/article/details/7436521

 

修改滚动速度

比较困难. 需要继承 UIPickerView, 并实现未公开函数 

- (double)scrollAnimationDuration

参考资料:

http://iphonedevelopment.blogspot.com/2009/01/longer-spinning-blurring.html

需要FQ, 把原文章复制过来.

Longer Spinning & Blurring

I can't tell you how many times I've been asked lately how to do the "UrbanSpoon Effect".

Note: There is a newer version of this code available here.


Now, I have no idea exactly how the folks at UrbanSpoon did their application, but they did it well. I don't have time for a full tutorial right now, or to do a full implementation of the effect, but in the process of helping somebody in a forum, I did write a little sample application that shows the basic theory of how to make your UIPickerView go round and round for longer. 

As I said, this project nowhere near as polished as what UrbanSpoon does - I cheated on the blurring because I hit some bugs while porting my NSImage convolution kernel code to work with UIImage and I don't have the time to debug something that gnarly right now. My "blur" is just a double resize - down then back up. It's really just interpolation, but it sorta makes it blurry. I probably wouldn't use that method of blurring in a real application but, then again, I'd be getting paid for a real application. 



This application does support using the accelerometer to start the spin. In order to make the picker view spin longer, I subclassedUIPickerView. My subclass had exactly one method. This is an undocumented method that returns the duration of the spin, so you just return the number of seconds you want your spin to go for:

#import "JLPickerView.h"

@implementation JLPickerView
- (double)scrollAnimationDuration
{
return 5.0;
}
@end


The other key thing to note in this application is that my UIPickerViewDataSource lies about how many rows each component has. I have a constant called kRowMultiplier. I take the count of the array that corresponds to a particular component, and I multiply it by kRowMultiplier. This value is currently set to 100, so if the array that feeds a component has ten items, my datasource lies and says there are a thousand, and then it just keeps feeding the same ten items over and over in the same order by calculating the actual row in the array to use:

int actualRow = row%[[self arrayForComponent:component] count];

This creates a component with the same values repeated over and over.

When the view first appears, I load arrays with views containing both the blurred and unblurred data. You don't want to be programmatically blurring the values over and over when the spin happens, so we do it once and store the values in arrays. I then set the component's row to a value in the middle. This means that the user will never see the blank rows above and below the real values, which helps create the illusion of endless spinning.
[picker selectRow: [component1Data count] * (kRowMultiplier / 2) inComponent:0 animated:NO];

Now, when a value is selected in the picker, I do a bait-and-switch back to the middle. I move the component to the same value they're currently on, but back in the middle of the wheel. This way, no matter what happens, they'll never see the blank rows at the top or bottom. Since I'm moving them to the same value and the surrounding values are the same, and I tell it not to animate the change, the user has no idea it has happened.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSArray *componentArray = [self arrayForComponent:component];
int actualRow = row%[componentArray count];

int newRow = ([componentArray count] * (kRowMultiplier / 2)) + actualRow;
[picker selectRow:newRow inComponent:component animated:NO];

}

Finally, when the spin button is pressed, or the user shakes the phone, I calculate a random number for each component. This number is a value between 0 and the size of the array minus 1. This tells me what the new, randomly selected value will be. Then I move the components (again, without animating) to the corresponding row near the top or bottom of the dial, and then animate to the selected value at the other end of the wheel.
- (IBAction)spin
{
if (! isSpinning)
{
// Calculate a random index in the array
spin1 = arc4random()%[component1Data count];
spin2 = arc4random()%[component1Data count];
spin3 = arc4random()%[component1Data count];

// Put first and third component near top, second near bottom
[picker selectRow:([picker selectedRowInComponent:0]%[component1Data count]) + [component1Data count] inComponent:0 animated:NO];
[picker selectRow:(kRowMultiplier - 2) * [component2Data count] + spin2 inComponent:1 animated:NO];
[picker selectRow:([picker selectedRowInComponent:2]%[component3Data count]) + [component3Data count] inComponent:2 animated:NO];

// Spin to the selected value
[picker selectRow:(kRowMultiplier - 2) * [component1Data count] + spin1 inComponent:0 animated:YES];
[picker selectRow:spin2 + [component2Data count] inComponent:1 animated:YES];
[picker selectRow:(kRowMultiplier - 2) * [component3Data count] + spin3 inComponent:2 animated:YES];

isSpinning = YES;

// Need to have it stop blurring a fraction of a second before it stops spinning so that the final appearance is not blurred.
[self performSelector:@selector(stopBlurring) withObject:nil afterDelay:4.7];
}
}


I set a variable called isSpinning so that I know whether to provide blurred or unblurred data to the spinner. I have the spin set for 5 seconds, so 4.7 seconds in the future, I set the value back to NO. This cause it to stop blurring a moment before the spinning stops. This is done so that the final words displayed will not be blurred.

You can download the sample project here. 

One note, though: I have used a couple of undocumented, private methods in this project. I ordinarily shy away from doing that, but in the sake of getting this code out the door quickly, I did. I use one undocumented method for resizing the UIImage in order to create the fake blur effect, and another one to keep the picker from making sounds when I initially set the values because I don't want the user to realize I'm moving the picker - that would shatter the illusion. These are pretty subtle uses of undocumented methods so I doubt they'd get your application rejected, but they could. They're also not methods that are likely to change. But, again, they could - that's always a risk with undocumented methods, so caveat emptor - use at your own risk. If you want to play it safe, to a real motion blur and take out the calls to UIPickerView's setSoundsEnabled: method.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/404034.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Delphi工具之TDUMP

TDUMP是一个命令行程序,它输出.exe或.dll(以及其他文件类型)文件的结构。 TDUMP位于Delphi安装目录下的Bin目录里,如下: 缺省时,TDUMP的输出显示到屏幕上; 用户可以将TDUMP的输出定向到一个文本…

libevent的使用(socket)

From: http://blog.csdn.net/kaitiren/article/details/35253319 这篇文章介绍下libevent在socket异步编程中的应用。在一些对性能要求较高的网络应用程序中,为了防止程序阻塞在socket I/O操作上造成程序性能的下降,需要使用异步编程,即程序…

消费者承担消费税真的吃亏了吗?

像小老鼠一样享受,才不管消费税呢其实,我本来对经济学不感兴趣。一次偶然的机会,我在朋友的寝室里看到了传说中经济学最经典的教材之一——曼昆(Mankiw)的《经济学原理》。好奇心驱使我随手翻开了一页,读了…

cocos2dx libevent简介和使用

From: http://blog.csdn.net/kaitiren/article/details/35254577 libevent是一个基于事件触发的网络库,memcached底层也是使用libevent库,今天学习下。总体来说,libevent有下面一些特点和优势:* 统一数据源, 统一I/O事…

Linux查看主板的相关信息

一条命令就能知道主板的一些信息,具体的内容就无需解释了,诸如厂商啊什么的,英文词的借助Google吧,哈哈 转载于:https://blog.51cto.com/kumu1988/1086248

在mac上配置cocos2d-x开发环境

From: http://www.cnblogs.com/xiaodao/archive/2013/01/08/2850751.html 一、下载cocos2d-x最新安装包 在终端中cd到本地将要存放目录,执行git命令 git clone https://github.com/cocos2d/cocos2d-x.git 二、如果开发ios程序,需要配置xcode模板 下好…

ASP.NET笔记(三)

一、使用主题自定义网站&#xff08;App_Themes&#xff0c;<Page Theme/StyleSheetTheme..>&#xff0c;<page theme"">&#xff09; 创建主题并将其应用于页 在 Visual Web Developer 中&#xff0c;右击网站名&#xff0c;单击“添加 ASP.Net 文件夹”…

磁盘IO:缓存IO与直接IO

文件系统IO分为DirectIO和BufferIO,其中BufferIO也叫Normal IO。 1. 缓存IO 缓存I/O又被称作标准I/O&#xff0c;大多数文件系统的默认I/O操作都是缓存I/O。在Linux的缓存I/O机制中&#xff0c;数据先从磁盘复制到内核空间的缓冲区&#xff0c;然后从内核空间缓冲区复制到应用程…

F#初学笔记08

惰性求值惰性求值也叫按需调用&#xff0c;是一个演算策略&#xff0c;延迟一个表达式的演算&#xff0c;直到需要用到演算值的时候再演算&#xff0c;同时也避免了重复演算。惰性求值的好处包括&#xff1a;避免不必要的计算&#xff0c;提升性能。可以构造以构造一个无限的数…

在Finder标题栏上显示完整路径

From: http://www.7do.net/resources-5411-1-1.html 打开终端&#xff0c;输入以下命令并回车&#xff1a; defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES 然后再把finder关了再打开&#xff0c;你会发现路径栏变成这个样子了&#xff1a; 其实呢&a…

Wamp5 配置PHP 图文详解(转)

Wamp5论坛配置图文版 知识扫盲&#xff1a; 1、WampSever指的是apache mySQL PHP三合一套装&#xff0c;第一字母W&#xff0c;是指用于windows系统&#xff0c;我用的是2.0f版。用于Linux系统的&#xff0c;是LampSever&#xff0c;第一字母是L。 下载地址http://jaist.dl.s…

9个小窍门让OS X中Finder用起来更顺手

From: http://digi.tech.qq.com/a/20130309/000051.htm 腾讯数码讯&#xff08;编译&#xff1a; 李斯特&#xff09;Finder是OS X系统上用户与文件系统打交道的主要途径之一&#xff0c;它的默认设置是能满足普通用户绝大多数日常需求的。但我们同样可以通过一些小配置来使它…

xcode-select: error: tool 'xcodebuild' requires Xcode错误解决方法

From: http://blog.csdn.net/jymn_chen/article/details/21613745 因为机子里有两个Xcode&#xff0c;所以分别重命名了&#xff0c;但是在运行一个MakeFile时却报了以下错误&#xff1a; [plain] view plaincopyxcodebuild -target "GHUnitIOS (Device)" -configu…

利用Bdrive打造个人私有云存储解决方案

Bdrive 一款私有云储存软件&#xff0c;可以自己方便的在 Mac/Windows 下架设服务器&#xff0c;并可以通过 PC、Mac、iOS、Android 跨平台使用。以下简单介绍一下利用Bdrive来完成个人私有云存储解决方案。 第一步&#xff0c;搭建Bdrive云存储服务器 先下载Bdrive服务器程序&…

MVC路由中routes.IgnoreRoute({resource}.axd/{*pathInfo}) 到底什么意思!

转自&#xff1a;http://blog.csdn.net/lvjin110/article/details/24638913 参考&#xff08;1&#xff09; http://www.cnblogs.com/flyfish2012/archive/2013/02/01/2889184.html 我们在开发MVC当中&#xff0c;经常在我们的全局类的路由设置&#xff0c;看到这样的代码&…

未能加载文件或程序集“Autofac, Version=3.4.0.0,

遇到这个错误的时候&#xff1a;如下图 未能加载文件或程序集“Autofac, Version3.4.0.0, Cultureneutral, PublicKeyToken17863af14b0044da”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 只要在config加上 <runtime><assemblyBinding xmlns"…

浅谈关于SRAM与DRAM的区别

从名字上看&#xff0c;SRAM与DRAM的区别只在于一个是静态一个是动态。由于SRAM不需要刷新电路就能够保存数据&#xff0c;所以具有静止存取数据的作用。而DRAM则需要不停地刷新电路&#xff0c;否则内部的数据将会消失。而且不停刷新电路的功耗是很高的&#xff0c;在我们的PC…

字符串系列之最长回文子串

2019独角兽企业重金招聘Python工程师标准>>> 问题描述&#xff1a; 给定一个字符串SA1A2...An&#xff0c;要求找出其最长回文子串&#xff08;Longest Palindromic Substring&#xff09;。所谓回文子串就是S的某个子串Ai...Aj为回文。例如&#xff0c;对字符串Sab…

Host SMBus controller not enabled的解决方法

From: http://blog.csdn.net/starmlk/article/details/7982077 SMBus 目录 SMBus与I2C的差别SMBus 是 System Management Bus 的缩写&#xff0c;是1995年由Intel提出的&#xff0c;应用于移动PC和桌面PC系统中的低速率通讯。它主要是希望通过一条廉价并且功能强大的总线&…