我想在我的应用中显示一些文字,比如移动文字(从右到左滚动动画)。如何以编程方式执行此操作?
我带了UIViewcontroller
。我正在开发AVAudioplayer
。所以在UIViewController
的顶部,文本将从右向左移动。
答案 0 :(得分:10)
首先,您在视图中选择一个标签,并将其框架设置为视图,如下所示。
- (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];
}
现在该标签按照以下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);
}];
}
输出低于。
答案 1 :(得分:2)
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];
如果你想重复滚动文字
,你可以尝试一下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){
}];
答案 2 :(得分:1)
//在需要的地方调用此方法。 //并在此方法中编写此4行代码
[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
有效..
答案 3 :(得分:0)
你可以尝试这个
[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){
}];
这里" Moving_Cloud"是我的图像视图,所以你也可以试试你的标签。
答案 4 :(得分:0)
哟可以使用UIView
Animation
阻止
[UIView animateWithDuration:5.0f delay:0.0f options:UIViewAnimationOptionTransitionNone animations:^{
yourLabel.center = CGPointMake(0, yourLabel.center.y);
} completion:NULL ];
如果你想要autoreverses
[UIView animateWithDuration:5.0f delay:0.0f options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionBeginFromCurrentState animations:^{
yourLabel.center = CGPointMake(0, yourLabel.center.y);
} completion:NULL ];
答案 5 :(得分:0)
使用以下方法
- (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];
}];
}
将要从右向左移动的标签传递给此方法。添加条件以停止动画,因为此方法不断循环。您可以根据需要更改动画持续时间。
答案 6 :(得分:0)
在Swift中你可以像这样实现它
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)
});
}
答案 7 :(得分:0)
使用背景颜色添加视图。
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];
}];
}
答案 8 :(得分:0)
对于Swift,只需运行以下功能即可:
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)
})
}