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