- 浏览: 53799 次
- 性别:
- 来自: 北京
首先感谢cocoa china的划向彼岸(http://www.cocoachina.com/bbs/read.php?tid-15816-keyword-%C5%DC%C2%ED%B5%C6.html)的sample
因为我的需求不同,需要多个view的跑马灯,那么就得修改,首先是多个view的问题,大概思路是两个动画,先让所有的view先从各自的初始位置开始移动,当origin.x到320就让它从0-width开始,第一个动画需用到结束函数,根据animationID取出view,再进行第二个动画,具体如下:
第一个动画,即从初始位置移动至320,animationID保存index,只一次动画
for(int i=0;i<320/originWidth+1;i++){ UIView *paomaView=[[UIView alloc] initWithFrame:CGRectMake(0-originWidth+i*originWidth, 0, originWidth, self.view.frame.size.height)]; paomaView.backgroundColor=[UIColor clearColor]; paomaView.userInteractionEnabled=YES; [self.view addSubview:paomaView]; [paomaViewArray addObject:paomaView]; UILabel *lab=[[UILabel alloc] initWithFrame:paomaView.bounds]; lab.text=[titleArray objectAtIndex:i]; lab.textColor=[UIColor whiteColor]; lab.font=[UIFont systemFontOfSize:15]; lab.backgroundColor=[UIColor clearColor]; lab.userInteractionEnabled=YES; [paomaView addSubview:lab]; [UIView beginAnimations:[NSString stringWithFormat:@"%d",i] context:NULL]; [UIView setAnimationDuration:speed*(320-paomaView.frame.origin.x)]; [UIView setAnimationCurve:UIViewAnimationCurveLinear]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(animDone:finished:context:)]; frame = paomaView.frame; frame.origin.x = 320; paomaView.frame = frame; [UIView commitAnimations]; [paomaView release]; }
第二个动画,无限循环
-(void)animDone:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context { UIView *paomaView=[paomaViewArray objectAtIndex:[animationID intValue]]; CGRect frame = paomaView.frame; frame.origin.x = 0-originWidth; paomaView.frame = frame; [UIView beginAnimations:@"animation1" context:NULL]; [UIView setAnimationDuration:speed*(320+originWidth)]; [UIView setAnimationCurve:UIViewAnimationCurveLinear]; [UIView setAnimationDelegate:self]; [UIView setAnimationRepeatAutoreverses:NO]; [UIView setAnimationRepeatCount:999999]; frame = paomaView.frame; frame.origin.x = 320; paomaView.frame = frame; [UIView commitAnimations]; }
以上作为显示信息是没问题的,但是用户需求一般不会仅限于此...他们会想--点击,好吧,把label换成button,问题就来了:在animation中button的初始和结尾的位置其实从开始就确定了,它只是加了一个duration来执行动画,这个运动的过程button的touch点击是不响应的...多次换关键词google,找到stack overflow上有人也有同样的问题(cocoa touch - iPhone UIView Animation Disables UIButton Subview - Stack Overflow),上面说make animation yourself...
好吧,就用NSTimer模拟animation:
UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame=paomaView.bounds; btn.tag=i; [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; [paomaView addSubview:btn]; [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(moveViewByTimer:) userInfo:paomaView repeats:YES];
timer的执行:
-(void)moveViewByTimer:(NSTimer *)timer{ UIView *view=[timer userInfo]; CGRect frame=view.frame; if(frame.origin.x>=320){ frame.origin.x=0-originWidth; view.frame=frame; } frame.origin.x+=1;//0.005/speed; view.frame=frame; NSLog(@"origin x is %f",view.frame.origin.x); }
值得注意的是timer的时间间隔和速度,发现:当TimeInterval小于0.01的时候运动会有问题,button不会平行运动,他们会不断贴近,重叠...估计是for循环时timer是先后创建的,
这之间有间隔,当TimeInterval小于这个间隔的时候,timer就不能看作是平行的了,当然这只是估计.另外文档上"貌似"也这样说:
Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds(不知道咋翻译才对...)
跑起来是没问题了,就某些细节还不能很好的理解(TimeInterval...),姑且用之吧
- Paoma.zip (32 KB)
- 下载次数: 14
发表评论
-
symbolicate小记
2014-05-22 16:46 441日志分析少不了symbolicate,一直都看的这 ... -
内存这玩意-1
2014-04-17 22:00 538来拔点草 今天看到个autorel ... -
优化table的图片加载
2013-06-14 10:08 685table的优化,貌似都说烂了,面试必考,网上也各种 ... -
拍照得到正确尺寸图片
2013-06-14 09:44 1657慢慢适应新环境,发点东西。 拍照有三条路子 ... -
最近知识点 1
2013-05-02 23:41 712今天是个好日子,入住新地方,深夜发帖纪念一下^^ ... -
Block再探
2013-04-13 11:18 900最近面试碰到一 ... -
NSSet的查找
2013-04-13 10:35 793看<Pro.iOS.Apps.Performa ... -
找工作前 自我总结一下
2013-03-04 19:21 1370KVC: 1.key-value-coiding 最 ... -
ASIHTTPRequest中的block
2012-12-24 16:38 1763先感慨一下,好深的坑... ASI很好用,特别 ... -
重写UIView 滑动手势的问题
2012-11-29 18:25 2682今天做个功能:tableview中,在某个cell上左右滑 ... -
dispatch小记
2012-11-26 21:13 836GCD大名鼎鼎,也经常用,不过大多是copy居多, ... -
transition小问题
2012-11-21 14:22 638朋友问的,发现transition在remove/add ... -
IOS 点滴 4
2012-08-30 14:19 665继续滴水 1.双缓冲 http://blo ... -
IOS 点滴 3
2012-08-25 12:38 692打球归来,发一个 1.关于性能 http: ... -
IOS 点滴 2
2012-08-23 19:41 555再来一篇,嘿嘿 1.sin函数 http:// ... -
IOS 点滴 1
2012-08-23 19:13 910好久没来,blog都长草了,囧,一直以来都觉得iteye这 ... -
copy/assgin小记
2011-12-03 11:21 1500看网上的帖子,总觉得copy的说明偏于简单了,对后来人伤 ... -
类似sina微博cell右下角的图片效果
2011-10-09 14:05 1345昨天看了sina微博,看到cell右下角的图片,就想 ... -
CALayer小记1
2011-09-27 12:20 3034今天有闲情逸致,再来一篇 1.contents填充 ... -
获取本地IP
2011-09-27 12:02 821再来一篇,获取本地IP,三种方法: (1)在Da ...
相关推荐
单片机:TMS320F28335,C语言,实现跑马灯功能
跑马灯简单程序,能够实现灯的间歇亮灭,适用于初级学者,利用定时器中断实现
FPGA设计跑马灯verilog设计实验Quartus9.1工程源码 /*跑马灯实验:利用计数器轮流点亮LED灯,实现各种动态效果。 */ module ledwater(clk,rst,dataout); input clk,rst; output[7:0] dataout; reg[7:0] dataout; ...
drawMarqueeView.add(contentView: WQScrollLabel.createLabel(text: "我是跑马灯文字我是跑马灯文字我是跑马灯文字我是跑马灯文字", textColor: .cyan)) 3、开始跑 drawMarqueeView.startAnimation() 3、暂停 ...
vhdl跑马灯 适合初学者同学...流水灯的制作
cc2530实现按键控制跑马灯启停!!!!
stm32 跑马灯实验+例程
Android跑马灯实现的三种方式
博途V16西门子1200跑马灯程序,程序简单,亲测可用,可实现输出Q0.0~Q0.7的循环输出,初学者编写,容易理解。
Delphi实现透明跑马灯效果
利用viewpaper 实现跑马灯图片轮换 并可设置10多种动画效果。是从实际项目中抽取出的demo,完整代码导入开发环境就可以运行,仔细查看demo中跑马灯图片有惊喜哦!
兼容各种浏览器的跑马灯html代码,独立且实用
跑马灯首尾相接无缝隙.js
跑马灯实验,gpio口配置输出控制跑马灯
jQuery无缝滚动跑马灯效果 jQuery无缝滚动跑马灯效果 jQuery无缝滚动跑马灯效果 jQuery无缝滚动跑马灯效果 jQuery无缝滚动跑马灯效果
Android跑马灯,可以控制速度,并对对于文本长度大于屏幕分辨率情况,也可适用,史上最强的跑马灯实现。
unity中使用c#实现跑马灯效果,可根据自身需求进行修改和优化
分享一个Android中实现跑马灯效果
这个跑马灯是对网上其它跑马灯的扩展,增加为3位输入码控制8种跑等方式。
自定义QLabel,实现跑马灯效果,直接可将源码加入到工程,自定义原有的控件,即可实现跑马灯效果,源代码编译通过,使用Qt QLabel实现,可以直接上线使用。