UIView.animateWithDuration swift loop animation
No need to do the completion block approach, just use the animation options argument:
updated for Swift 3.0
UIView.animate(withDuration: 2.0, delay: 0, options: [.repeat, .autoreverse], animations: {
coloredSquare.frame = CGRect(x: 120, y: 220, width: 100, height: 100)
}, completion: nil)
If for any reason you want to stop the animation later, just use:
coloredSquare.layer.removeAllAnimations()
Swift UIView.animateWithDuration and For-Loops
This is a tricky error (note spaces around ..<
).
for index in startingIndex ..< lowerViews.count {
will work or
for index in startingIndex..<lowerViews.count {
will work but:
for index in startingIndex..< lowerViews.count {
won't work.
The reason for this is the fact that when startingIndex..<
is used, then ..<
is considered to be a postfix (unary) operator (not an infix operator). Therefore the whole expression stops making sense and you start getting strange errors.
Also see what are the rules for spaces in swift
How to make UIView animation sequence repeat and autoreverse
To animation from point 1 to 2 to 3 to 2 to 1 and repeat, you can do use animateKeyframesWithDuration
in iOS 7 and later:
someView.frame = frame1;
[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
someView.frame = frame2;
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
someView.frame = frame3;
}];
} completion:nil];
If using auto-layout, you can animate the changing of the constraint constants:
[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
topConstraint.constant = 200;
leftConstraint.constant = 200;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
topConstraint.constant = 100;
leftConstraint.constant = 300;
[self.view layoutIfNeeded];
}];
} completion:nil];
Or, the approach with auto layout is to deactivate the constraints, and then you can animate using frame
values or what have you.
In earlier versions of iOS, you can use CAKeyframeAnimation
, for example to animate along a path:
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(100.0, 100.0)];
[path addLineToPoint:CGPointMake(200.0, 200.0)];
[path addLineToPoint:CGPointMake(100.0, 300.0)];
CAKeyframeAnimation *animatePosition = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animatePosition.path = [path CGPath];
animatePosition.duration = 1.0;
animatePosition.autoreverses = YES;
animatePosition.repeatCount = HUGE_VALF;
[self.someView.layer addAnimation:animatePosition forKey:@"position"];
You can do this with however many points you want. This also useful technique if you want to animate along a curved path (e.g. a circle or bezier curve).
To just animate between two points, you can use animateWithDuration:delay:options:animations:completion:
, such as:
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut
animations:^{
// do whatever animation you want, e.g.,
someView.frame = someFrame1;
}
completion:NULL];
This animates the movement of someView
from the starting frame to someFrame1
and back.
By the way, using UIViewAnimationOptionCurveEaseInOut
in conjunction with UIViewAnimationOptionAutoreverse
and UIViewAnimationOptionRepeat
will give you a smoother effect as the animation reverses and repeats.
Chaining UIView animations (in a loop)
That is what the completion
argument is for. Chain your animations by starting the next animation from the completion handler for the previous animation.
UIView Animation Options Repeat count
You can use the following to achieve your goal :
UIView.animateKeyframes(withDuration: 2, delay: 0, options: [], animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5) {
self.ball.alpha = 0.0
}
UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) {
self.ball.alpha = 1
}
}) { (isFinished) in
}
Related Topics
How to Deselect a Selected Uitableview Cell
Scrollview and Keyboard in Swift
Change the Font of a Uibarbuttonitem
Width and Height Equal to Its Superview Using Autolayout Programmatically
How to Change Height of Grouped Uitableview Header
Differencebetween the App Id and the Bundle Id? Where Is the App Id in the Xcode Project
Frosted Glass (iOS 7 Blur) Effect
How to Convert Nsinteger to Nsstring Datatype
Pod Install Displaying Error in Cocoapods Version 1.0.0.Beta.1
How to Dismiss Keyboard When Touching Anywhere Outside Uitextfield (In Swift)
Uitableviewrowaction Title as Image Icon Instead of Text
What Dpi Resolution Is Used for an iPhone App
Change App Version with Only IPA File Provided (No Xcode)
How to Cache Using Nsurlsession and Nsurlcache. Not Working
Detect App Crashed During Load/Last Time It Was Run
Icloud + Coredata - How to Avoid Pre-Filled Data Duplication
Is There a Public API for Card View UI That Can Be Seen Across iOS 10