UIImageView 闪烁动画

UIImageView blinking animation

我正在尝试让 UIImageViewviewDidLoad 上闪烁。我不确定最好的方法是什么。我试过使用 .hidden=YES.hidden=NO 的循环,但这似乎是一种不好的方法。我需要一些适当的建议。

使用 UIImageView 动画,你可以只放入一个空图像或空白图像:

http://spin.atomicobject.com/2014/08/27/animate-images-uiimageview-completion-handler/

尽情享受

如果你只想隐藏和显示然后使用 NStimer 为此目的 像这样

在 .h 文件中添加这个 属性 @属性(非原子,强)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];
}

Swift:

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>

您可以使用 UIImageView 的 alpha。这是一个示例(只需在 viewDidLoad 中调用 startAnim):

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++;
                                              }
                                          }];
                     }];
}

试试这个:

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