Reputation: 13916
I'm trying to do a customized animation on view opacity like this:
I want to repeat step 1 to 4 indefinitely: 1, 2, 3, 4, 1, 2, 3, 4,.....
Here is what I tried:
[UIView animateWithDuration:1
delay:5
options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat|UIViewAnimationOptionCurveEaseInOut
animations:^{
self.imageView.layer.opacity = 0;
}
completion:nil
];
But the delay only appeared once at the beginning, what I ended up with is:
1, 2, 4, 2, 4, 2, 4,.....
Upvotes: 5
Views: 5981
Reputation: 154
This is an old question, but little has changed and it came up in my search, so here is a suggestion I think is better. NSTimer
s are unnecessary, the same effect can be achieved using the keyframe animation methods on UIView
.
Swift 5
// durations in seconds [ pause 5s | fade out 1s | pause 5s | fade in 1s] = 12s total
let fadeDuration: Double = 1
let delayDuration: Double = 5
let totalDuration: Double = delayDuration + fadeDuration + delayDuration + fadeDuration
// convert to relative times for key frames
let fadeRelativeDuration: Double = fadeDuration / totalDuration
let firstStartRelativeTime: Double = delayDuration / totalDuration
let secondStartRelativeTime: Double = (delayDuration + fadeDuration + delayDuration) / totalDuration
UIView.animateKeyframes(withDuration: totalDuration, delay: 0, options: [.repeat], animations: {
UIView.addKeyframe(withRelativeStartTime: firstStartRelativeTime, relativeDuration: fadeRelativeDuration) {
self.imageView.layer.opacity = 0
}
UIView.addKeyframe(withRelativeStartTime: secondStartRelativeTime, relativeDuration: fadeRelativeDuration) {
self.imageView.layer.opacity = 1
}
})
You will still need to restart the animation after the app is backgrounded, e.g. by observing UIApplicationWillEnterForegroundNotification
in your view or view controller.
Upvotes: 2
Reputation: 13916
@user1980004 Thanks for the hint on NSTimer. I'll just post the complete working code for my question:
// interface
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) NSTimer *animationTimer;
-(void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self glowAnimation]; // Timer fires after a delay, so I need to fire animation for the first time
// The interval value is determined by the sum of delay and duration for both the forward and reverse animation in glowAnimation function;
self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:12 target:self selector:@selector(glowAnimation) userInfo:nil repeats:YES];
}
-(void) viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
if (self.animationTimer){
[self.animationTimer invalidate]; // Cancel the timer schedule
self.animationTimer = nil;
}
}
-(void) glowAnimation{
// Forward and reverse animation are chained with delay in each.
[UIView animateWithDuration:1
delay:5
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.imageView.layer.opacity = 0;
}
completion:^(BOOL finished){
[UIView animateWithDuration:1
delay:5
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.imageView.layer.opacity = 1;
}
completion:nil ];
}
];
}
All together the 1, 2, 3, 4, 1, 2, 3, 4 animation sequence in the question can be achieved.
Upvotes: 1
Reputation: 220
I had the same problem. I solved in this way, with use of NSTimer
[NSTimer scheduledTimerWithTimeInterval: 2
target: self
selector:@selector(moveImage: )
userInfo: nil repeats:YES];
}
-(void) moveImage: (NSTimer*)timer {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
Upvotes: 8