Can't get [[id Animator] setAlphaValue: 0.0] to work - objective-c

I'm trying to animate an opacity fade out + frame size change of an NSImageView . The frameSize works smoothly but for some reason the alpha value just jumps directly to it's final value at the beginning of the animation. I've tried back layering the superview and I'm running out of ideas here. Here is my code sample ->
AABLCLogo -> NSImageView IBOutlet
speed -> 1.0f
//setting target frame for image view
NSRect newFrame = [AABLCLogo frame];
newFrame.origin.x = newFrame.origin.x + (newFrame.size.width*0.1);
newFrame.origin.y = newFrame.origin.y + (newFrame.size.height*0.1);
newFrame.size.width = newFrame.size.width * 0.8;
newFrame.size.height = newFrame.size.height * 0.8;
//backing layer for super view
[[AABLCLogo superview] setWantsLayer:YES];
//animating image view
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration: speed];
[[AABLCLogo animator] setFrame: newFrame];
[[AABLCLogo animator] setAlphaValue: 0.0];
[NSAnimationContext endGrouping];
//XCode 4 delay snippet
double delayInSeconds = speed;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW,
delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime,
dispatch_get_main_queue(),
^(void) {
//code executes after ''speed'' seconds
[[AABLCLogo superview] setWantsLayer:NO];
[[self view] removeFromSuperview];
});

I suspect this is an issue with animating on the main thread. In most cases, if you want to animate two things simultaneously, you'll need to use NSViewAnimation. Use this code as a guide, it resizes the window while fading out part of it's content.
NSRect newWindowFrame = NSMakeRect(self.window.frame.origin.x, self.window.frame.origin.y, 640, self.window.frame.size.height);
NSMutableArray *viewAnimations = [NSMutableArray array];
NSDictionary *windowSizeDict = [NSDictionary dictionaryWithObjectsAndKeys:self.window, NSViewAnimationTargetKey, [NSValue valueWithRect:newWindowFrame], NSViewAnimationEndFrameKey, nil];
[viewAnimations addObject:windowSizeDict];
NSDictionary *animateOutDict = [NSDictionary dictionaryWithObjectsAndKeys:self.dataSidebarController.containerView, NSViewAnimationTargetKey, NSViewAnimationFadeOutEffect, NSViewAnimationEffectKey, nil];
[viewAnimations addObject:animateOutDict];
NSViewAnimation *animation = [[NSViewAnimation alloc] initWithViewAnimations:viewAnimations];
[animation setDuration:0.35];
[animation setAnimationBlockingMode:NSAnimationNonblocking];
[animation setDelegate:self];
[animation startAnimation];

You want to send setWantsLayer:YES to the view you are animating, not its superview.
NSView's setAlphaValue: only works an a layer-backed or layer-hosting instance, since it forwards the value to the opacity property of NSView's layer.
For the full story, read the "Discussion" of NSView's setAlphaValue: in Apple's documentation.

I believe the layer you request is not create until the next time the view is drawn.
Thus you either need to force display of the superview or wait until the next iteration of the run loop before adding on animations.

Related

How can I hide my UIImageView after the animation is completed?

How can I hide my UIImageView after the animation is completed? My animation is working fine when I segue into the view. I want to be a able to hide the image after the animation is complete. How might I do that?
-(void) animate
{
NSLog(#"Animate");
CGPoint startPoint = [pickerCircle center];
CGPoint endPoint = [pickerButton1 center];
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, startPoint.x, startPoint.y);
CGPathAddLineToPoint(thePath, NULL, endPoint.x, endPoint.y);
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
animation.duration = 3.f;
animation.path = thePath;
animation.repeatCount = 2;
animation.removedOnCompletion = YES;
[pickerCircle.layer addAnimation:animation forKey:#"position"];
pickerCircle.layer.position = endPoint;
}
Set the animation's delegate property to self like so:
animation.delegate = self;
And then you can use:
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
// hide your UIImage here
}
It will be called when the animation is done.
Did you try completion? Here example 4.2there is an example about showing and hiding an view with using completion.
I don't know animations well but I think completion should work.

Animate NSSplitview and window size

