【问题标题】:How To Set the Animated Images As UILabel Background?如何将动画图像设置为 UILabel 背景?
【发布时间】:2012-10-17 13:25:10
【问题描述】:

我尝试将动画图像设置为 UILabel 背景。我的标签从点移动(x=0 y260 到 x=480 y=260)我想在动画期间添加 UIImage。所以我尝试这样

UIImageView *myimg=[[UIImageView alloc]init];
        myimg.animationImages =[NSArray arrayWithObjects:   
                                [UIImage imageNamed:@"1ANI.png"],
                                [UIImage imageNamed:@"2ANI.png"],
                                [UIImage imageNamed:@"3ANI.png"],nil];

        myimg.animationDuration = 1.5;
        myimg.animationRepeatCount = 0;
        [myimg startAnimating]; 
        [lbl1 addSubview:myimg];
        [myimg release];

我像这样用单个 UIImage 设置 UILabel 背景

UIImage *image=[UIImage imageNamed:@"1ANI.png"];
            lbl2.backgroundColor = [UIColor colorWithPatternImage:image]; 

单图像代码有效,但是当我尝试使用动画图像实现它时(如上面我的代码所示)然后它不起作用有人可以指导我如何将动画图像设置为 UILabel Background.Thanx

【问题讨论】:

  • 您无法将 imageView 放在具有清晰背景的标签后面。如果您担心希望动画与标签一起移动,请将它们都放在另一个 UIView 中并移动它。

标签: iphone xcode animation uilabel


【解决方案1】:

试试这个

UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 21)];
[label setText:@"asfsdf" ];
[label setBackgroundColor:[UIColor clearColor]];

UIImageView *imagView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 100, 21)];
imagView.animationImages =[NSArray arrayWithObjects:
                        [UIImage imageNamed:@"1ANI.png"],
                        [UIImage imageNamed:@"2ANI.png"],
                        [UIImage imageNamed:@"3ANI.png"],nil];

imagView.animationDuration = 1.5;
imagView.animationRepeatCount = 0;
[imagView startAnimating];
[self.view addSubview:imagView];
[self.view addSubview:label];

【讨论】:

  • 标签没有透明背景..你必须用lbl1.backgroundColor = [UIColor clearColor]使其透明
  • 是否可以将 imageview 添加到 uilabel。因为我的 UILabel 沿着屏幕移动,所以它不适合我在特定点创建 imageview 这就是为什么我需要在我的 UILabel 添加 uiimageview。
  • 嗯,可能很难在“UILabel”中包含图像。好吧,你可以做一件事,创建一个 UIView 的子类并添加上面提到的 imageview 和 UILabel 并直接将其用于您的目的
【解决方案2】:

首先不要将 UIImageView 添加到标签为[lbl1 addSubview:myimg]; 的子视图

先添加 imageview,然后再添加 uilabel。您的标签将是透明的并浮动在 uiimageview 上。

您可能还需要 [imageView setuserinteractionEnabled:YES]; 获取 uiimageview。

【讨论】:

  • 标签没有透明背景。你必须用lbl1.backgroundColor = [UIColor clearColor]使其透明
  • 是否可以将 imageview 添加到 uilabel。因为我的 UILabel 沿着屏幕移动,所以它不适合我在特定点创建 imageview 这就是为什么我需要在我的 UILabel 添加 uiimageview。
【解决方案3】:

iOS 7 中的这项工作

[myLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"panel_dev_header"]]];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 2018-01-05
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多