IOS 跑馬燈效果

//

//  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) { 

        

        //初始化代碼

        [self setBackgroundColor:[UIColor clearColor]]; 

        if (myAdView==nil) { 

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

            [myAdView setClipsToBounds:YES];

            

            if (myLable==nil) { 

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

                [myLable setFont:[UIFont fontWithName:@"Helvetica" size:12.0]]; 

                [myLable setNumberOfLines:0]; 

                [myLable setBackgroundColor:[UIColor clearColor]]; 

                [myAdView addSubview:myLable]; 

            }

            

            [myAdView setBackgroundColor:[UIColor clearColor]]; 

            [self addSubview: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

{

    [[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!"];

   

    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

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

            [myLable setFrame: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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章