2013-03-14 49 views
2

我在裏面有一個標籤和文字。我希望我的文本像數字信息板一樣跨越標籤的寬度。如何在iOS中做到這一點?我試圖與該代碼(這是我從這裏得到:http://www.youtube.com/watch?v=EFoNEjPwTXM),但它不工作:如何在iOS中的屏幕上移動標籤文本?

在.m文件:

-(void)time: (NSTimer *) theTimer 
{ 
    currentSong.center = CGPointMake(currentSong.center.x - 3.5, currentSong.center.y); 
    if (currentSong.center.x < - (currentSong.bounds.size.width/2)) 
    { 
     currentSong.center = CGPointMake (320 + (currentSong.bounds.size.width/2), currentSong.center.y); 
    } 
} 

viewDidLoad

timer = [NSTimer timerWithTimeInterval:0.09 target:self selector:@selector(time:) userInfo:nil repeats:YES]; 

在.h文件:

IBOutlet UILabel *currentSong; 
    IBOutlet NSTimer *timer;  

-(void)time: (NSTimer *) theTimer; 

@end 
+2

這是被稱爲「跑馬燈」:http://stackoverflow.com/questions/11255988/continuous-scrolling-of-uilabel-like-marquee – trojanfoe 2013-03-14 13:04:17

回答

0

試試這可能有助於

-(void)viewDidLoad { 

    [super viewDidLoad]; 

    [self marqueeMessage:@"test"]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 



- (void)marqueeMessage:(NSString *)messageString { 
    UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(0, 50, 90, 21))]; 
    //label.tag=nextIndex; 
    label.text = messageString; 
    label.backgroundColor=[UIColor clearColor]; 
    [self.view addSubview:label]; 
    [UIView beginAnimations:@"test" context:nil]; 
    [UIView setAnimationDuration:3]; 
    [UIView setAnimationDidStopSelector:@selector(marqueeMessage:)]; 
    [UIView setAnimationDelegate:self]; 

    label.frame = CGRectMake(360,50,90,21); 
    [UIView commitAnimations]; 
} 
+0

謝謝,但我的想法是隻移動文本,而不是整個標籤。並且如果可能的話是相反的。我的意思是從右到左。 – scourGINHO 2013-03-14 13:29:53

0

嘗試......它可能滿足ü[R要求

int x; 
float size; 
@synthesize label; 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    label=[[UILabel alloc]init ]; 
      //WithFrame:CGRectMake(300, 400, 400, 30)]; 
    [email protected]"www.stackoverflow.com"; 
    size=[self getLabelWidth:label]; 
    label.frame=CGRectMake(300, 400, size, 30); 

    label.backgroundColor=[UIColor clearColor]; 
    x=300; 

    [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(clickme) userInfo:nil repeats:YES]; 

} 

-(void)clickme 
{ 
    x--; 
    label.frame=CGRectMake(x, 400, size, 30); 
    if(x==-size) 
    { 
     x=300; 
    } 
    [self.view addSubview:label]; 
} 

-(float)getLabelWidth:(UILabel*)label1 
{ 

    CGSize maximumSize = CGSizeMake(500,30); 
    CGSize StringSize = [label1.text sizeWithFont:label1.font constrainedToSize:maximumSize lineBreakMode:UILineBreakModeTailTruncation]; 
    NSLog(@"width is %f",StringSize.width); 
    return StringSize.width; 
} 
0

這是一個黑客攻擊的一位。它所做的只是在文本已經在標籤之前定期添加空格,以便文本看起來像在移動,而實際上標籤不是。下面的代碼:

//Interface (.h) 
@property (nonatomic, strong) IBOutlet UILabel *label; //Remember to connect this to  
                 //a label in Storyboards 

//Implementation (.m) 
-(void)moveText { 
    [self.label setText:[NSString stringWithFormat:@" %@", self.label.text]]; 
} 

- (void)viewDidLoad { 
    [self.label setText:@"A very long and boring string"]; 

    //You can change the time interval to change the speed of the animation 
    [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(moveText) userInfo:nil repeats:YES]; 
} 
0
- (void)sendNotification:(NSString*)txt{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:3.5]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:messageView cache:YES]; 
    [lblTime setText:txt]; 
    [lblTime setTextAlignment:UITextAlignmentLeft]; 
    lblTime.frame = CGRectMake(260, 0, 258, 19); 
    [UIView commitAnimations]; 
}