2015-08-15 208 views
3

我想讓UIImageView眨眼viewDidLoad。我不確定要做到這一點的最佳方法是什麼。我試過使用循環.hidden=YES.hidden=NO,但這似乎是一個不好的方法來做到這一點。我需要一些適當的建議。UIImageView閃爍動畫

回答

2

如果你只想隱藏和顯示然後使用的NSTimer爲此目的 這樣的東西

in .h file添加此屬性 @property(nonatomic,strong)NSTimer * timer;

- (void)onTimerEvent:(NSTimer*)timer 
{ 
    self.imageView.hidden = !self.imageView.hidden; 
} 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimerEvent:) userInfo:nil repeats:YES]; 
} 

斯威夫特

func onTimerEvent(timer: Timer) { 
    self.imageView.isHidden = !self.imageView.isHidden 
} 

public override func viewDidLoad() { 
    super.viewDidLoad() 
    Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(onTimerEvent(timer:)), userInfo: nil, repeats: true) 
} 

但可以UIImageView的動畫不同的圖像,以及 像這樣

self.iamgeView.animationImages = <arrayOfImages> 
self.imageView.duration = <duration> 
+0

我想這並得到一個錯誤信息:在類型爲'DemoViewController *'的對象上找不到屬性「timer」 – Owen

+0

需要在demoViewController.h中添加NStimer * timer作爲屬性 –

+0

wo謝謝你! – Owen

3

您可以使用您的UIImageView的阿爾法。下面是一個例子(只需撥打startAnimviewDidLoad):

static NSInteger count = 0; 
static NSInteger maxBlind = 10; 

- (void)startAnim{ 

    CGFloat animDuration = 0.5f; 
    [UIView animateWithDuration:animDuration 
        animations:^{ 
         self.myImageView.alpha = 0.f; 
        } completion:^(BOOL finished) { 
         [UIView animateWithDuration:animDuration 
              animations:^{ 
               self.myImageView.alpha = 1.f; 
              } completion:^(BOOL finished) { 
               if (count < maxBlind){ 
                [self startAnim]; 
                count++; 
               } 
              }]; 
        }]; 
} 
+0

上面是很好,謝謝,但缺少一條線。您需要刪除第二個完成塊中的imageView,否則它將保持原樣,淡入淡入/淡出: }完成:^(BOOL finished){ if(count user938797

5

試試這個:

-(void)blink:(UIView*)view count:(int) count 
{ 
    if(count == 0) 
    { 
     return; 
    } 

    [UIView animateWithDuration:0.2 animations:^{ 

     view.alpha = 0.0; 

    } completion:^(BOOL finished){ 

     [UIView animateWithDuration:0.2 animations:^{ 

      view.alpha = 1.0; 

     } completion:^(BOOL finished){ 

      [self blink:view count:count-1]; 

     }]; 


    }]; 
} 

,或者如果你希望它永遠閃爍試試這個:

-(void)blinkForever:(UIView*)view 
{ 
    [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{ 

     view.alpha = 0.0; 

    } completion:nil]; 
} 
+0

我喜歡這個解決方案。但我有個問題。如何讓它閃爍爲黑色而不是背景中的任何內容?將alpha設置爲0只會使其透明並顯示該視圖背景中的圖像。我嘗試通過一個UIImage視圖,其背景顏色是黑色的,但沒有幫助。我甚至嘗試將該視圖的圖像設置爲黑色圖像,但不會生成動畫。有任何想法嗎? – frakman1

+0

您可以將閃爍視圖放在另一個背景爲黑色的超級視圖中,但閃爍視圖在Alpha等於1.0時應該完全不透明,否則黑色背景將會顯示。 –