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

移动开发 iOS
本文介绍的是IOS应用程序跑马灯效果案例,通过代码很认真的完成了跑马灯的任务,先来看详细代码内容。

IOS应用程序跑马灯效果案例是本文要介绍的内容,内容不多,主要是以代码实现跑马灯效果的内容。先来看详细内容。最新项目中要求实现web页面中常有的跑马灯效果来显示广告内容。ios中没有提供相应的api,下面是利用NSTimer和NSLable实现的一个跑马灯效果。界面如下:

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

实现的代码如下:

  1. #import "AdvertisingView.h"   
  2. #import <QuartzCore/QuartzCore.h>   
  3. @implementation AdvertisingView   
  4. @synthesize myArray;   
  5. - (id)initWithFrame:(CGRect)frame {   
  6.     self = [super initWithFrame:frame];   
  7.     if (self) {   
  8.         [self setBackgroundColor:[UIColor clearColor]];   
  9.         if (myAdView==nil) {   
  10.             myAdView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 188, 33)];   
  11.             [myAdView setClipsToBounds:YES];   
  12.             if (myLable==nil) {   
  13.                 myLable=[[UILabel alloc] initWithFrame:CGRectMake(0, 23, 175, 33)];   
  14.                 [myLable setFont:[UIFont fontWithName:@"Helvetica" size:10.0]];   
  15.                 [myLable setNumberOfLines:0];   
  16.                 [myLable setBackgroundColor:[UIColor clearColor]];   
  17.                 [myAdView addSubview:myLable];   
  18.             }   
  19.             [myAdView setBackgroundColor:[UIColor clearColor]];   
  20.             [self addSubview:myAdView];   
  21.         }   
  22.     }   
  23.     return self;   
  24. }   
  25. - (void)dealloc {   
  26.     [[NSNotificationCenter defaultCenter] removeObserver:self];   
  27.     if (timer!=nil&&[timer isValid]) {   
  28.         [timer invalidate];   
  29.         timer=nil;   
  30.     }   
  31.     self.myArray=nil;   
  32.     [self.myArray release];   
  33.     myAdView=nil;   
  34.     [myAdView release];   
  35.     myLable=nil;   
  36.     [myLable release];   
  37.     [super dealloc];   
  38. }   
  39. -(void)addAdvertisingList   
  40. {   
  41.     //数据层   
  42.     self.myArray=[[NSMutableArray alloc] initWithCapacity:1];   
  43.     [self.myArray addObject:@"大家好"];   
  44.     [self.myArray addObject:@"We are pleased to announce that the fourth milestone release of the Spring Android project is now available!"];   
  45.     [self.myArray addObject:@"Support for Spring Social 1.0.0.RC1, and Spring Security 3.1.0.RC2 through the Spring Android Auth module,
  46.  which includes a SQLite datastore for persisting OAuth API connections."];   
  47.     if ([self.myArray count]) {   
  48.         [myLable setText:@""];   
  49.         NSString *text=nil;   
  50.         for ( int i=0; i<[self.myArray count]; i++) {   
  51.             if (i==0) {   
  52.                 text=[self.myArray objectAtIndex:i];   
  53.             }else{   
  54.                 text=[NSString stringWithFormat:@"%@\n%@",text,[self.myArray objectAtIndex:i]];   
  55.             }   
  56.         }   
  57.         UIFont *font = [UIFont fontWithName:@"Helvetica" size:10.0];   
  58.         CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f) lineBreakMode:UILineBreakModeWordWrap];   
  59.         CGRect rect=myLable.frame;   
  60.         rect.size=size;   
  61.         [myLable setFrame:rect];   
  62.         [myLable setText:text];   
  63.         if (timer==nil) {   
  64.             timer=[NSTimer scheduledTimerWithTimeInterval: 0.05   
  65.                                                    target: self   
  66.                                                  selector: @selector(handleTimer:)   
  67.                                                  userInfo: nil   
  68.                                                   repeats: YES];    
  69.         }   
  70.     }   
  71. }   
  72. -(void)handleTimer:(id)sender   
  73. {   
  74.     if ([self.myArray count]>0) {   
  75.         CGRect newFrame1 = myLable.frame;   
  76.         if (newFrame1.origin.y<-newFrame1.size.height) {   
  77.             newFrame1.origin.y = 23;   
  78.             [myLable setFrame:newFrame1];   
  79.         }else {   
  80.             newFrame1newFrame1.origin.y =  newFrame1.origin.y-0.8;   
  81.             [myLable setFrame:newFrame1];   
  82.         }      
  83.     }   
  84. }   
  85. -(void)drawMainLable:(CGRect)newFrame   
  86. {   
  87.     CGRect newFrame1 = myLable.frame;   
  88.     newFrame1newFrame1.origin.y =  newFrame1.origin.y+50;   
  89.     [myLable setFrame:newFrame1];   
  90. }   
  91. @end 

源代码:http://easymorse-iphone.googlecode.com/svn/trunk/scrollLayer/

小结:IOS应用程序跑马灯效果案例的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2015-08-07 15:45:02

swift跑马灯源码

2013-01-14 17:18:43

Android开发TextView跑马灯效果

2022-07-12 08:32:17

transition跑马灯

2023-11-01 08:33:45

CSS动画效果

2021-01-29 09:48:17

鸿蒙HarmonyOS应用开发

2021-01-28 14:34:35

鸿蒙HarmonyOS应用开发

2017-11-10 11:04:29

NVIDIA TITA处理器典藏版

2011-07-21 16:19:30

iOS Twitter

2011-05-11 10:58:39

iOS

2014-04-02 09:56:13

iOS应用减小安装包

2011-07-07 17:23:31

iOS

2011-07-28 16:06:34

IOS 应用程序 资源

2011-07-28 15:47:20

IOS 程序 测试

2015-07-09 15:42:48

ios应用生命周期

2011-05-31 15:41:00

Cocoa TouchCocoaiOS

2013-01-11 14:45:43

iOS开发移动应用iPhone

2018-10-25 15:13:23

APP脱壳工具

2011-07-06 10:22:31

XCode IOS object-C

2015-12-09 14:00:41

ios应用

2016-02-15 09:37:14

Docker持续交付应用程序
点赞
收藏

51CTO技术栈公众号