IOS 跑马灯效果

来源:互联网 发布:文件服务器同步软件 编辑:程序博客网 时间:2024/04/29 13:27

//

//  UIAdvertisingView.m

//  test2

//

//  Created by liangliang on 11-7-29.

//  Copyright 2011 __MyCompanyName__. All rights reserved.

//


#import"UIAdvertisingView.h"



@implementation UIAdvertisingView



@synthesize myArray;



- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) { 

        

        //初始化代码

        [selfsetBackgroundColor:[UIColorclearColor]]; 

        if (myAdView==nil) { 

           myAdView=[[UIViewalloc] initWithFrame: frame];//CGRectMake(0, 0, 188, 33)]; 

            [myAdViewsetClipsToBounds:YES];

            

            if (myLable==nil) { 

                myLable=[[UILabel alloc] initWithFrame: frame]; //CGRectMake(0, 40, 300, 200)]; 

                [myLablesetFont:[UIFontfontWithName:@"Helvetica"size:12.0]]; 

                [myLablesetNumberOfLines:0]; 

                [myLablesetBackgroundColor:[UIColorclearColor]]; 

                [myAdViewaddSubview:myLable]; 

            }

            

            [myAdViewsetBackgroundColor:[UIColorclearColor]]; 

            [selfaddSubview:myAdView]; 

        }

        

    } 

   return self

}


// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

   // Drawing code

}



- (void)dealloc

{

    [[NSNotificationCenterdefaultCenter] removeObserver:self]; 

    if (timer!=nil&&[timerisValid]) { 

        [timerinvalidate]; 

        timer=nil

    } 

    self.myArray=nil

    [self.myArrayrelease]; 

   myAdView=nil

    [myAdViewrelease]; 

     myLable=nil

    [myLablerelease]; 

    [superdealloc]; 

}



-(void)addAdvertisingList 

   //数据层 

   self.myArray=[[NSMutableArrayalloc] initWithCapacity:1]; 

    [self.myArrayaddObject:@"大家好"]; 

    [self.myArrayaddObject:@"We are pleased to announce that the fourth milestone release of the Spring Android project is now available!"];

   

    if ([self.myArraycount]) { 

        

        [myLablesetText:@""]; 

        NSString *text=nil

        for ( int i=0; i<[self.myArraycount]; i++) { 

            if (i==0) { 

                text=[self.myArrayobjectAtIndex:i]; 

            }else

                text=[NSStringstringWithFormat:@"%@\n%@",text,[self.myArrayobjectAtIndex:i]]; 

            } 

        }

        

        UIFont *font = [UIFont fontWithName:@"Helvetica" size:10.0]; 

       CGSize size = [textsizeWithFont:font constrainedToSize:CGSizeMake(175.0f,2000.0f) lineBreakMode:UILineBreakModeWordWrap]; 

        CGRect rect=myLable.frame

        rect.size=size; 

        [myLablesetFrame:rect]; 

        [myLablesetText:text];

        

        if (timer==nil) { 

           timer=[NSTimerscheduledTimerWithTimeInterval: 0.05 

                                                  target: self 

                                                selector: @selector(handleTimer:) 

                                                userInfo: nil 

                                                 repeats: YES];  

        } 

    } 

}


-(void)handleTimer:(id)sender 

    if ([self.myArraycount]>0) { 

        CGRect newFrame1 = myLable.frame

        if (newFrame1.origin.y<-newFrame1.size.height) { 

            newFrame1.origin.y =23

            [myLablesetFrame:newFrame1]; 

        }else

            newFrame1.origin.y =  newFrame1.origin.y-0.8

            [myLablesetFrame:newFrame1]; 

        }    

    } 



@end








//

//  UIAdvertisingView.h

//  test2

//

//  Created by liangliang on 11-7-29.

//  Copyright 2011 __MyCompanyName__. All rights reserved.

//


#import<UIKit/UIKit.h>



@interface UIAdvertisingView :UIView {

    

    

    NSMutableArray *myArray;

    UIView * myAdView;

    UILabel * myLable;

    NSTimer *timer;

}


-(void)addAdvertisingList;

-(void)handleTimer:(id)sender;



@property (nonatomic,retain) NSMutableArray *myArray;


@end


原创粉丝点击