Как да направя текст на етикет да се движи по екрана в iOS?

Имам етикет и текст вътре. Искам текстът ми да се движи по ширината на етикета като цифрово информационно табло. Как да направите това в iOS? Опитах с този код (който взех от тук: http://www.youtube.com/watch?v=EFoNEjPwTXM ), но не работи:

В .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);
    }
}

In 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

person scourGINHO    schedule 14.03.2013    source източник
comment
Това се нарича Marquee: stackoverflow.com/questions /11255988/   -  person trojanfoe    schedule 14.03.2013


Отговори (3)


Опитайте това може да помогне

-(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];
}
person 08442    schedule 14.03.2013
comment
Благодаря, но идеята ми беше да преместя само текста, а не целия етикет. И ако е възможно да е в обратна посока. От дясно на ляво, имам предвид. - person scourGINHO; 14.03.2013

Опитайте това...може да отговаря на вашите изисквания

 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)];
    label.text=@"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;
}
person 08442    schedule 15.03.2013

Това е малко хак. Това, което прави, е, че просто периодично добавя интервали преди текста, който вече е в етикета, така че текстът да изглежда сякаш се движи, докато всъщност етикетът не е. Ето кода:

//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];
}
person pasawaya    schedule 15.03.2013