Reputation: 341
I want to show some text in my app like moving text (Scrolling with animation from right to left). How to do this programmatically?
I took UIViewcontroller
. I am developing AVAudioplayer
. so in the top side of UIViewController
the text will move from right to left.
Upvotes: 4
Views: 12822
Reputation: 620
for Swift just run this func:
func startMarqueeLabelAnimation() {
DispatchQueue.main.async(execute: {
UIView.animate(withDuration: 20.0, delay: 1, options: ([.curveLinear, .repeat]), animations: {() -> Void in
self.marqueeLabel.center = CGPoint(x: 0 - self.marqueeLabel.bounds.size.width / 2, y: self.marqueeLabel.center.y)
}, completion: nil)
})
}
Upvotes: 0
Reputation: 17902
Add View with background colour.
UIView *animatedLblView = [[UIView alloc]initWithFrame:CGRectMake(0, 75, self.view.frame.size.width, 30)];
animatedLblView.backgroundColor = [UIColor colorWithRed:0.00 green:0.34 blue:0.61 alpha:1.0];
[self.view addSubview:animatedLblView];
//Our animated component(UIButton)
_animatingButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width+100, 30)];
[_animatingButton setTitle:@"Upload documents, please contact 1234567890" forState:UIControlStateNormal];
[animatedLblView addSubview:_animatingButton];
//Timer
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(LabelAnimation)
userInfo:nil
repeats:nil];
-(void)LabelAnimation {
//Set animation
[UIView animateWithDuration:10.0f delay:0.0f options:UIViewAnimationOptionTransitionNone animations:^{
_animatingButton.frame = CGRectMake(-(self.view.frame.size.width+100), 0, self.view.frame.size.width+100, 30);
} completion:^(BOOL finished) {
_animatingButton.frame = CGRectMake(self.view.frame.size.width, 0, 0, 30);
// Call the same function
[self LabelAnimation];
}];
}
Upvotes: 0
Reputation: 8287
IN Swift you can implement it like this
override func viewDidLoad() {
super.viewDidLoad()
marqueeLabel = UILabel(frame: CGRectMake(320, 100, 400, 60))
marqueeLabel.text = "Your music title here"
self.view.addSubview(marqueeLabel)
UIView.animateWithDuration(10.0, delay: 0.0, options: [.Repeat], animations: { () -> Void in
self.marqueeLabel.frame = CGRectMake(-320, 100, 400, 60)
}, completion: { (finished: Bool) -> Void in
self.marqueeLabel.frame = CGRectMake(320, 100, 400, 60)
});
}
Upvotes: 0
Reputation: 23407
First of all you take a label in your view and set its frame out of view as following.
- (void)viewDidLoad
{
[super viewDidLoad];
la = [[UILabel alloc]initWithFrame:CGRectMake(320, 100, 200, 60)];
la.text = @"This is my music line";
[self.view addSubview:la];
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(LabelAnimation)
userInfo:nil
repeats:YES];
}
Now that label give animation as below method called in ViewDidLoad
-(void)LabelAnimation
{
[UIView animateWithDuration:3.0f delay:0.0f options:UIViewAnimationOptionTransitionNone animations:^{
la.frame = CGRectMake(-320, 100, 200, 60);
} completion:^(BOOL finished)
{
la.frame = CGRectMake(320, 100, 200, 60);
}];
}
output is below.
Upvotes: 14
Reputation: 39
//Call this method where you need this. // and in this method write this 4 lines of code
[self Message:@"test"];
- (void)Message:(NSString *)messageString
{
UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(321, 20, 300, 30))];
label.text = messageString;
label.backgroundColor = [UIColor clearColor];
[self.view addSubview:label];
[UIView beginAnimations:@"test" context:nil];
[UIView setAnimationDuration:3];
[UIView setAnimationDidStopSelector:@selector(Message:)];
[UIView setAnimationDelegate:self];
label.frame = CGRectMake(-100, 20, 300, 30);
[UIView commitAnimations];
}
enter code here
It works..
Upvotes: 1
Reputation: 364
UILabel*label=[[UILabel alloc]init];
label.text=@"Song Name";
label.frame=CGRectMake(321, 20, 300, 30);
[self.view addSubview:label];
[UIView beginAnimations:@"" context:nil];
[UIView setAnimationDuration:20.0];
label.frame=CGRectMake(0, 20, 300, 30);
[UIView commitAnimations];
Or you can try this out if you want to repeat the scrolling of the text
UILabel*label=[[UILabel alloc]init];
label.text=@"Song Name";
label.frame=CGRectMake(321, 20, 300, 30);
[self.view addSubview:label];
[UIView animateWithDuration:5.0 delay:0.0 options: UIViewAnimationOptionRepeat
animations:^{
label.frame=CGRectMake(-100, 20, 300, 30);
}completion:^(BOOL finished){
}];
Upvotes: 3
Reputation: 4705
Use the below method
- (void)marqueeLabel:(UILabel *)label
{
__block UILabel *labelToBeMarqueed = label;
__block CGRect labelFrame = labelToBeMarqueed.bounds;
labelFrame.origin.x = [UIScreen mainScreen].bounds.size.width;
labelToBeMarqueed.frame = labelFrame;
[UIView animateWithDuration:2.0f
animations:^{
labelFrame.origin.x = -[UIScreen mainScreen].bounds.size.width;
labelToBeMarqueed.frame = labelFrame;
} completion:^(BOOL finished) {
[self marqueeLabel:label];
}];
}
Pass the label that you want to move from right to left to this method. Add a condition to stop the animation as this method loops continuously. You can change the animation duration as required.
Upvotes: 0
Reputation: 3399
Yo can use UIView
Animation
Blocks for that
[UIView animateWithDuration:5.0f delay:0.0f options:UIViewAnimationOptionTransitionNone animations:^{
yourLabel.center = CGPointMake(0, yourLabel.center.y);
} completion:NULL ];
And if you want something like autoreverses
[UIView animateWithDuration:5.0f delay:0.0f options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionBeginFromCurrentState animations:^{
yourLabel.center = CGPointMake(0, yourLabel.center.y);
} completion:NULL ];
Upvotes: 0
Reputation: 1312
you could try this one
[UIView animateWithDuration:15.0f animations:^{
Moving_Cloud.frame = CGRectMake(320.0f, 30.0f, Moving_Cloud.frame.size.width, Moving_Cloud.frame.size.height);
}
completion:^(BOOL finished){
}];
here " Moving_Cloud " is my image view so likewise you can try for your label.
Upvotes: 0