简体   繁体   中英

Is that Possible to move a uilabel in navigationbar from left to right?

In my UIViewController I am using UILabel for navigation title in xib. But text is too long so I need a animation there.Some thing like a left to right moving UILabel ?

Is that possible? I saw this code but this is for moving one point to another point . Here UILabel name is "label"

  float newX = 90.0f;
float newY = 101.0f;

[UIView transitionWithView:label
                  duration:0.5f
                   options:UIViewAnimationCurveEaseInOut
                animations:^(void) {
                    label.center = CGPointMake(newX, newY);
                }
                completion:^(BOOL finished) {
                    // Do nothing
                }];

I understand that you want marquee effect that will move each character in label to right.

Here is a way to do that.

First set NSTimer in viewDidAppear: method to call a method animateLabel with with specified time interval as shown below :

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(animateLabel:) userInfo:nil repeats:YES];
}

Then define a method that will get label text and reset it to new text. This method is called every 0.4 seconds and give you the marquee effect you want.

- (void)animateLabel:(NSTimer *)timer
{
    int labelSize = label.text.length; 
    label.text = [NSString stringWithFormat:@"%c%@", [label.text characterAtIndex:labelSize - 1], [label.text substringWithRange:NSMakeRange(0, labelSize - 1)]];
}

PS : I have not seen such marquee effect on navigation title. I have seen some popular apps where, if label is long enough to not to fit in navigation title then they keep it as it is with ... at the end. I don't say you should do the same but just want to tell you.

EDIT : To do it right to left :

- (void)animateLabel:(NSTimer *)timer
{
    int labelSize = label.text.length; 
    label.text = [NSString stringWithFormat:@"%@%c", [label.text substringWithRange:NSMakeRange(1, labelSize - 1)], [label.text characterAtIndex:0]];
}

如果你的意思是将UILabel移动到一个点并再次移回,为什么不使用完成来调用另一个动画呢?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM