【iOS解决方案】iPhone走马灯控件实现

来源:互联网 发布:费马的房间 知乎 编辑:程序博客网 时间:2024/05/22 09:39

本文来源:http://www.cnblogs.com/KiloNet/archive/2010/09/17/1829155.html

 走马灯效果实现原理,就是利用iPhone自带的动画来控制UILable的y轴位置:

    代码如下:

[cpp] view plaincopy
  1. //LampText.h  
  2. @interface LampText : UILabel {  
  3.     float motionWidth;  
  4. }  
  5. @property (nonatomic)   float motionWidth;  
  6. @end  
  7.   
  8.   
  9.   
  10. //LampText.m  
  11. #import "LampText.h"  
  12.   
  13. @implementation LampText  
  14. @synthesize motionWidth;  
  15.   
  16. - (id)initWithFrame:(CGRect)frame {  
  17.     if ((self = [super initWithFrame:frame])) {  
  18.         motionWidth = 200;  
  19.     }  
  20.     return self;  
  21. }  
  22.   
  23. - (void)drawRect:(CGRect)rect {  
  24.       
  25.     [super drawRect:rect];  
  26.     float w  = self.frame.size.width;  
  27.     if (motionWidth>=w) {  
  28.         return;  
  29.     }  
  30.       
  31.     CGRect frame = self.frame;  
  32.     frame.origin.x = 320;  
  33.     self.frame = frame;  
  34.       
  35.     [UIView beginAnimations:@"testAnimation" context:NULL];  
  36.     [UIView setAnimationDuration:8.0f * (w<320?320:w) / 320.0 ];    
  37.     [UIView setAnimationCurve:UIViewAnimationCurveLinear];       
  38.     [UIView setAnimationDelegate:self];    
  39.     [UIView setAnimationRepeatAutoreverses:NO];       
  40.     [UIView setAnimationRepeatCount: LONG_MAX];   
  41.       
  42.     frame = self.frame;  
  43.     frame.origin.x = -w ;  
  44.     self.frame = frame;  
  45.     [UIView commitAnimations];    
  46. }  
  47.   
  48. - (void)dealloc {  
  49.     [super dealloc];  
  50. }  
  51.   
  52. @end  
  53.   
  54. //调用:  
  55.    NSString *title = @"Hi,kilonet, weclome to my blog!";  
  56.     CGFloat w = [title sizeWithFont:[UIFont fontWithName:@"Arial" size:18]].width;  
  57.     LampText *titleLabel = [[LampText alloc]initWithFrame:CGRectMake(0, 0, w, 40)];  
  58.     [titleLabel setBounds:CGRectMake(0, 0, w, 40)];   
  59.     titleLabel.lineBreakMode = UILineBreakModeClip;  
  60.     titleLabel.text = title;  
  61.     titleLabel.textAlignment = UITextAlignmentCenter;  
  62.     titleLabel.font = [UIFont fontWithName:@"Arial" size:18];  
  63.     titleLabel.textColor = [UIColor whiteColor];  
  64.     titleLabel.backgroundColor = [UIColor clearColor];  
  65.     //[titleLabel sizeToFit];  
  66.       
  67.     [self.view addSubview: titleLabel];  
  68.     [titleLabel release];  


其它扩展:

在Navigation里实现跑马灯效果,因为Navigation的宽度限制了,所以须使用下面代码调用:

[cpp] view plaincopy
  1. +(void) showNavTitle:(UIViewController *)controller title:(NSString *)title  {  
  2.     [Utilitys showNavTitle:controller title:title width:320.0];  
  3. }  
  4.   
  5. +(void) showNavTitle:(UIViewController *)controller title:(NSString *)title width:(CGFloat) width {  
  6.   
  7.     CGFloat w = [title sizeWithFont:[UIFont fontWithName:@"Arial" size:18]].width;  
  8.     CGFloat x = 0;  
  9.     if (w <= width) {  
  10.         x = (width - w) / 2;  
  11.     }  
  12.       
  13.     LampText *titleLabel = [[LampText alloc]initWithFrame:CGRectMake(x, 0, w, 40)];  
  14.     titleLabel.motionWidth = width;  
  15.     titleLabel.lineBreakMode = UILineBreakModeClip;  
  16.     titleLabel.text = title;  
  17.     titleLabel.textAlignment = UITextAlignmentCenter;  
  18.     titleLabel.font = [UIFont fontWithName:@"Arial" size:18];  
  19.     titleLabel.textColor = [UIColor whiteColor];  
  20.     titleLabel.backgroundColor = [UIColor clearColor];  
  21.   
  22.     UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, 40)];  
  23.     [scroll addSubview:titleLabel];  
  24.     controller.navigationItem.titleView = scroll;  
  25.   
  26.     [titleLabel release];  
  27.     [scroll release];  
  28.       
  29. }  
0 0
原创粉丝点击