Questions tagged [cabasicanimation]

"CABasicAnimation" is the most basic explicit animation in Core Animation. By simply specifying a "from" value, a "to" value and optionally other parameters like the duration you can have hardware accelerated animations in just a few lines of code.

CABasicAnimation is the most basic explicit animation in Core Animation (). By simply specifying a from value, a to value and optionally other parameters like the duration you can have hardware accelerated animations in just a few lines of code.

Animations are created with the key path that they should be animating like

CABasicAnimation *moveAnimation = 
↪   [CABasicAnimation animationWithKeyPath:@"position"];

After creating a new animation it is configured. Common configurations include setting the values to animate to and from, setting a duration and changing the timing function (how the animation is animated). Note that Core Graphics values have to be wrapped into NSValue objects when used.

// valueWithCGPoint: for iOS and valueWithPoint: for OS X.
[moveAnimation setFromValue:[NSValue valueWithCGPoint:myStartPoint]]; 
[moveAnimation setToValue:[NSValue valueWithCGPoint:myEndPoint]]; 
[moveAnimation setDuration:3.0]; // 3 seconds
[moveAnimation setTimingFunction:
↪   [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

When the animation is configured it is applied by adding it to the layer that should animate. Note that the key parameter when adding the animation has nothing to do with the key path of the animation. It is only used to reference the animation after is has been added to the layer.

[myLayer addAnimation:moveAnimation 
               forKey:@"myMoveAnimation"];

Note that explicitly animating a property like this doesn't change the value of the actual property and after the animation finishes it will remove itself and the layer will jump back to the state it had before the animation. To also change the value of the property, explicitly change the value in the same before or after adding the animation to the layer.

One powerful feature even custom properties can be animated the same way (by specifying the key path).

Basic animations is available on both (since iOS 2.0) and (since OS X 10.5 "Leopard") and is part of the framework.

571 questions
156
votes
15 answers

CABasicAnimation resets to initial value after animation completes

I am rotating a CALayer and trying to stop it at its final position after animation is completed. But after animation completes it resets to its initial position. (xcode docs explicitly say that the animation will not update the value of the…
Nilesh Ukey
  • 5,232
  • 3
  • 18
  • 22
57
votes
2 answers

How to delay a CABasicAnimation?

I have a CABasicAnimation and want to start it after a delay. In UIKit I can specify delays. The CAMediaTiming protocol has a timeOffset property but I can't see an effect. My next try is to use GCD to delay it but it feels like overkill.
openfrog
  • 37,829
  • 61
  • 213
  • 364
51
votes
6 answers

Is there a way to pause a CABasicAnimation?

I have a basic spinning animation of the iPhone. Is there any way that I can "pause" the animation so that the position of the view will be maintained? I guess one way of doing this would be to cause the animation to "complete" instead of calling…
mclaughj
  • 11,017
  • 4
  • 29
  • 37
42
votes
2 answers

CABasicAnimation unlimited repeat without HUGE_VALF?

I'm trying to perform auto repeat of my image rotation animation with CABasicAnimation. I have tried to search on web how to set such property but was unable to find that. Is it really no such property for CA animation? I know that you can set some…
Centurion
  • 13,168
  • 31
  • 99
  • 189
41
votes
3 answers

how to repeat animation forever in Swift (HUGE_VALF)?

According to the docs, the way to repeat a CABasicAnimation forever is to set its repeatCount to HUGE_VALF. But in Swift, HUGE_VALF causes a compile error. Swift doesn't seem to know about the standard library (or wherever this constant…
matt
  • 447,615
  • 74
  • 748
  • 977
40
votes
3 answers

After rotating a CALayer using CABasicAnimation the layer jumps back to it's unrotated position

I am trying to create a falling coin. The coin image is a CALayer with 2 CABasicAnimations on it - a falling down and a rotation one. When the falling down animation gets to its end, it stays there. The rotation animation though, which is supposed…
Mikle
  • 1,777
  • 3
  • 19
  • 32
34
votes
3 answers

Core Animation is not working with "alpha" value

Before this code, my movie pic alpha is set to 0, CABasicAnimation* fadein= [CABasicAnimation animationWithKeyPath:@"alpha"]; [fadein setToValue:[NSNumber numberWithFloat:1.0]]; [fadein setDuration:0.5]; [[moviepic…
5argon
  • 2,697
  • 3
  • 23
  • 50
22
votes
4 answers

iOS 8 animation bug

I have a simple method for animate view. -(void)animateSelf { CABasicAnimation * animation; animation = [CABasicAnimation animationWithKeyPath:@"position.y"]; // settings ... [self.view.layer addAnimation:animation…
Oleg Sobolev
  • 3,176
  • 2
  • 14
  • 28
22
votes
5 answers

How to chain different CAAnimation in an iOS application

I need to chain animations, CABasicAnimation or CAAnimationGroup but I don't know how to do it, the only that I do is that all the animation execute at the same time for the same layer. How could I do it? For example, a layer with its contents set…
torhector2
  • 433
  • 1
  • 5
  • 19
21
votes
2 answers

How can I know the values in CABasicAnimation keyPath

I find some code like this: CABasicAnimation *anim = [CABasicAnimation animation]; anim.keyPath = @"transform.scale"; anim.fromValue = [NSNumber numberWithFloat:1.0]; anim.toValue = [NSNumber numberWithFloat:0]; anim.removedOnCompletion =…
Sailing
  • 327
  • 1
  • 2
  • 9
20
votes
3 answers

CABasicAnimation with CALayer path doesn't animate

I'm trying to animation the transition of a CALayer from normal corners to rounded corners. I only want to round the top corners, so I am using a UIBezierPath. Here's the code: UIRectCorner corners = UIRectCornerTopLeft |…
eric.mitchell
  • 8,501
  • 12
  • 50
  • 91
19
votes
2 answers

Infinite CABasicAnimation stops

I 've an infinite animation (rotating an image, type: CABasicAnimation) in my UIView with animation.repeatCount = HUGE_VALF; When I push a new ViewController and go back to the initial ViewController with the animation containing View inside, the…
pinki
  • 844
  • 1
  • 11
  • 28
17
votes
1 answer

When to use UIView animation, CABasicAnimation and UIViewPropertyAnimator?

I wanted to know in which scenarios we should use any of them as the best fit? In the following blog https://www.hackingwithswift.com/ios10, the example written in "Animations revisited" section, can we re-apply the same requirement using…
Raj
  • 911
  • 1
  • 12
  • 26
17
votes
2 answers

replacing CALayer and CABasicAnimation with SKScene and SKActions

I am trying to restructure this Github Swift project on Metaballs so that the circles are represented by SKShapeNodes that are moved around by SKActions instead of CABasicAnimation. I am not interested in the various Metaball parameters (the…
OnlyCodeMatters
  • 653
  • 1
  • 5
  • 13
17
votes
1 answer

How do you animate the sublayers of the layer of a UIView during a UIView animation?

In a UIView animation for a view, you can animate its subviews being laid out by including UIViewAnimationOptionLayoutSubviews in the options parameter of [UIView animateWithDuration:delay:options:animations:completion:]. However, I cannot find a…
ememem
  • 928
  • 1
  • 10
  • 22
1
2 3
38 39