Animating and removing an NSImageView object failed? - macos

My app situation:
I am now having an app displaying a dynamic picture. And I want to capture its current image and save it to disc.
My goal:
I am working on an animated visual effect: add an image view on top, and then animates it from its origin frame into CGRecrZero. Finally, remove this image view from vie hierarchy after animation is done.
Now problem:
First time triggerring the action, it seems work. But when trigger it at the second time, the image view does not animate and stay atop of all subviews.
Codes of the view controller(with ARC):
Firstly, this is the IBAction to trigger the snapshot:
- (IBAction)actionSaveSnapshot:(id)sender
{
... // Save the image file. This works everytime.
NSThread *tempThread = [[NSThread alloc] initWithTarget:self
selector:#selector(threadSimulateScreenCapture:)
object:nil];
if (tempThread)
{
[tempThread start];
tempThread = nil; // Simply release it
}
}
And here is the thread:
- (void)threadSimulateScreenCapture:(id)arg
{#autoreleasepool {
NSImageView __strong *subImageView;
subImageView = [[NSImageView alloc] init];
NSRect subRect = [_imageView frame]; // the dynamic image view which I want to snapshot with
const CGFloat animationTime = 0.4;
[subImageView setWantsLayer:YES];
[subImageView setImageScaling:NSImageScaleAxesIndependently];
[subImageView setImage:[_imageView image]];
dispatch_async(dispatch_get_main_queue(), ^{
[CATransaction begin];
[self.view addSubview:subImageView];
[subImageView setFrame:subRect];
[subImageView setNeedsDisplay:YES];
[CATransaction commit];
});
[NSThread sleepForTimeInterval:0.01];
dispatch_async(dispatch_get_main_queue(), ^{
[CATransaction begin];
[CATransaction setAnimationDuration:animationTime];
[subImageView.layer setFrame:CGRectZero];
[CATransaction commit];
});
[NSThread sleepForTimeInterval:(NSTimeInterval)animationTime]; // wait for end of animation
dispatch_async(dispatch_get_main_queue(), ^{
[subImageView removeFromSuperview];
});
NSLog(#"SubView Count: %#\n%#", self.view.subviews, subImageView); // MARK. described below
subImageView = nil; // release the image
}} // ENDS: thread and aotureleasepool
Every time the program runs to the "MARK" line and print the subview array, it is clear thar the subImageView is not removed from superview then. And every time the thread ends, a "deleted thread with uncommitted CATransaction ...." raised.
What wrong did I do?

I may solved my own problem: At the line with [subImageView setFrame:subRect];, insert another line with: [subImageView.layer setFrame:subRect];
Meanwhile, add another line before the "addSubview:" method: [self.view setWantsLayer:YES];.
It seemed work. But I do not know why. Why I should set both view and layer's frame?

Related

dismissViewControllerAnimated differs in ios8 and ios7

I have a launchViewController A and two other view controllers B and C. In the app, present sequence is A->B->C. I also have a need to dismiss C and directly back to A.
The problem is:
In iOS7, I call [self dismissViewControllerAnimated:false completion:^{}] in A. the functions viewWillAppear and viewDidAppear in B view controller will not be called.
But in iOS8, things are different. viewWillAppear and viewDidAppear will be called in B view controller. This results in a flashes of B's contents when dismiss.
Can anyone help me to find a way to fix this.
if you want to dismiss current ViewController in Which you are you should use
[self.presentingViewController dismissViewControllerAnimated:false completion:^{}]
instead of [self dismissViewControllerAnimated:false completion:^{}]
I fixed the issue by using this:
- (void)dismissDeepViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion {
if([UIDevice currentDevice].systemVersion.floatValue >= IOSCapVersion){
UIView *window = ((AppDelegate *)[UIApplication sharedApplication].delegate).window;
UIView *view = [window snapshotViewAfterScreenUpdates:false];
[((AppDelegate *)[UIApplication sharedApplication].delegate).window addSubview:view];
[self dismissViewControllerAnimated:false completion:^{
[UIView animateWithDuration:AnimationDurationDismissViewController animations:^{
view.alpha = 0;
}completion:^(BOOL finished) {
if (finished) {
[view removeFromSuperview];
}
}];
completion();
}];
}
else{
[self dismissViewControllerAnimated:flag completion:^{
completion();
}];
}
}

