【发布时间】:2014-07-24 09:48:49
【问题描述】:
我有一个数组,它的数据使用 UILabel 显示在滚动视图上。我可以左右滚动。但我想再添加一项功能,即在滚动视图上自动滚动标签文本。这意味着即使我不滚动,我也希望它自行移动。 我的滚动代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
_scrollView.contentInset=UIEdgeInsetsMake(0, 300, 0, 300);
NSMutableArray *items=[[NSMutableArray alloc] initWithObjects:@"A walk to remember",@"The Last song",@"The Notebook",@"Dear John",@"The Longest Ride",nil];
CGFloat y = 10;
self.label = [[UILabel alloc]initWithFrame:CGRectMake(0,y,800, 25)];
for(int i=0;i<[items count];i++){
y+=20;
NSString* lab=[items objectAtIndex:i];
_label.backgroundColor=[UIColor clearColor];
lab=[items componentsJoinedByString:@" | "];
NSLog(@"label:%@",lab);
[self.label setLineBreakMode:NSLineBreakByWordWrapping];
[self.label setText:lab];
}
NSTimer *timer = [NSTimer timerWithTimeInterval:1
target:self
selector:@selector(timer)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
//[_scrollView scrollRectToVisible:NSMakeRange(0,0,0.0) animated:YES];
[_scrollView addSubview:_label];
[UIView commitAnimations];
_scrollView.contentSize=CGSizeMake(_scrollView.frame.size.width*[items count], _scrollView.frame.size.height);
}
定时器代码:
-(void)timer {
[self.label setText:[NSString stringWithFormat:@" %@", self.label.text]];
}
请帮助我如何使标签文本水平移动。感谢您的帮助
【问题讨论】:
-
我认为这是在滚动视图上添加标签的代码。没有动画代码。可以加个时间码吗?
-
这段代码让我的文字现在自动移动。但是从左到右。我希望它从右向左移动并再次重复。
-
只需尝试使用AutoScrollLabel。这将帮助你....
-
非常感谢 N J Gadhiya,它成功了 :)
标签: objective-c ios7 uiscrollview nstimer