What I'm trying to achieve is that when the user press a button the window grows and the extra space is taken by a panel of NSSplitview that uncollapses and grows to fill the space.
I can easily animate the window resize and the splitview growing independently but when I try to put the two animation together one inevitably happens before the other or by steps. First the window partly resize, then the splitview resize follow, then the window finish resizing and finally the splitview finishes as well. Any ides on why that might be happening?
Here is the code I use:
- (IBAction)helpButtonPressed:(id)sender
{
if ([sender isKindOfClass:[NSMenuItem class]]) [sender setState:![sender state]];
NSWindow *window = [[[self windowControllers] objectAtIndex:0] window];
NSRect oldFrame = [window frame];
CGFloat windowWidthAdd;
if ([sender state]) windowWidthAdd = HELP_WIDTH; else windowWidthAdd = -HELP_WIDTH;
NSRect newFrame = NSMakeRect(oldFrame.origin.x, oldFrame.origin.y,oldFrame.size.width+windowWidthAdd, oldFrame.size.height);
[[NSAnimationContext currentContext] setDuration:0.3f];
[NSAnimationContext beginGrouping];
[[window animator] setFrame:newFrame display:YES];
if ([sender state]) [self uncollapseRightView]; else [self collapseRightView];
[NSAnimationContext endGrouping];
}
-(void)collapseRightView
{
NSView *right = [[self.splitView subviews] objectAtIndex:1];
NSView *left = [[self.splitView subviews] objectAtIndex:0];
NSRect leftFrame = [left frame];
NSRect overallFrame = [self.splitView frame];
[right setHidden:YES];
[[left animator] setFrameSize:NSMakeSize(overallFrame.size.width,leftFrame.size.height)];
}
-(void)uncollapseRightView
{
NSView *left = [[self.splitView subviews] objectAtIndex:0];
NSView *right = [[self.splitView subviews] objectAtIndex:1];
[right setHidden:NO];
CGFloat dividerThickness = [self.splitView dividerThickness];
// get the different frames
NSRect leftFrame = [left frame];
// Adjust left frame size
leftFrame.size.width = (leftFrame.size.width-HELP_WIDTH-dividerThickness);
NSRect rightFrame = [right frame];
rightFrame.origin.x = leftFrame.size.width + dividerThickness;
rightFrame.size.width = HELP_WIDTH;
[[left animator] setFrameSize:leftFrame.size];
[[right animator] setFrame:rightFrame];
}
If you are looking a little closer at the class reference of NSSplitView, you will recognize it conforms to NSAnimatablePropertyContainer. That means NSSplitView will provide you with an "animating proxy of itself". If you call animator on your NSSplitView you'll get this proxy, on which you should be able to change properties in an animated fashion.
For adjusting the animation duration and timing function, use NSAnimationContext the same way you already did.
Last but not least: Did you recognize NSSplitView's minPossiblePositionOfDividerAtIndex: and maxPossiblePositionOfDividerAtIndex:?

UILabel size change suddenly when parent UIView animate

