Re: The simplest way to fade out a view
Re: The simplest way to fade out a view
- Subject: Re: The simplest way to fade out a view
- From: Nick <email@hidden>
- Date: Fri, 20 May 2011 21:44:08 +0300
Gustavo,
thanks for the response.
Unfortunately, this idea makes the view to blink..
The view fades away, but then when alpha reaches 0.0 it becomes 1.0
(and the view becomes opaque again for a little while) and after that
the view becomes hidden as that callback is being called.
I tried setting "additive" and "cumulative" properties to YES but it
didnt work either.
The issue is in that the animation discards what it had done to the
view's layer after it is finished. The alpha does not remain 0.0 after
it's finished - it becomes 1.0 again.
How could i force the animation to actually change the alpha value
from 1.0 to 0.0 and make it stay 0.0 unless i change it back?
-(IBAction)clickMeButton:(id)sender {
CABasicAnimation * alphaAnimation = [CABasicAnimation
animationWithKeyPath:@"opacity"];
alphaAnimation.delegate = self;
alphaAnimation.removedOnCompletion = YES;
alphaAnimation.autoreverses = NO;
alphaAnimation.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
alphaAnimation.duration = 1.0f;
alphaAnimation.toValue = [NSNumber numberWithFloat:0.0];
[[theView layer] addAnimation:alphaAnimation forKey:@"opacityAnimation"];
}
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
[theView setHidden:YES];
}
2011/5/20 Gustavo Pizano <email@hidden>:
> CAAnimation isn't that complicated.
>
> CABasicAnimation * alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
> alphaAnimation.delgate = self;
> alphaAnimation.removedOnCompletition = YES;
> alphaAnimation.autoreverses = NO;
> alphaAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
> alphaAnimation.duration = 1.0f;
> alphaAnimation.toValue = [NSNumber numberWithFloat:0.0];
>
> [myView.layer addAnimation:alphaAnimation forKey:@"opacityAnimation"];
>
> then you can implement the delegate's method
>
> - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
>
> and do whatever you need there after completition
>
> Note: remember to put your view to acceptLayer to YES;
>
> you can change that property anytime, but don't do it so ofthen :P
>
>
> I hope it helps
>
> BTW I wrote this on mail app so I hadn't compile it or run it.
>
>
>
> On May 20, 2011, at 12:35 PM, Nick wrote:
>
>> This sample works exactly as mine - the button remains on the screen
>> completely opaque and the suddenly disappears after 0.25 secs pass.
>> (Snow Leopard)
>>
>> So i guess unless i find time to read that huge guide about Core
>> Animation and manage to adapt CAAnimation, i will use that approach
>> with [[myview animator] setAlphaValue:0.0]; and a timer that hides the
>> view, which was suggested by Chase
>>
>>
>> 2011/5/20 Quincey Morris <email@hidden>:
>>> On May 19, 2011, at 15:01, Nick wrote:
>>>
>>>> No, only fade out (and fade in) effects are needed. I just added frame
>>>> movement to see if animation works at all. Animation does work, but
>>>> the fade does not happen - no matter, if the frame is moved
>>>> simultaneously or not.
>>>
>>> Here's my actual code for doing this. It works.
>>>
>>>> NSMutableArray* dictionaries = [NSMutableArray array];
>>>>
>>>> // Create a dictionary for the animation parameters
>>>>
>>>> NSMutableDictionary* dictionary = [NSMutableDictionary dictionaryWithObject: subview forKey: NSViewAnimationTargetKey];
>>>> [dictionaries addObject: dictionary];
>>>>
>>>> // Add a key for the target frame rect, if necessary
>>>>
>>>> if (!shouldBeHidden || !NSEqualRects (oldFrameRect, newFrameRect))
>>>> [dictionary setObject: [NSValue valueWithRect: newFrameRect] forKey: NSViewAnimationEndFrameKey];
>>>>
>>>> // If the visibility is changing, add a key for the fade in or out
>>>>
>>>> if (!isHidden != !shouldBeHidden)
>>>> {
>>>> [dictionary setObject: shouldBeHidden ? NSViewAnimationFadeOutEffect : NSViewAnimationFadeInEffect forKey: NSViewAnimationEffectKey];
>>>>
>>>> // Work around a Snow Leopard bug ...
>>>>
>>>> if (!shouldBeHidden)
>>>> [subview setHidden: shouldBeHidden];
>>>> }
>>>>
>>>> // Create the animation
>>>>
>>>> NSViewAnimation* viewAnimation = [[NSViewAnimation alloc] initWithViewAnimations: dictionaries];
>>>>
>>>> viewAnimation.duration = 0.25;
>>>> viewAnimation.animationCurve = NSAnimationEaseIn;
>>>>
>>>> [viewAnimation startAnimation];
>>>
>
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden