2011-10-07 64 views
-2

如何在iPhone編程中使用NSTimer定期更改圖像?NSTimer change image iPhone編程

我爲圖像加載創建圖像視圖。

我想要在imageview中顯示圖像,並通過使用NSTimer定期更改圖像。

回答

0

導入你喜歡的圖片到你的應用程序捆綁叫 他們image1.png,image2.png image3.png等等....

Interface Builder中添加的UIImageView調用它(參考) 「theImage」

在H文件

@interface delme3ViewController : UIViewController { 

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

@end 

在M檔:

- (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]]; 
} 
0

您還可以使用圖像名稱數組而不是重命名包中的圖像。 :)

+1

或者可以使用一個UIImage對象數組。 (儘管由於系統緩存圖像的方式,這通常不會帶來任何好處。) –

1

而不是使用NSTimer,我只會使用UIImages的arrary和動畫他們。請參閱文檔from here

+1

該方法的結果是使用許多大型圖像更平滑的動畫。 – ozz

+0

我將如何使用此淡入淡出圖像?使時間間隔更長一點,而且哪個方法會調用fadeinandout/alpha更改爲uiimageview? – jsetting32

+0

鏈接已死... – Chris