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应用程序跑马灯效果案例的内容介绍完了,希望本文对你有所帮助!