gpt4 book ai didi

ios - 如何在 Objective C 中像 Marquee Tag 一样为 UIImage 设置动画

转载 作者:行者123 更新时间:2023-11-29 11:50:00 27 4
gpt4 key购买 nike

我是 iOS 的新手,我面临与品牌标签相同的动画图像的问题。我正在为 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 做。我的主要问题是图像可以根据来自网络服务的数据是多个的。提前致谢!

最佳答案

您可以使用 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];
}];

}

关于ios - 如何在 Objective C 中像 Marquee Tag 一样为 UIImage 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41545397/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com