Search code examples
cocoacore-animation

CoreAnimation doesn't work with -setAlphaValue on QTMovieView


I have some basic code for adding a QTMovieView. I want it to fade in, so I add an animation on setAlphaValue. Only issue is that it doesn't work. The alpha value is instantly set to whatever it was supposed to animate to. If I try animating e.g. setFrame: it works fine. You'll find my code below. This is on 10.5.7 with the Mac OS X 10.5 SDK.

    - (void)addMovie:(NSString *)aFile
    {
            QTMovieView     *aMovieView;
            QTMovie         *aMovie;
            NSRect          contentFrame;

            contentFrame = [[self contentView] frame];

            aMovieView = [[QTMovieView alloc]
                            initWithFrame:contentFrame];
            [aMovieView setWantsLayer:YES];
            [aMovieView setControllerVisible:NO];
            [aMovieView setPreservesAspectRatio:YES];

            aMovie = [[QTMovie alloc]
                            initWithFile:aFile error:nil];

            [aMovieView setMovie:aMovie];

            [aMovieView setAlphaValue:0.4];

            [[self contentView] addSubview:aMovieView];

            [NSAnimationContext beginGrouping];
            [[NSAnimationContext currentContext] setDuration:2.0];
            [[aMovieView animator] setAlphaValue:0.9];
            [NSAnimationContext endGrouping];

    }

Any ideas?


Solution

  • I began on QTMovieLayer, but being less powerfull (of course) than QTMovieView it opened another box of issues. The solution was to use NSAnimation on the QTMovieView. I have a NSAnimation class looking somewhat like this:

    AlphaAnimation.h

        #import <Cocoa/Cocoa.h>
    
        NSString * const AAFadeIn;
        NSString * const AAFadeOut;
    
        @interface AlphaAnimation : NSAnimation {
                NSView          *animatedObject;
                NSString        *effect;
        }
        - (id)initWithDuration:(NSTimeInterval)duration effect:(NSString *)effect object:(NSView *)object;
        @end
    

    AlphaAnimation.m

        #import "AlphaAnimation.h"
    
        NSString * const AAFadeIn = @"AAFadeIn";
        NSString * const AAFadeOut = @"AAFadeOut";
    
        @implementation AlphaAnimation
        - (id)initWithDuration:(NSTimeInterval)aDuration effect:(NSString *)anEffect object:(NSView *)anObject
        {
                self = [super initWithDuration:aDuration animationCurve:0];
    
                if (self) {
                        animatedObject = anObject;
                        effect = anEffect;
                }
    
                return self;
        }
    
        - (void)setCurrentProgress:(NSAnimationProgress)progress
        {
                if ([effect isEqual:AAFadeIn])
                        [animatedObject setAlphaValue:progress];
                else
                        [animatedObject setAlphaValue:1 - progress];
        }
        @end
    

    Which can then be used like this:

    animation = [[AlphaAnimation alloc] initWithDuration:0.5 effect:AAFadeIn object:movieView];
    
    [animation setAnimationBlockingMode:NSAnimationNonblocking];
    [animation startAnimation];
    

    If your QTMovieViews are in full screen it isn't very smooth, though.