如何在Objective C中像Marquee Tag一样为UIImage设置动画
Posted
技术标签:
【中文标题】如何在Objective C中像Marquee Tag一样为UIImage设置动画【英文标题】:How to animate UIImage like Marque Tag in Objective C 【发布时间】:2017-01-09 10:04:19 【问题描述】:我是 ios 新手,我遇到了关于动画图像与 marque 标签相同的问题。我正在为 UILabel 文本设置动画,为此我的代码是这样的
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
scrolllbl = [[UILabel alloc]initWithFrame:CGRectMake(0,0,scrolllbl.bounds.size.width, 1800)];
NSString *theText = @"A long string";
CGRect labelRect = CGRectMake(10, 50, 300, 50);
scrolllbl.adjustsFontSizeToFitWidth = YES;
[scrolllbl setNumberOfLines:0];
CGFloat fontSize = 30;
while (fontSize > 0.0)
CGSize size = [theText sizeWithFont:[UIFont fontWithName:@"Verdana" size:fontSize] constrainedToSize:CGSizeMake(labelRect.size.width, 10000) lineBreakMode:NSLineBreakByWordWrapping];
if (size.height <= labelRect.size.height) break;
fontSize -= 1.0;
//set font size
scrolllbl.font = [UIFont fontWithName:@"Arial" size:fontSize];
else
scrolllbl = [[UILabel alloc]initWithFrame:CGRectMake(0,0,scrolllbl.bounds.size.width, 800)];
// scrolllbl.textColor = [UIColor redColor];
//lbl2.text=result1;
NSLog(@"Result Array =%@",shortnamearray);
CGFloat y = 10;
NSMutableArray* animals = [NSMutableArray new];
NSUInteger maxCount = headarray.count > shortnamearray.count ? headarray.count : shortnamearray.count;
for (int i = 0; i < maxCount; i ++)
if ([headarray objectAtIndex:i])
[animals addObject:[headarray objectAtIndex:i]];
if ([shortnamearray objectAtIndex:i])
[animals addObject:[shortnamearray objectAtIndex:i]];
NSLog(@"Array is =%@",animals);
for(int i=0;i<[shortnamearray count] && i<[headarray count];i++)
// y+=20;
y+=10;
NSString *newArray =[animals objectAtIndex:i];
newArray=[animals componentsJoinedByString:@""];
NSString *NewString;
[scrolllbl setLineBreakMode:NSLineBreakByWordWrapping];
scrolllbl.textAlignment = NSTextAlignmentCenter;
//[scrolllbl setText:NewString];
NSString * htmlString = @"<html><body>";
NSString *htmlString2=@"</body></html>";
NewString=[NSString stringWithFormat:@"%@%@%@",htmlString,newArray,htmlString2];
NSLog(@"New String =%@",NewString);
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[NewString dataUsingEncoding:NSUnicodeStringEncoding] options:@ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType documentAttributes:nil error:nil];
scrolllbl.attributedText = attrStr;
NSString *theText = @"A long string";
CGRect labelRect = CGRectMake(10, 50, 300, 50);
scrolllbl.adjustsFontSizeToFitWidth = YES;
[scrolllbl setNumberOfLines:0];
CGFloat fontSize = 15;
while (fontSize > 0.0)
CGSize size = [theText sizeWithFont:[UIFont fontWithName:@"Verdana" size:fontSize] constrainedToSize:CGSizeMake(labelRect.size.width, 10000) lineBreakMode:NSLineBreakByWordWrapping];
if (size.height <= labelRect.size.height) break;
fontSize -= 1.0;
//set font size
scrolllbl.font = [UIFont fontWithName:@"Arial" size:fontSize];
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];
[newsscroll addSubview:scrolllbl];
[UIView commitAnimations];
scroll.contentSize=CGSizeMake(newsscroll.frame.size.width+[shortnamearray count], scrolllbl.frame.size.height);
这就是我为 UILabel 做的事情。请给我建议如何为 UIImage 做。我的主要问题是根据来自网络服务的数据,图像可以是多个图像。提前致谢!
【问题讨论】:
使用 UICollectionView 来满足您对多个 i,age 的要求,并带有标记效果。 请试试这个github.com/cbess/AutoScrollLabel @CodeChanger 如何在其中添加标记效果? 【参考方案1】:您可以使用animateWithDuration
为图像制作简单代码:
在viewDidLoad
中调用addAllImages
:
-(void)addAllImages
scr=[[UIScrollView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width, 0, 320, 150)];
float xPosition=5;
for (int i =0; i<=10; i++)
UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(xPosition, 0, 150, 150)];
img.image=[UIImage imageNamed:@"xyz.png"];
[scr addSubview:img];
xPosition+=150+5;
scr.frame=CGRectMake([UIScreen mainScreen].bounds.size.width, 0, xPosition, 150);
为图像设置动画时调用animateImage
方法:
-(void)animateImage
[UIView animateWithDuration:3.0 animations:^
scr.frame=CGRectMake(0,0, scr.frame.size.width, scr.frame.size.height);
completion:^(BOOL finished)
scr.frame=CGRectMake([UIScreen mainScreen].bounds.size.width,0, scr.frame.size.width, scr.frame.size.height);
[self animateImage];
];
【讨论】:
但是这个动画只有一个图像。 如果你有多个图像,首先在滚动视图中添加所有图像,然后你可以为滚动视图设置动画 现在检查它,如果有任何问题重播我 你好@Muju,如果它的工作请正确回答【参考方案2】:请查看this link随意拍摄图像数组和移动时间间隔可能对您有帮助。而且它非常易于使用
【讨论】:
以上是关于如何在Objective C中像Marquee Tag一样为UIImage设置动画的主要内容,如果未能解决你的问题,请参考以下文章
如何在Objective C中只绘制UIView的顶角而不是整个视图?
如何在Objective C中仅弯曲UIView的顶角而不是整个视图?
我如何在颤动中像这样 2019-07-08T10:37:28Z 格式化日期