【问题标题】:How to animate UIImage like Marque Tag in Objective C如何在Objective C中像Marquee Tag一样为UIImage设置动画
【发布时间】: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 的要求,并带有标记效果。
  • @CodeChanger 如何在其中添加标记效果?

标签: ios objective-c uiimage uilabel


【解决方案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随意拍摄图像数组和移动时间间隔可能对您有帮助。而且它非常易于使用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-14
    • 2021-10-01
    • 2013-03-07
    • 1970-01-01
    • 2011-06-13
    • 2023-01-24
    • 2020-11-02
    相关资源
    最近更新 更多