试试下面的代码
int i = 0;
float width = 0.0;
float origin_x = 0.0;
for (NSString *s in arrList)
{
CGSize size;
NSRange r;
size = [s sizeWithFont:[UIFont fontWithName:@"*****" size:14.0] constrainedToSize:CGSizeMake(10000.0, 35.0) lineBreakMode:UILineBreakModeWordWrap];
width = (size.width + 12.0 + 10.0);
UIView *viewTopStory = [[[UIView alloc] initWithFrame:CGRectMake(origin_x, 0.0, width, 35.0)] autorelease];
viewTopStory.backgroundColor = [UIColor clearColor];
UIImageView *imgStar = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"star.png"] ] autorelease];
imgStar.frame = CGRectMake(5, 8.0, 12, 12);
[viewTopStory addSubview:imgStar];
UILabel *lblTopstory = [[[UILabel alloc] initWithFrame:CGRectMake(22.0, 2.75, size.width, 30.0)] autorelease];
lblTopstory.font = [UIFont fontWithName:@"*****" size:14.0];
lblTopstory.backgroundColor = [UIColor clearColor];
lblTopstory.text = s;
[viewTopStory addSubview:lblTopstory];
[_viewTopStory addSubview:viewTopStory];
CGRect frame = _viewTopStory.frame;
frame.size.width += width;
_viewTopStory.frame = frame;
origin_x += width;
i++;
}
CAKeyframeAnimation *animation = [CAKeyframeAnimation
animationWithKeyPath:@"transform"];
//NSMutableArray *marrframeValues = [[[NSMutableArray alloc]init] autorelease];
CATransform3D Translation0 = CATransform3DMakeTranslation(0.0, 0.0, 0.0);
CATransform3D Translation4 = CATransform3DMakeTranslation(_viewTopStory.frame.size.width, 0.0, 0.0);
NSArray *frameValues = [NSArray arrayWithObjects:[NSValue valueWithCATransform3D:Translation0], [NSValue valueWithCATransform3D:Translation4],
nil];
[animation setValues:frameValues];
NSArray *frameTimes = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:1.0], nil];
[animation setKeyTimes:frameTimes];
animation.fillMode = kCAFillModeBackwards;
//animation.autoreverses = YES;
animation.repeatCount = 10000;
animation.removedOnCompletion = NO;
animation.duration = 200.0;
[_viewTopStory.layer addAnimation:animation forKey:@"popup"];
我已经放置了带有初始固定框架的 _viewTopStory,然后我根据文本修改了框架。
希望它会帮助你。