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.

602 questions
7
votes
1 answer

CABasicAnimation not working with UIImageView

I have a UIImageView that is called upon programatically, I am trying to get it to spin around but its not working. The image will be placed inside a dynamic UITableView (I cannot change it to static). The image appears fine in my table view but it…
Ed3121577
  • 510
  • 1
  • 6
  • 13
7
votes
4 answers

Pause and resume CABasicAnimation for Loading Screen?

I have implemented a LoadingLayer class that has some animating elements (2 to be exact) which are animated with CABasicAnimation. Now, this loading screen view persists throughout the whole application. Does hiding the view stop all animations ? Or…
the_critic
  • 12,720
  • 19
  • 67
  • 115
7
votes
1 answer

CABasicAnimation - Setting start stroke position

I'm animating the drawing of a basic circle. This works fine, except the animation begins drawing at the 3 o'clock position. Does anyone know how I can make it start at 12 o'clock? self.circle = [CAShapeLayer layer]; self.circle.fillColor =…
Jonathan
  • 13,947
  • 17
  • 94
  • 123
6
votes
1 answer

Stop CABasicAnimation from being removed after completion

Hey, I have this code snippet (duration is .5, amount is 1.5) CABasicAnimation *grow = [CABasicAnimation animationWithKeyPath:@"transform"]; grow.duration = duration; grow.repeatCount = 0; grow.removedOnCompletion = NO; grow.autoreverses =…
Andrew
  • 1,497
  • 1
  • 14
  • 22
6
votes
2 answers

Stroke Animation with CABasicAnimation for UITextField

I'm trying to add animation to the border of UITextField when edited by the user. The idea is to show line animation in the login page once the first text field is being edited and then after the user switches to the next textfield, the line should…
Maryoomi1
  • 113
  • 1
  • 3
  • 16
6
votes
1 answer

Animate a layer property which simply changes other properties?

Imagine a CAGradientLayer. It's very easy to animate .startPoint and .endPoint. Now imagine a float spinLike which simply sets both of them at once. {So, instead of having two different animations, you could simply animate spinLike.} So something…
Fattie
  • 27,874
  • 70
  • 431
  • 719
6
votes
5 answers

Animate CALayer hide

I'm trying to hide a CALayer after a few microseconds and I'm using CABasicAnimation to animate the hide. At the moment I'm trying to use [aLayer setHidden:YES]; CABasicAnimation * hideAnimation = [CABasicAnimation…
Tom Irving
  • 10,041
  • 6
  • 47
  • 63
6
votes
1 answer

Can animate CALayer's alpha but not backgroundColor

I'd like to animate the background color of a layer. I am able to animate the alpha, but cannot animate the background color. Works: var animation = CABasicAnimation(keyPath: "opacity") animation.toValue = 0.6 animation.duration =…
SimplGy
  • 20,079
  • 15
  • 107
  • 144
6
votes
2 answers

Changing the speed of CALayer in CABasicAnimation while rotating a wheel causes jerk effect

I develop an app which requires a wheel to be rotated around z axis with increasing or decreasing the speed of the wheel steadily over time. I use CABasicAnimation & my code is as follows. While i change the speed property of the layer at particular…
Augus
  • 221
  • 2
  • 9
6
votes
1 answer

How to animate zoomScale on a UIScrollView

I need to set zoomScale back to 1 on a UIScrollView, but I need it to be animated. I thought I could use CABasicAnimation for this, but NSValue doesn't seem to have a "valueForFloat" or "valueForNSNumber", so I'm not sure what to use for…
soleil
  • 12,133
  • 33
  • 112
  • 183
6
votes
6 answers

How to Change Speed while Animation CABasicAnimation

In my application i am using CABasicAnimation for animation. I want to change the speed of the animation dynamically so i have added one slider to change the speed. Following is my animation code. But i am not able to change the speed, when i change…
Prerna chavan
  • 3,179
  • 7
  • 35
  • 77
6
votes
1 answer

CABasicAnimation how to make it easy

I'm currently using the following animation on a UITableViewCell: CATransform3D rotationTransform = CATransform3DMakeRotation(1.0f * M_PI, 0, 0, 1.0); CABasicAnimation* rotationAnimation = [CABasicAnimation…
LightNight
  • 1,616
  • 6
  • 30
  • 59
6
votes
1 answer

Looped, reversible animation using Core Animation

In my iOS 5 app I have a custom UIButton that has a red orb as its image. When the button is tapped I would like the orb to start pulsing/fading between red and green. I have both red and green images and can successfully cross-disolve between the…
Skoota
  • 5,280
  • 9
  • 52
  • 75
5
votes
2 answers

How do I remove a CAShapeLayer and CABasicAnimation from my UIView?

This is my code: @objc func drawForm() { i = Int(arc4random_uniform(UInt32(formNames.count))) var drawPath = actualFormNamesFromFormClass[i] shapeLayer.fillColor = UIColor.clear.cgColor shapeLayer.strokeColor = UIColor.black.cgColor …
5
votes
2 answers

Type 'CGPoint' has no member 'x' when using #keyPath in CABasicAnimation

I am trying to use the #keyPath syntax in order to get a CALayer property to animate it like this: let myAnimation = CABasicAnimation.init(keyPath: #keyPath(CALayer.position.x)) and I get the following error: Type 'CGPoint' has no member 'x' What…
TheoK
  • 3,601
  • 5
  • 27
  • 37