Search code examples
iosobjective-cuiimageviewviewdidload

UIImageView blinking animation


I am trying to make a UIImageView blink upon viewDidLoad. I am not sure what the best way to do this is. I've tried using loops with .hidden=YES and .hidden=NO but this seems like a bad way to do this. I need some proper advice.


Solution

  • If you have just want to hide and show then use NStimer For that purpose something like this

    in .h file add this property @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];
    }
    

    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)
    }
    

    But UIImageView can animate different images as well something like this

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