【问题标题】:i want show different images continuously without user touch the button我想在没有用户触摸按钮的情况下连续显示不同的图像
【发布时间】:2014-10-10 06:00:43
【问题描述】:

当用户触摸按钮时,下面的代码会显示图像。我想在没有用户触摸按钮的情况下连续显示不同的图像,例如“熊、猫、牛、狗”。任何人都可以帮助我。

NSMutableArray *dashBoy = [NSMutableArray array];
 for (i = 1; i<= 12; i++)
 {
    butterfly = [NSString stringWithFormat:@"bear_%d.png", i]; 
    if ((image = [UIImage imageNamed:butterfly]))
        [dashBoy addObject:image]; 
} 
[stgImageView setAnimationImages:dashBoy]; 
[stgImageView setAnimationDuration:4.0f];
[stgImageView setAnimationRepeatCount:2];
[stgImageView startAnimating];

【问题讨论】:

  • 你需要证明你已经为解决这个问题付出了一些努力。你做了什么研究?
  • 请不要将代码放在注释中,它很难阅读。编辑您的问题以包含代码,以及您所做的说明。

标签: ios objective-c


【解决方案1】:

你可以这样做:-

首先,创建一个 UIImageView 属性,如下所示,

@property (nonatomic,weak) IBOutlet UIImageView *imageView;    //Also connect it with storyboard or xib.

然后你可以写下IBAction如下:-

- (IBAction)startTap:(id)sender {
[self.imageView setAnimationDuration:1.0f];    //You could change the duration for making animation fast or slow.
[self.imageView setAnimationImages:imageArray];   //imageArray is NSArray of images of which you want to show slide show.
[self.imageView startAnimating];    //simply to start animation.
}

- (IBAction)stopTap:(id)sender {
[self.imageView stopAnimating];    //To stop animation.
}

【讨论】:

    【解决方案2】:

    试试这个.. 创建两个实例对象,如

    NSMutableArray * imagesArray;UIImageView *animationImageView;

     -(void)viewDidLoad
        {
            NSArray *imageNames = @[@"img1.png", @"img2.png", @"img3.png", @"img4.png",@"img5.png"];
            imagesArray = [[NSMutableArray alloc]init];
                NSMutableArray *images = [[NSMutableArray alloc] init];
                for (int i = 0; i < imageNames.count; i++) 
                {
                    [imagesArray addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
                }
                animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 193)];
                animationImageView.animationImages = imagesArray;
                animationImageView.animationDuration = 0.5; // set your own
                [self.view addSubview:animationImageView];
         }
    

    并启动和停止两个UIButton 操作

    -(IBAction)startAnimation:(UIButton*)sender
    {
        [animationImageView startAnimating];
    }
    -(IBAction)stopAnimation:(UIButton*)sender
    {
        [animationImageView stopAnimating];
    }
    

    【讨论】:

    • 为什么投反对票你能给我理由@down voter
    猜你喜欢
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    相关资源
    最近更新 更多