Search code examples
objective-ccocoaautolayoutinterface-buildernsanimation

Move an NSView from outside view with autolayout using NSAnimation


I am using auto-layout in my project and have a 'detailed view' slide over the 'tile view' upon a button press.

The 'tile view' is aligned to the top, bottom, left and right. The 'detailed view' is aligned to centre x,y and equal width/height.

I got the animations to work however I had to use two different methods:

1) This is my preferred method of animation and works perfectly fine on the 'tile view'

CAKeyframeAnimation* tileKeyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
tileKeyAnimation.fillMode = kCAFillModeForwards;
tileKeyAnimation.removedOnCompletion = NO;
tileKeyAnimation.keyTimes = @[@0,@0.45];
tileKeyAnimation.values   = @[[NSValue valueWithCATransform3D:CATransform3DIdentity],
                              [NSValue valueWithCATransform3D:self.tileTransform]];
tileKeyAnimation.duration = 0.5;
[_tileView.layer addAnimation:tileKeyAnimation forKey:@"transform"];

2) Since the above animation was not working for the 'detailed view', I resorted to using a different type of animation for it. (I created an outlet for the centreX constraint named _detailedCenterXConstraint)

_detailedCenterXConstraint.constant = _tileView.bounds.size.width;

[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
    [context setTimingFunction:[[CAMediaTimingFunction alloc] initWithControlPoints:0 :0 :0 :0.45]];
    [context setDuration:[self pushAnimationDuration]];
    [_detailedCenterXConstraint.animator setConstant:_detailedView.bounds.size.width];
} completionHandler:^{
}];

The problem with this approach is that the animations do not match with each other. I have tried using a custom CAMediaTimingFunction in (2) to match the animation timing in (1) but it seems that I'm not using this correctly.

Is there a reason why a CAKeyframeAnimation (1) should not work on my 'detailed view'?

Alternatively is there a way how to match the animation timing between the two approaches?


Solution

  • I have managed to get an identical animation with the following code:

    [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
        [context setTimingFunction:0.45 :1.0 :0.45 :1.0];
        [context setDuration:[self pushAnimationDuration]/2];
        [_detailedCenterXConstraint.animator setConstant:_detailedView.bounds.size.width];
    } completionHandler:^{
    }];