2013-01-10 188 views
1

我有三個圖像,我想在後臺旋轉。以下是我到目前爲止。我想有一個類可以容納所有這些UIImageView並在後臺隨機顯示它們。我讀了關於UIView和框架方法,但我不知道如何添加它們,因爲它只需要一幀。旋轉背景圖像

因此,我用NSArray來代替所有的對象。現在唯一的問題是出現新的背景時,舊的背景不會消失。現在我刪除舊的背景嗎?

這將是巨大的,如果有人能在正確的方向指向我。

謝謝!

@interface ViewController : UIViewController 

@property (strong, nonatomic) NSArray *imageArray; 
@property (strong, nonatomic) UIImageView *imageView; 

- (IBAction)buttonPressed:(UIButton *)sender; 

@end 

// .m文件

@implementation ViewController 
@synthesize imageArray; 
@synthesize imageView; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    imageView = [[UIImageView alloc] init]; 

    UIImage *image1 = [UIImage imageNamed:@"image1.png"]; 
    UIImage *image2 = [UIImage imageNamed:@"image2.png"]; 
    UIImage *image3 = [UIImage imageNamed:@"image3.png"]; 

    imageArray = [[NSArray alloc] initWithObjects:image1, image2, image3, nil]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)buttonPressed:(UIButton *)sender { 

    NSUInteger index = arc4random_uniform(self.imageArray.count); 
    [imageView setImage:[imageArray objectAtIndex:index]]; 

} 
@end 
+0

「只需要一幀。」 - 你期望一個觀點有多個框架?或者是什麼? – 2013-01-10 23:15:59

+1

在後臺只有一個UIImageView,並在UIImageView的圖像(UIImage)屬性上進行切換。 –

+0

凱恩,我可以在哪裏閱讀以瞭解您推薦的內容?謝謝! – user1968349

回答

0

此:

[self.view insertSubview:current atIndex:0]; 

是加入另一種觀點認爲,以您的視圖層次,但是你是不是刪除您以前放置的一個。所以你有一堆不斷增加的UIVews。您還將新視圖置於堆棧的底部。

嘗試

[[[self.view subviews] objectAtIndex:[[self.view subviews] count]-1] removeFromSuperview]; 
[self.view addSubview:current]; 

或者更好 - 因爲@Kaan建議 - 有一個單一的UIImageView,只是改變它的UIImage的財產。

要做到這一點,你的陣列將包含UIImages不是你的UIImageViews。

你的UIView可以一個的UIImageView,也可以包含一個UIImageView。

的UIImageView的有,你可以將圖像設置屬性。

您的代碼將是這個樣子......

self.imageArray = [[NSArray alloc] initWithObjects:image1, image2, image3, nil]; 
NSUInteger index = arc4random_uniform(self.imageArray.count); 
UIImage *current; 
current = [self.imageArray objectAtIndex:index]; 
self.imageview.image = current; 
+0

嗨,他,我在哪裏可以閱讀以瞭解如何更改UIImageView的UIImage屬性?謝謝! – user1968349

+0

在[apple docs](https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImageView_Class/Reference/Reference.html) – foundry

+0

@ user1968349 - 查看我更新的答案。這是清楚的嗎?如果是這樣,請接受答案(勾選) – foundry

0

在你的方法,你已經創建了6個對象 - 3個UIImages和3個UIImageViews的。

你想要什麼可以使用4個對象而不是6個,使用更少的內存,使你的應用程序運行更快並同時回答你的問題(另外,我假設你有所有相同的背景圖像大小,設備屏幕的大小)。

我建議首先創建,只有一個的UIImageView:

//backgroundImage is an IVAR 
backgroundImage = [[UIImageView alloc] init]; 

跟着三個UIImages並將它們放置在一個NSArray:

UIImage *image1 = [UIImage imageNamed:@"image1.png"]; 
UIImage *image2 = [UIImage imageNamed:@"image2.png"]; 
UIImage *image3 = [UIImage imageNamed:@"image3.png"]; 
//imagesArray is an IVAR 
imagesArray = [[NSArray alloc]] initWithObjects: image1, image2, image3, nil]; 

最後,要改變的背景下,調用隨機函數並更新UIImageView圖像屬性,而不是彈出視圖堆棧頂部的不同視圖:

NSUInteger index = arc4random() % [imagesArray count]; 
[backgroundImage setImage:[imagesArray objectAtIndex:index]]; 
+0

這並不會在每次IBAction發生時更改背景圖像。 – user1968349

+0

代碼似乎從我的結束,也許錯誤是在執行?確保backgroundImage是您班級的IVAR。 – achi

+0

嗨Eli,我在哪裏稱視圖? – user1968349