I want to send a subview out of my super UIView with animation, It works fine but when i tried to change the size during animation any UILabel that i have in my subview suddenly become too small.
here is part of my code
-(void)pushOutScreen:(UIViewController *)pop{
[UIView animateWithDuration:1
delay:0.0
options: UIViewAnimationTransitionFlipFromLeft
animations:^{
CGRect frame = pop.view.frame;
frame.size.height = frame.size.height /4;
frame.size.width = frame.size.width /4;
frame.origin.x = -500;
frame.origin.y = 318;
pop.view.frame = frame;
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
}
note: any UIButton or UIImage that is inside my subview animated well but i have only problem with UILabel.
UIView animation is not good option for doing this, instead of it try CAKeyframeAnimation. This is sample code for scaling UIView:
- (void) scaleView:(UIView *)popView {
CAKeyframeAnimation *animation = [CAKeyframeAnimation
animationWithKeyPath:#"transform"];
animation.delegate = self;
// CATransform3DMakeScale has 3 parameter (x,y,z)
CATransform3D scale1 = CATransform3DMakeScale(1.0, 1.0, 1);
CATransform3D scale2 = CATransform3DMakeScale(0.2, 0.2, 1);
NSArray *frameValues = [NSArray arrayWithObjects:
[NSValue valueWithCATransform3D:scale1],
[NSValue valueWithCATransform3D:scale2],
nil];
[animation setValues:frameValues];
NSArray *frameTimes = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:1.0],
nil];
[animation setKeyTimes:frameTimes];
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
animation.duration = 1.0;
[popView.layer addAnimation:animation forKey:#"popup"];
}
you can use this after you add your UIView as subView then you can call this method as scale it. for push out a sub view with this method you need to use removeFromSubView after the animation finished.
for knowing that when it finished use
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
[subView removeFromSuperview];
}
I hope it be useful!

Flip, Grow, and Translate Animation

Look at this video of the MLB At Bat app. Basically, I just want to present a modalViewController with the UIModalPresentationFormSheet style and have it grow from another view then flip. Like when you tap on a game in the scoreboard on the MLB app. Anyone know how I can accomplish this?
Thanks
EDIT: My main view is pretty much the same setup as the MLB app. I'm using AQGridView and want the animation to occur when a cell in the grid view is tapped.
EDIT 2: I'd also be open to ditching the UIViewController concept and just using a plain UIView, then replicate the style of UIModalPresentationFormSheet manually if that's easier.
EDIT 3: Okay, forget using a UIViewController to do this, since I haven't gotten any responses, I'll assume it isn't possible. My new question is just how do I replicate the animation in the posted video using just UIView's? So basically, the initial view needs to grow, move, and flip all at the same time.
EDIT 4: I think I have the actual animation figured out, now my only problem is calculating coordinates to feed into CATransform3DTranslate. My view needs to animate pretty much exactly like in the video. It needs to start over another view and animate to the center of the screen. Here's how I'm trying to calculate the coordinates for the view that pops up in the center:
CGPoint detailInitialPoint = [gridView convertPoint:view.frame.origin toView:detailView.frame.origin];
CGPoint detailFinalPoint = detailView.frame.origin;
gridView is the container view of my main view that holds the smaller grid items. view is the specific grid item that we are animating from. And detailView is the view that comes up in the middle of the screen.
You can implement your own transition using a category on UIViewControler.
UIViewController+ShowModalFromView.h
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#interface UIViewController (ShowModalFromView)
- (void)presentModalViewController:(UIViewController *)modalViewController fromView:(UIView *)view;
#end
UIViewController+ShowModalFromView.m
#import "UIViewController+ShowModalFromView.h"
#implementation UIViewController (ShowModalFromView)
- (void)presentModalViewController:(UIViewController *)modalViewController fromView:(UIView *)view {
modalViewController.modalPresentationStyle = UIModalPresentationFormSheet;
// Add the modal viewController but don't animate it. We will handle the animation manually
[self presentModalViewController:modalViewController animated:NO];
// Remove the shadow. It causes weird artifacts while animating the view.
CGColorRef originalShadowColor = modalViewController.view.superview.layer.shadowColor;
modalViewController.view.superview.layer.shadowColor = [[UIColor clearColor] CGColor];
// Save the original size of the viewController's view
CGRect originalFrame = modalViewController.view.superview.frame;
// Set the frame to the one of the view we want to animate from
modalViewController.view.superview.frame = view.frame;
// Begin animation
[UIView animateWithDuration:1.0f
animations:^{
// Set the original frame back
modalViewController.view.superview.frame = originalFrame;
}
completion:^(BOOL finished) {
// Set the original shadow color back after the animation has finished
modalViewController.view.superview.layer.shadowColor = originalShadowColor;
}];
}
#end
This can easily be changed to use whatever animated transition you want; for your's, you might want to use a CA3DTransform. Hope this helps!
To do a flip, grow, and translate animation you can use the following code:
- (void)animate {
int newX, newY; //New position
int newW, newH; //New size
[UIView animateWithDuration:someDuration delay:someDelay animations:^{
yourView.transform = CATransform3DMakeRotation(M_PI_2,1.0,0.0,0.0); //flip halfway
yourView.frame = CGRectMake(newX/2, newY/2, newW/2, newH/2);
} completion:^{
while ([yourView.subviews count] > 0)
[[yourView.subviews lastObject] removeFromSuperview]; // remove all subviews
// Add your new views here
[UIView animateWithDuration:someDuration delay:someDelay animations:^{
yourView.transform = CATransform3DMakeRotation(M_PI,1.0,0.0,0.0); //finish the flip
yourView.frame = CGRectMake(newX, newY, newW, newH);
} completion:^{
// Flip completion code here
}];
}];
}
Hope this helps!
OK, I figured it out. Here's what I did:
CGFloat duration = 0.8;
/*
//Detail View Animations
*/
CATransform3D initialDetailScale = CATransform3DMakeScale(view.frame.size.width/vc.view.frame.size.width, view.frame.size.height/vc.view.frame.size.height, 1.0);
CATransform3D initialDetailTransform = CATransform3DRotate(initialDetailScale, -M_PI, 0.0, -1.0, 0.0);
CATransform3D finalDetailScale = CATransform3DMakeScale(1.0, 1.0, 1.0);
CATransform3D finalDetailTransform = CATransform3DRotate(finalDetailScale, 0.0, 0.0, -1.0, 0.0);
NSMutableArray *detailAnimations = [NSMutableArray array];
CABasicAnimation *detailTransform = [CABasicAnimation animationWithKeyPath:#"transform"];
detailTransform.fromValue = [NSValue valueWithCATransform3D:initialDetailTransform];
detailTransform.toValue = [NSValue valueWithCATransform3D:finalDetailTransform];
detailTransform.duration = duration;
[detailAnimations addObject:detailTransform];
CABasicAnimation *detailFadeIn = [CABasicAnimation animationWithKeyPath:#"opacity"];
detailFadeIn.fromValue = [NSNumber numberWithFloat:1.0];
detailFadeIn.toValue = [NSNumber numberWithFloat:1.0];
detailFadeIn.duration = duration/2;
detailFadeIn.beginTime = duration/2;
[detailAnimations addObject:detailFadeIn];
CABasicAnimation *detailMove = [CABasicAnimation animationWithKeyPath:#"position"];
detailMove.fromValue = [NSValue valueWithCGPoint:vc.view.layer.position];
detailMove.toValue = [NSValue valueWithCGPoint:self.view.layer.position];
detailMove.duration = duration;
[detailAnimations addObject:detailMove];
CAAnimationGroup *detailGroup = [CAAnimationGroup animation];
[detailGroup setAnimations:detailAnimations];
[detailGroup setDuration:duration];
detailGroup.removedOnCompletion = NO;
detailGroup.fillMode = kCAFillModeForwards;
detailGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[vc.view.layer addAnimation:detailGroup forKey:#"anim"];
/*
//Grid Item View Animations
*/
CATransform3D initialGridScale = CATransform3DMakeScale(1.0, 1.0, 1.0);
CATransform3D initialGridTransform = CATransform3DRotate(initialGridScale, 0.0, 0.0, 1.0, 0.0);
CATransform3D finalGridScale = CATransform3DMakeScale(vc.view.frame.size.width/view.frame.size.width, vc.view.frame.size.height/view.frame.size.height, 1.0);
CATransform3D finalGridTransform = CATransform3DRotate(finalGridScale, M_PI, 0.0, 1.0, 0.0);
NSMutableArray *gridAnimations = [NSMutableArray array];
CABasicAnimation *gridTransform = [CABasicAnimation animationWithKeyPath:#"transform"];
gridTransform.fromValue = [NSValue valueWithCATransform3D:initialGridTransform];
gridTransform.toValue = [NSValue valueWithCATransform3D:finalGridTransform];
gridTransform.duration = duration;
[gridAnimations addObject:gridTransform];
CABasicAnimation *gridFadeOut = [CABasicAnimation animationWithKeyPath:#"opacity"];
gridFadeOut.fromValue = [NSNumber numberWithFloat:0.0];
gridFadeOut.toValue = [NSNumber numberWithFloat:0.0];
gridFadeOut.duration = duration/2;
gridFadeOut.beginTime = duration/2;
[gridAnimations addObject:gridFadeOut];
CABasicAnimation *gridMove = [CABasicAnimation animationWithKeyPath:#"position"];
gridMove.fromValue = [NSValue valueWithCGPoint:view.layer.position];
gridMove.toValue = [NSValue valueWithCGPoint:[self.view.layer convertPoint:self.view.layer.position toLayer:gridView.layer]];
gridMove.duration = duration;
[gridAnimations addObject:gridMove];
CAAnimationGroup *gridGroup = [CAAnimationGroup animation];
[gridGroup setAnimations:gridAnimations];
gridGroup.duration = duration;
gridGroup.fillMode = kCAFillModeForwards;
gridGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
gridGroup.removedOnCompletion = NO;
[view.layer addAnimation:gridGroup forKey:#"anim"];
I'm doing this with an item in an AQGridView. In my code sample view is the instance of AQGridViewCell that I am animating. And vc.view is my detail UIViewController/UIView that I'm displaying.
I would highly recommend taking a look at this post.
You shouldn't need to actually handle all this animation yourself.
If you use the UIView class method transitionFromView:toView:duration:options:completion: and passing in UIViewAnimationOptionTransitionFlipFromLeft or UIViewAnimationOptionTransitionFlipFromRight -- whichever way you want the animation to flip.
I have implemented the same thing as shown in the MLB app using this method. Your from view would be the cell in the grid and the to view would be the thing that would on the "back" of the cell.
Hope this will reduce the overall code you'll need.
I would use a UICollectionView with a custom UICollectionViewCell and animate in with its delegate call... Just an example for others to pick at.
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:
(NSIndexPath *)indexPath
{
[UIView beginAnimations:#"showImage" context:Nil];
CGRect cellFrame = cell.frame;
CGRect imgFram = cell.imageView.frame;
[UIView setAnimationDuration:0.8];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:cell
cache:YES];
cellFrame.size = CGSizeMake(200, 200);
cellFrame.origin.y = 10;
cellFrame.origin.x = 45;
cell.frame = cellFrame;
imgFram.size = CGSizeMake(200, 200);
cell.imageView.frame = imgFram;
[collectionView bringSubviewToFront:cell];
[UIView commitAnimations];
}

Draggable CALayer

Any way of making a CALayer draggable by the user? If so, how?
(In Cocoa - Mac)
Layers can not receive mouse events themselves. You would have to do the event handling in the view or view controller containing the layer.
If a mouseDragged: event originates on a layer (see -[CALayer hitTest:] and -[CALayer containsPoint:] to test this), adjust the layer's position accordingly. You will probably want to disable implicit animations to have the layer follow the mouse pointer immediately (rather than lagging a little behind because of the animation of the position property):
[CATransaction begin];
[CATransaction setDisableActions:YES];
layer.position = ...;
[CATransaction commit];
I tried creating a window through code and adding the CALayer to it, but I don't get why it's not showing.
NSRect rect = NSZeroRect;
rect.size = NSMakeSize( SSRandomFloatBetween( 300.0, 200.0 ), SSRandomFloatBetween( 300.0, 200.0 ));
NSWindow *newWin = [[NSWindow alloc] initWithContentRect:rect styleMask:NSBorderlessWindowMask backing:NSWindowBackingLocationDefault defer:YES];
[newWin setBackgroundColor: [NSColor clearColor]];
[newWin setOpaque:NO];
[newWin setIgnoresMouseEvents:NO];
[newWin setMovableByWindowBackground:YES];
[newWin makeKeyAndOrderFront:self];
[[newWin contentView] setWantsLayer:YES];
NSRect contentFrame = [[newWin contentView] frame];
CALayer *newWinLayer = [CALayer layer];
newWinLayer.frame = NSRectToCGRect(contentFrame);
layer.backgroundColor=CGColorCreateGenericGray(0.0f, 0.5f);
layer.borderColor=CGColorCreateGenericGray(0.756f, 0.5f);
layer.borderWidth=5.0;
// Calculate random origin point
rect.origin = SSRandomPointForSizeWithinRect( rect.size, [window frame] );
// Set the layer frame to our random rectangle.
layer.frame = NSRectToCGRect(rect);
layer.cornerRadius = 25.0f;
[newWinLayer addSublayer:layer];
Window is linked to a big window, with a semi-transparent (black filled) window that is resized to fill the screen.
I've made the window draggable, but why isn't the CALayer in the window showing?