2009-07-27 78 views
1

我只是想實現一個簡單的動畫,其中一個圖片在應用程序開始後幾秒鐘內褪色,然後它應該淡出並完全消失。我從來沒有在iPhone上做任何動畫,所以我被卡住了。當使用像image.opacity = 1.0;像開發者文檔,我得到錯誤:簡單的動畫與NSTimer和不透明度的iPhone

request for member 'opacity' in something not a structure or union.

回答

4

淡出圖像,你要類似的代碼:

[UIView beginAnimations]; 
myImageView.opacity = 0.0; 
[UIView commitAnimations]; 

你沒有設置圖像本身的不透明度以至於包含圖像的UIImageView(在本示例中爲myImageView)的不透明度。您也可以通過添加一條線控制動畫的持續時間:

[UIView setAnimationDuration: 1.5]; 
調用之間

-beginAnimations-commitAnimations

2

你在找什麼是屬性「alpha」,如view.alpha。你只是在尋找錯誤的名字。

法定阿爾法值從0.0(完全透明)到1.0(完全不透明)。它會做你正在尋找的。

使用上面評論中引用的beginAnimations/commitAnimations範例。

0

總結其他答案:

淡出圖像,你要類似的代碼:

// This line may not be necessary. 
myImageView.alpha = 1.0f; 

[UIView beginAnimations]; 
[UIView setAnimationDuration: 1.5]; 
myImageView.alpha = 0.0; 
[UIView commitAnimations]; 
0

上述解決方案並沒有在我的項目工作。我不得不做這樣....

**[UIView beginAnimations:@"myPic.png" context:nil];** 
    //You need to set the pic name 

[UIView setAnimationDuration: myTimer]; 
_myPic.alpha = 1.0f; 
_myPic.alpha = 0.0; 
[UIView commitAnimations]; 

這是淡出圖像

問候 菲利普

0

看來要在淡入,然後淡出。但持續時間過後可以踢球。不僅僅是動畫本身的持續時間。

我不準備iOS的東西,所以原諒我,如果我的代碼在錯誤的地方。

這是在模擬器和單視圖應用程序中測試的。

我向ViewController的UIView添加了一個新的UIview,並將其鏈接到它的.h文件作爲插座。 (將其命名爲View)

然後將UImageView添加到新的UIView(theView)中,並使用屬性檢查器爲UImageView設置圖像。

enter image description here

然後加入代碼到ViewController.m文件。

我想告訴你的主要事情是

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay

它調用的延遲後,使用默認的模式在當前線程上的接收器的方法。

(還有類似的方法,可以在其它線程執行...)

我已淡出兩種方法之一,在凋謝之一。

有兩個performSelectors。

當視圖加載時,第一個被調用。

第二個是在fadeIn動畫塊中。您可以將其放置在viewDidLoad中或塊之後。但我認爲將它放在原地更安全。

這裏是.m文件代碼。

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

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

    _theView.alpha = 0.0; 
    [self performSelector:@selector(fadeIn) withObject:nil afterDelay:2.0]; 
} 


- (void)fadeToBlack 
{ 
    [UIView animateWithDuration:3.0 
          delay:0.0 
         options:UIViewAnimationOptionCurveEaseIn 
        animations:^{_theView.alpha = 0.0;} 
        completion:nil]; 
} 

- (void)fadeIn 
{ 

    [UIView animateWithDuration:3.0 
          delay:0.0 
         options:UIViewAnimationOptionCurveEaseIn 
        animations:^{_theView.alpha = 1.0; [self performSelector:@selector(fadeToBlack) withObject:nil afterDelay:10.0];} 
        completion:nil]; 


} 


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

@end 

我在這裏期待改正......因爲這是不是真的,我完全熟悉(ViewControllers),但其作品在SIM確定的區域。