2013-03-14 3 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]; 

В заголовочный файл:

IBOutlet UILabel *currentSong; 
    IBOutlet NSTimer *timer;  

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

@end 
+2

Это называется "Marquee": http://stackoverflow.com/questions/11255988/continuous-scrolling-of-uilabel-like-marquee – trojanfoe

ответ

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

0

Попробуйте это ... это может встретить у г требование

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]; 
} 
Смежные вопросы