六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 367|回复: 0

IOS应用程序跑马灯效果案例

[复制链接]

升级  78.6%

856

主题

856

主题

856

主题

探花

Rank: 6Rank: 6

积分
2572
 楼主| 发表于 2013-2-4 02:27:10 | 显示全部楼层 |阅读模式
IOS应用程序跑马灯效果案例是本文要介绍的内容,内容不多,主要是以代码实现跑马灯效果的内容。先来看详细内容。最新项目中要求实现web页面中常有的跑马灯效果来显示广告内容。ios中没有提供相应的api,下面是利用NSTimer和NSLable实现的一个跑马灯效果。界面如下:

实现的代码如下:

  • #import "AdvertisingView.h"   
  • #import <QuartzCore/QuartzCore.h>   
  • @implementation AdvertisingView   
  • @synthesize myArray;   
  • - (id)initWithFrame:(CGRect)frame {   
  •     self = [super initWithFrame:frame];   
  •     if (self) {   
  •         [self setBackgroundColor:[UIColor clearColor]];   
  •         if (myAdView==nil) {   
  •             myAdView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 188, 33)];   
  •             [myAdView setClipsToBounds:YES];   
  •             if (myLable==nil) {   
  •                 myLable=[[UILabel alloc] initWithFrame:CGRectMake(0, 23, 175, 33)];   
  •                 [myLable setFont:[UIFont fontWithName:@"Helvetica" size:10.0]];   
  •                 [myLable setNumberOfLines:0];   
  •                 [myLable setBackgroundColor:[UIColor clearColor]];   
  •                 [myAdView addSubview:myLable];   
  •             }   
  •             [myAdView setBackgroundColor:[UIColor clearColor]];   
  •             [self addSubview:myAdView];   
  •         }   
  •     }   
  •     return self;   
  • }   
  • - (void)dealloc {   
  •     [[NSNotificationCenter defaultCenter] removeObserver:self];   
  •     if (timer!=nil&&[timer isValid]) {   
  •         [timer invalidate];   
  •         timer=nil;   
  •     }   
  •     self.myArray=nil;   
  •     [self.myArray release];   
  •     myAdView=nil;   
  •     [myAdView release];   
  •     myLable=nil;   
  •     [myLable release];   
  •     [super dealloc];   
  • }   
  • -(void)addAdvertisingList   
  • {   
  •     //数据层   
  •     self.myArray=[[NSMutableArray alloc] initWithCapacity:1];   
  •     [self.myArray addObject:@"大家好"];   
  •     [self.myArray addObject:@"We are pleased to announce that the fourth milestone release of the Spring Android project is now available!"];   
  •     [self.myArray addObject:@"Support for Spring Social 1.0.0.RC1, and Spring Security 3.1.0.RC2 through the Spring Android Auth module,
  •                                which includes a SQLite datastore for persisting OAuth API connections."];   
  •     if ([self.myArray count]) {   
  •         [myLable setText:@""];   
  •         NSString *text=nil;   
  •         for ( int i=0; i<[self.myArray count]; i++) {   
  •             if (i==0) {   
  •                 text=[self.myArray objectAtIndex:i];   
  •             }else{   
  •                 text=[NSString stringWithFormat:@"%@\n%@",text,[self.myArray objectAtIndex:i]];   
  •             }   
  •         }   
  •         UIFont *font = [UIFont fontWithName:@"Helvetica" size:10.0];   
  •         CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f) lineBreakMode:UILineBreakModeWordWrap];   
  •         CGRect rect=myLable.frame;   
  •         rect.size=size;   
  •         [myLable setFrame:rect];   
  •         [myLable setText:text];   
  •         if (timer==nil) {   
  •             timer=[NSTimer scheduledTimerWithTimeInterval: 0.05   
  •                                                    target: self   
  •                                                  selector: @selector(handleTimer:)   
  •                                                  userInfo: nil   
  •                                                   repeats: YES];   
  •         }   
  •     }   
  • }   
  • -(void)handleTimer:(id)sender   
  • {   
  •     if ([self.myArray count]>0) {   
  •         CGRect newFrame1 = myLable.frame;   
  •         if (newFrame1.origin.y<-newFrame1.size.height) {   
  •             newFrame1.origin.y = 23;   
  •             [myLable setFrame:newFrame1];   
  •         }else {   
  •             newFrame1newFrame1.origin.y =  newFrame1.origin.y-0.8;   
  •             [myLable setFrame:newFrame1];   
  •         }      
  •     }   
  • }   
  • -(void)drawMainLable:(CGRect)newFrame   
  • {   
  •     CGRect newFrame1 = myLable.frame;   
  •     newFrame1newFrame1.origin.y =  newFrame1.origin.y+50;   
  •     [myLable setFrame:newFrame1];   
  • }   
  • @end
源代码:http://easymorse-iphone.googlecode.com/svn/trunk/scrollLayer/
小结:IOS应用程序跑马灯效果案例的内容介绍完了,希望本文对你有所帮助!
【编辑推荐】
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表