NSTimer change image iPhone Programming

倖福魔咒の 提交于 2019-12-02 23:26:03

问题


How to change images periodically using NSTimer in iPhone progranmming?

I create an image view for loading images.

I want to display images in imageview and periodically change images by using NSTimer.


回答1:


import the images you like to your bundle application called them image1.png, image2.png image3.png and so on....

Add UIImageview in interface builder call it (reference) "theImage"

in the H file:

@interface delme3ViewController : UIViewController {

    UIImageView *theImage;
    int imageCount;
}
@property (nonatomic, retain) IBOutlet UIImageView *theImage;

@end

in the M file:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [NSTimer scheduledTimerWithTimeInterval:1 
                                     target:self 
                                   selector:@selector(animateFunction) 
                                   userInfo:nil 
                                    repeats:YES];
    imageCount = 1;

}

-(void)animateFunction
{
    NSLog(@"Timer heartbeat %i",imageCount);
    imageCount = imageCount + 1;
    NSString *imageName = [NSString stringWithFormat:@"image%i.png"];
    [theImage setImage:[UIImage imageNamed:imageName]];
}



回答2:


Instead of using NSTimer, I would just use an arrary of UIImages and animate them. See the documentation from here.




回答3:


you can also use array of image names rather renaming images in your bundle. :)



来源:https://stackoverflow.com/questions/7686642/nstimer-change-image-iphone-programming

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!