NSProgressIndicator animating disappear when change view

My NSProgressIndicator is animating in a view
when I switch to another view ,and switch back
my NSProgressIndicator disappear.
why?
Here is my code for switch to another view
- (void)switchToView:(NSView *)newView withAnimate:(BOOL)animate
{
if ([[rootView subviews] count] != 0)
{
[rootView setWantsLayer:YES];
[rootView displayIfNeeded];
NSTimeInterval duration = [[self window] animationResizeTime:newFrame];
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.25];
[[rootView animator] replaceSubview:[[rootView subviews] objectAtIndex:0] with:newView];
[NSAnimationContext beginGrouping];
if (duration > 0.25) {
[[NSAnimationContext currentContext] setDuration:0.25];
}
else{
[[NSAnimationContext currentContext] setDuration:duration];
}
[[[self window] animator] setFrame:newFrame display:YES animate:YES];
[NSAnimationContext endGrouping];
[NSAnimationContext endGrouping];
[self performSelector:#selector(endAnimation) withObject:nil afterDelay:0.25f];
}
}
-(void)endAnimation{
[[ _mainWindow contentView] setWantsLayer:NO];
}
I just replace a view,and make a fade out animation NSProgressIndicator animating all the time in a view.
I have solve this by myself ,I think it will be helpful for your guys.
I stop my NSProgressIndicator before I want to replace my view with NSProgressIndicator.
then I replace the view with a new view ,if I switch back I will start NSProgressIndicator with a - (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay; method ,just set the delay=0 is OK.
In xib files , you should set NSProgressIndicator DisplayWhenStopped is YES.
And it will work well.
Then reason is after 10.6.5,At least on OS X 10.6.5 and above, as soon as you set an indetermined-progress-indicator's wantsLayer property to YES, the animation stops immediately .
So before it stop immediately ,I stop it by myself.And start it when I want to use it.
NOTICE: You should stop it by yourself better than system does, it can`t work well if system stop it .

NSView Does Not Respond To Mouse Events After CABasicAnimation

I have a pageLeft and pageRight animation that animates the position and alpha of 2 views, making a total of 4 simultaneous CABasicAnimations:
Move next page into the view
Move current page out of the view
Fade in next page
Fade out current page
Everything works well with the animations and setup. this question is about mouse events after the page animation. I have found that after the animation runs, the nextView does not receive mouseDown: events. If the animation is not run because the shouldNotAnimate block is run below instead, the nextView does receive mouseDown: events.
This means that something about the way I have setup my CABasicAnimations and CATransaction is causing this. I am stumped as to what it could be. Any ideas?
Animation Code:
BOOL forward = currentIndex < index;
if (shouldNotAnimate) {
// handles swapping pages faster than the animation duration
[self.sourceViewContainer setSubviews:[NSArray array]];
[nextView.layer removeAllAnimations];
[nextView setFrame:self.sourceViewContainer.bounds];
[nextView.layer setPosition:self.sourceViewContainer.bounds.origin];
[nextView.layer setOpacity:1.0];
[self.sourceViewContainer addSubview:nextView];
[currentView removeFromSuperview];
[currentView.layer removeAllAnimations];
[currentView setFrame:self.sourceViewContainer.bounds];
[currentView.layer setPosition:self.sourceViewContainer.bounds.origin];
[currentView.layer setOpacity:1.0];
self.animationsRunning = NO;
} else {
self.animationsRunning = YES;
[CATransaction begin];
[CATransaction setCompletionBlock:^{
self.animationsRunning = NO;
}];
// Setup incoming push animation
[nextView setFrame:CGRectMake(self.sourceViewContainer.bounds.size.width * (forward ? 1 : -1), 0, self.sourceViewContainer.bounds.size.width, self.sourceViewContainer.bounds.size.width)];
[self.sourceViewContainer addSubview:nextView];
CABasicAnimation *incomingAnimation = [CABasicAnimation animationWithKeyPath:#"position"];
incomingAnimation.fromValue = [NSValue valueWithPoint:nextView.frame.origin];
incomingAnimation.toValue = [NSValue valueWithPoint:self.sourceViewContainer.bounds.origin];
incomingAnimation.duration = PANEL_PAGE_SWIPE_ANIMATION_DURATION;
incomingAnimation.removedOnCompletion = NO;
incomingAnimation.fillMode = kCAFillModeForwards;
incomingAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
incomingAnimation.completion = ^(BOOL finished) {
[nextView.layer setPosition:self.sourceViewContainer.bounds.origin];
[nextView.layer removeAnimationForKey:#"incoming"];
};
[nextView.layer addAnimation:incomingAnimation forKey:#"incoming"];
// Setup outgoing push animation
CGRect offscreen = CGRectMake(self.sourceViewContainer.bounds.size.width * (forward ? -1 : 1), 0, self.sourceViewContainer.bounds.size.width, self.sourceViewContainer.bounds.size.height);
CABasicAnimation *outgoingAnimation = [CABasicAnimation animationWithKeyPath:#"position"];
outgoingAnimation.fromValue = [NSValue valueWithPoint:self.sourceViewContainer.bounds.origin];
outgoingAnimation.toValue = [NSValue valueWithPoint:offscreen.origin];
outgoingAnimation.duration = PANEL_PAGE_SWIPE_ANIMATION_DURATION;
outgoingAnimation.removedOnCompletion = NO;
outgoingAnimation.fillMode = kCAFillModeForwards;
outgoingAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
outgoingAnimation.completion = ^(BOOL finished) {
[currentView removeFromSuperview];
[currentView.layer setPosition:self.sourceViewContainer.bounds.origin];
[currentView.layer removeAnimationForKey:#"outgoing"];
};
[currentView.layer addAnimation:outgoingAnimation forKey:#"outgoing"];
// Setup incoming alpha animation
CABasicAnimation *fadeInAnimation = [CABasicAnimation animationWithKeyPath:#"opacity"];
fadeInAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
fadeInAnimation.toValue = [NSNumber numberWithFloat:1.0f];
fadeInAnimation.duration = PANEL_PAGE_SWIPE_ANIMATION_DURATION;
fadeInAnimation.removedOnCompletion = NO;
fadeInAnimation.fillMode = kCAFillModeForwards;
fadeInAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
fadeInAnimation.completion = ^(BOOL finished) {
[nextView.layer setOpacity:1.0f];
[nextView.layer removeAnimationForKey:#"fadeIn"];
};
[nextView.layer addAnimation:fadeInAnimation forKey:#"fadeIn"];
// Setup outgoing alpha animation
CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:#"opacity"];
fadeOutAnimation.fromValue = [NSNumber numberWithFloat:1.0f];
fadeOutAnimation.toValue = [NSNumber numberWithFloat:0.0f];
fadeOutAnimation.duration = PANEL_PAGE_SWIPE_ANIMATION_DURATION;
fadeOutAnimation.removedOnCompletion = NO;
fadeOutAnimation.fillMode = kCAFillModeForwards;
fadeOutAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
fadeOutAnimation.completion = ^(BOOL finished) {
[currentView removeFromSuperview];
[currentView.layer setOpacity:1.0f];
[currentView.layer removeAnimationForKey:#"fadeOut"];
};
[currentView.layer addAnimation:fadeOutAnimation forKey:#"fadeOut"];
[CATransaction commit];
} // Run animations
Solution
I had to explicitly set the view frame in the completion block, even though I had done this immediately prior to the animation calls.
[CATransaction begin];
[CATransaction setCompletionBlock:^{
self.animationsRunning = NO;
[nextView setFrame:self.sourceViewContainer.bounds];
}];
I think I know what's going on.
CAAnimation objects don't actually change the underlying property that they animation. Instead, it applies the properties to a presentation layer, which is drawn instead of the regular layer. When you create an animation with removedOnCompletion=false, the presentation layer is left active after the animation is completed, but the underlying property still isn't changed. Plus, moving a view's layer doesn't move the view itself, so it won't respond to user interaction at it's new location.
I'd suggest moving your views using UIView block animation (UIView animateWithDuration:completion: and it's relatives) instead of using CAAnimation objects. Those methods actually move the view to it's new position, so that it responds to user interaction at the final location once the animation is complete.
You could do your move and alpha change all in one animation block.
I had an issue with iOS once where the UIMapView because unresponsive after an animation due to a call to the map view. Somehow userInteractionEnable was set to FALSE after the animation finished.
What you could do to test if that is the case is to assert that the value for userInteractionEnable is TRUE.
assert(view.userInteractionEnable != FALSE); // ensure user interaction is enabled
You may find that when you run the app that it breaks on this assertion. If it does, you can simply set it to TRUE and your app will work as you expect.
As for my issue with UIMapView, the problem went away after an iOS version update so it was likely a bug caused by an edge case. I filed a bug about the problem which may have helped raise awareness about the unexpected behavior.

Why fadeout effect for view did't work?

I add to the scrollview my view with content as a documenView and use this code to fadeout my documentView:
- (IBAction)deleteAllRules:(id)sender {
NSViewAnimation * deleteListOfRulesViewAnimation;
NSRect viewRect;
NSMutableDictionary* animDict;
// Create the attributes dictionary
animDict = [NSMutableDictionary dictionaryWithCapacity:3];
viewRect = listOfRulesView.frame;
// Specify which view to modify.
[animDict setObject:listOfRulesView forKey:NSViewAnimationTargetKey];
// Specify the starting position of the view.
[animDict setObject:[NSValue valueWithRect:viewRect] forKey:NSViewAnimationStartFrameKey];
// Shrink the view from its current size to nothing.
NSRect viewZeroSize = listOfRulesView.frame;
viewZeroSize.size.width = 0;
viewZeroSize.size.height = 0;
[animDict setObject:[NSValue valueWithRect:viewZeroSize] forKey:NSViewAnimationEndFrameKey];
// Set this view to fade out
[animDict setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
// Create the view animation object.
deleteListOfRulesViewAnimation = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray
arrayWithObjects:animDict, nil]];
// Set some additional attributes for the animation.
[deleteListOfRulesViewAnimation setDuration:1.5]; // One and a half seconds.
[deleteListOfRulesViewAnimation setAnimationCurve:NSAnimationEaseIn];
[deleteListOfRulesViewAnimation setDelegate:self];
// Run the animation.
[deleteListOfRulesViewAnimation startAnimation];
// The animation has finished, so go ahead and release it.
[deleteListOfRulesViewAnimation release];
}
but view hide with out fadeout effect. Why?
It's recommended to use Core Animation for 10.5+ code.
The simplest way to use Core Animation is to use the animator proxy. For example you can do the fade out effect by animating the alpha value of the view :
[[myView animator] setAlpaValue:0.0f]
You can change the animation duration using:
[[NSAnimationContext currentContext] setDuration:0.4f]

Change animation time for properties of a CALayer

I have a CALayer to animate a change in its image contents. Now, how can I change how long it takes for this animation to take place?
You can just call:
[CATransaction setAnimationDuration:durationSecs]
in -layoutSublayers or anywhere else that you modify the layers and expect them to implicitly animate. This will effect the current implicit transaction and any sub-transactions within this one.
A different way to do this:
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:2.5f] forKey:kCATransactionAnimationDuration];
//Perform CALayer actions, such as changing the layer contents, position, whatever.
aCALayerObject.contents = [self newCALayerContents];
[CATransaction commit];
That code would animate the change of the CALayer's contents over 2.5 seconds. You can also use this to disable all animations completely. Like this:
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
It's more or less simple. You have an ivar CALayer *yourLayer. Then you set the delegate and implement the delegate method -(id<CAAction>)actionForLayer:forKey:
- (void)awakeFromNib {
yourLayer.delegate = self;
yourLayer.name = #"yourLayer";
}
- (id <CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event {
if([layer.name isEqualToString yourLayer.name]) { // Check for right layer
CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:event]; // Default Animation for 'event'
ani.duration = .5; // Your custom animation duration
return ani;
} else return nil; // Default Animation
}

Resources