`
hua397
  • 浏览: 53799 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

跑马灯

阅读更多

  首先感谢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...),姑且用之吧

 

分享到:
评论
1 楼 aa3913733 2012-12-09  

    [*]

    [*]
引用

相关推荐

Global site tag (gtag.js) - Google Analytics