CABasicAnimation with zero duration - calayer

I have a CALayer in an AVMutableComposition that is faded in, should stay on screen for a while and then disappear. The problem is, it should disappear without an animation, but CABasicAnimation has a minimum duration of 0.25 seconds.
How would can I achieve to set the opacity of the layer after a given time without animating it?

Encapsulating the removal of the layer into a Core Animation transaction where you disable animations:
[CATransaction begin];
[CATransaction setDisableActions:YES];
// remove the layer from its hierarchy
[CATransaction commit];
or the same in Swift:
CATransaction.begin()
CATransaction.setDisableActions(true)
// remove the layer from its hierarchy
CATransaction.commit()

Related

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 forKey:#"position.y"];
animation = [CABasicAnimation animationWithKeyPath:#"transform.rotation.z"];
// settings ...
[self.view.layer addAnimation:animation forKey:#"transform.rotation.z"];
[UIView animateWithDuration: 1.0 animations:^{
CGRect rect = self.view.frame;
rect.origin.y += 800;
self.view.frame = rect;
} completion:nil];
}
For iOS 7 it worked well. But for iOS 8 animation behaves unpredictably. Is there a way to combine these animations for iOS 8?
I tried to replace animateWithDuration: by another CABasicAnimation, but it did not help.
The view.frame logs are correct, but the animation of view.frame jumps out of obscure origin.
After removing CABasicAnimation for position.y (the first one) bug is gone.
You have three animations:
You animate the layer's position
You animate the layer's transform
You animate the view's frame which in turn animates the layer's position.
Animations 1 and 3 collide.
On iOS 7 the behavior is that animation 3 cancels animation 1.
On iOS 8 the behavior is that animations 1 and 3 run concurrently ("Additive Animations").
I suggest you just remove animation 1 and also check out the related WWDC 2014 video (I think it was Building Interruptible and Responsive Interactions).
What worked for me was disabling autolayouts on the view and THE SUBVIEWS of the view I was animating at some point before doing the animation. Disabling autolayouts from the storyboard was not enough in iOS8.
[viewToAnimate removeFromSuperview];
[viewToAnimate setTranslatesAutoresizingMaskIntoConstraints:YES];
//addSubview again at original index
[superView insertSubview:viewToAnimate atIndex:index];
This example might help you, I wish I had discovered it before wasting hours. Rather than animate the frame, animates its contraints. Click on the auto layout constraint you would like to adjust (in interface builder e.g top constraint). Next make this an IBOutlet;
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;
Animate upwards;
self.topConstraint.constant = -100;
[self.viewToAnimate setNeedsUpdateConstraints];
[UIView animateWithDuration:1.5 animations:^{
[self.viewToAnimate layoutIfNeeded];
}];
Animate back to original place
self.topConstraint.constant = 0;
[self.viewToAnimate setNeedsUpdateConstraints];
[UIView animateWithDuration:1.5 animations:^{
[self.viewToAnimate layoutIfNeeded];
}];
Originally posted by me here
So you would adjust the constraint for self.view.frame in your example.
I also got an issue of little nasty differences between iOS7 and iOS8 animation.
In most cases it was broken it was either:
single combination of Scale, Transform and Rotate CGAffineTransforms - the result was dependant on iOS version
or complex sequence of animations on different views - some views were 'reseting' their positions before commencing a new piece of animations. About 5% of animation pieces were affected.
I'm pretty sure there were no simultaneous animations on the problematic views.
Autolayout and constraints suggestions did not help (moreover, all animated views were create in code as autolayout interfered with animation a lot even before iOS8).
What turned out to be a universal solution for both problems is to put the problematic views into a wrapper view and use it to split-off Rotation animation or to do the animation that causes 'reset' effect. Now it functions the same in 7.1.1 and 8.1.1.

Pausing a SceneKit animation

I'm trying to create a test app in which the user can pause an animation by clicking in the SceneView. The SceneView loads the animation from a .dae file created in a 3d app (Cinema 4D). The app successfully plays and loops the animation upon launch.
To pause the animation, I used Technical Q&A QA1673 as a reference. In the case of this .dae file, the animation actually comes in as a hierarchy of animations, so I have tried reaching down to each underlying CAKeyframeAnimation and setting its speed to zero. My code currently looks like this:
- (void)mouseDown:(NSEvent *)event {
SCNNode *cubeNode = [self.scene.rootNode childNodeWithName:#"C4D_Cube" recursively:YES];
CAAnimation *cubeAnimation = [cubeNode animationForKey:#"Cube_Anim_01-02-1"];
CAAnimationGroup *cubeAnimationGroup = (CAAnimationGroup *)cubeAnimation;
// cubeAnimationGroup contains 3 CAAnimationGroups, each of which contains a CAKeyframeAnimation.
// So I directly access each CAKeyframeAnimation and set its speed to zero.
for (CAAnimationGroup *subGroup in [cubeAnimationGroup animations]) {
CFTimeInterval pausedTime = CACurrentMediaTime();
[[subGroup animations] setValue:#0.0 forKey:#"speed"];
[[subGroup animations] setValue:[NSNumber numberWithFloat:pausedTime] forKey:#"timeOffset"];
}
}
When I set a breakpoint, I can see that the speed of the keyframe animations does change from 1 to 0, but the animation continues to play at its normal speed in the scene view. I originally tried just setting the speed on the top level CAAnimationGroup to zero, but this also had no effect. What's the correct way to pause an animation in progress?
The animations returned by "animationForKey:" are copies of the running animations.
The documentation says "Attempting to modify any properties of the returned object will result in undefined behavior."
So you could do something like this instead:
for(NSString *key in [myNode animationKeys]){
CAAnimation *animation = [myNode animationForKey:key];
[animation setSpeed:0]; //freeze
[animation setTimeOffset:CACurrentMediaTime() - [animation beginTime]]; //move back in time
[cube addAnimation:animation forKey:key]; //re-add the animation with the same key to replace
}
Note that if you just want to pause all the animations coming from a .DAE you might want to do:
[mySCNView setPlaying:NO]; //pause scene-time based animations
Or you could set paused to true.
In Swift:
mySCNView.scene?.paused = true
SCNScene has [isPaused][1] property which you can set. BTW, so does SKScene cause it is a SKNode.

How to detect intersect of two image frames while animating?

In my UIView, I've created a UIImageView called targetView where I have set it to a series of animations:
[UIView animateWithDuration:1.0
animations:^{self.targetView.center = CGPointMake(220,20);}
completion:^(BOOL finished){
//another animation will begin;
}];
I also created another UIImageView called shootView which will move to the target direction upon a finger swipe. Its movement is also implemented as an animation. At the end of its animation, detect the intersect with the targetView:
[UIView animateWithDuration:1.0
animations:^{
self.shootView.center = CGPointMake(destX, destY);}
completion:^(BOOL finished){
if (CGRectIntersectsRect(self.targetView.frame, self.shootView.frame)) {
//do something
}];
Now there is a problem: the intersect command only works fine if the targetView has reached the end point of the current animation and the shootView happens to be there. While the targetView is on the move somewhere in the middle of an animation, no intersect can be detected even if visually it is very obvious that the two frames are intersecting.
This approach might be tricky since you can only get callbacks on the start and end of UIView animations. In your example the CGRectIntersectRect method is only being called after the animation has completed.
One solution may be to use an NSTimer to animate the position of the shootview, and with each change of position do the collision detection. i.e
[NSTimer timerWithTimeInterval: 0.03 target: self selector: #selector(timerTicked:) userInfo: nil repeats: YES];
-(void) timerTicked: (NSTimer*) timer {
// do move
// check for colisions
// optional invalidation of timer
}

Explicit animation of NSView using core animation

I'm trying to slide in a NSView using core animation. I think I need to use explicit animation rather than relying on something like [[view animator] setFrame:newFrame]. This is mainly because I need to set the animation delegate in order to take action after the animation is finished.
I have it working just fine using the animator, but as I said, I need to be notified when the animation finishes. My code currently looks like:
// Animate the controlView
NSRect viewRect = [controlView frame];
NSPoint startingPoint = viewRect.origin;
NSPoint endingPoint = startingPoint;
endingPoint.x += viewRect.size.width;
[[controlView layer] setPosition:NSPointToCGPoint(endingPoint)];
CABasicAnimation *controlPosAnim = [CABasicAnimation animationWithKeyPath:#"position"];
[controlPosAnim setFromValue:[NSValue valueWithPoint:startingPoint]];
[controlPosAnim setToValue:[NSValue valueWithPoint:endingPoint]];
[controlPosAnim setDelegate:self];
[[controlView layer] addAnimation:controlPosAnim forKey:#"controlViewPosition"];
This visually works (and I get notified at the end) but it looks like the actual controlView doesn't get moved. If I cause the window to refresh, the controlView disappears. I tried replacing
[[controlView layer] setPosition:NSPointToCGPoint(endingPoint)];
with
[controlView setFrame:newFrame];
and that does cause the view (and layer) to move, but it is corrupting something such that my app dies with a seg fault soon afterwards.
Most of the examples of explicit animation seem to only be moving a CALayer. There must be a way to moving the NSView and also being able to set a delegate. Any help would be appreciated.
Changes made to views take effect at the end of the current run loop. The same goes for any animations applied to layers.
If you animate a view's layer, the view itself is unaffected which is why the view appears to jump back to its original position when the animation completes.
With these two things in mind, you can get the effect you want by setting the view's frame to what you want it to be when the animation is done and then adding an explicit animation to the view's layer.
When the animation begins, it moves the view to the starting position, animates it to the end position and when the animation is done, the view has the frame you specified.
- (IBAction)animateTheView:(id)sender
{
// Calculate start and end points.
NSPoint startPoint = theView.frame.origin;
NSPoint endPoint = <Some other point>;
// We can set the frame here because the changes we make aren't actually
// visible until this pass through the run loop is done.
// Furthermore, this change to the view's frame won't be visible until
// after the animation below is finished.
NSRect frame = theView.frame;
frame.origin = endPoint;
theView.frame = frame;
// Add explicit animation from start point to end point.
// Again, the animation doesn't start immediately. It starts when this
// pass through the run loop is done.
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"position"];
[animation setFromValue:[NSValue valueWithPoint:startPoint]];
[animation setToValue:[NSValue valueWithPoint:endPoint]];
// Set any other properties you want, such as the delegate.
[theView.layer addAnimation:animation forKey:#"position"];
}
Of course, for this code to work you need to make sure both your view and its superview have layers. If the superview doesn't have a layer, you'll get corrupted graphics.
I think you need to call the setPosition at the end (after setting the animation).
Also, I don't think you should animate explicitely the layer of the view, but instead the view itself by using animator and setting the animations. You can use delegates too with animator :)
// create controlPosAnim
[controlView setAnimations:[NSDictionary dictionaryWithObjectsAndKeys:controlPosAnim, #"frameOrigin", nil]];
[[controlView animator] setFrame:newFrame];

CAShapeLayer Slow User Interaction

I have a CAShapeLayer and it has to do a simple task of moving on the screen, guided by the user's finger.
The problem is that the movement is too slow. The layer does move, but there is a lag and it feels slow.
I have another test app where an UIImage is moved and there is no lag at all and the image moves instantly.
What can I do to overcome this?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
currentPoint = [[touches anyObject] locationInView:self];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint activePoint = [[touches anyObject] locationInView:self];
CGPoint newPoint = CGPointMake(activePoint.x - currentPoint.x,activePoint.y - currentPoint.y);
curLayer.position = CGPointMake(shapeLayer.position.x+newPoint.x,shapeLayer.position.y+newPoint.y);
currentPoint = activePoint;
}
Thanks!
Keep in mind that when you set the position on a layer (assuming it's not the root layer of a UIView on which actions are disabled by default), it implicitly animates to the new position, which takes 0.25 seconds. If you want to make it snappier, temporarily disable actions on the layer like this:
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint activePoint = [[touches anyObject] locationInView:self];
CGPoint newPoint = CGPointMake(activePoint.x -
currentPoint.x,activePoint.y - currentPoint.y);
[CATransaction begin];
[CATransaction setDisableActions:YES];
curLayer.position = CGPointMake(shapeLayer.position.x +
newPoint.x, shapeLayer.position.y + newPoint.y);
[CATransaction commit];
currentPoint = activePoint;
}
This should cause it to jump to the new position rather than animate. If that doesn't help, then let me take a look at your layer init code so I can see what properties and dimensions it has. Properties such as cornerRadius, for example, can affect performance.
Try setting shouldRasterize to YES on your CAShapeLayer, particularly if it is usually drawn at the same scale. If your app runs on high-DPI devices, you may also need to set rasterizationScale to match the layer’s contentsScale.
While rasterizing your shape can make it faster to move the shape around, you’ll probably want to temporarily disable rasterization while you’re animating the layer’s path or size.

Resources