2009-10-25 112 views
0

我有一些添加QTMovieView的基本代码。我希望它淡入,所以我在setAlphaValue上添加了一个动画。唯一的问题是它不起作用。 alpha值立即设置为它应该动画的任何值。如果我尝试动画,例如setFrame:它工作正常。你会在下面找到我的代码。这在Mac OS X 10.5 SDK的10.5.7版本中。CoreAnimation不能在QTMovieView上使用-setAlphaValue

- (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]; 

    } 

任何想法?

回答

1

我开始使用QTMovieLayer,但与QTMovieView相比,它不那么强大(当然),它打开了另一盒问题。解决方案是在QTMovieView上使用NSAnimation。我有一个NSAnimation类看着有点像这样:

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 

然后可以这样使用:

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

[animation setAnimationBlockingMode:NSAnimationNonblocking]; 
[animation startAnimation]; 

如果您的QTMovieViews全屏显示,它并不是非常流畅。

3

我不确定,但听起来像QTMovieView可能不支持alpha合成。我想尝试的另一个测试是在QTMovieView的超级视图上调用setWantsLayer以查看是否影响QTMovieViews正确组合的能力。可能不会工作。

您可以尝试使用QTMovieLayer代替。合并QTMovieLayer的一种方法是创建自己的NSView子类,创建并管理添加到其根层中的QTMovieLayer。请注意,当在同一个窗口中混合支持层的视图和非层支持的视图时,如果支持层的视图不是全部位于前面或后面并重叠,则可能会发出有趣的排序。

+0

感谢您的反馈。奇怪的是,我可以例如做[movieView setAlphaValue:0.5],它会是正确的。所以,我的下一个方法是手动启动一个新线程,该线程将调用一个方法,直到alphaValue == 1.0为止。但是看起来很愚蠢,就像Core Animation那样。 将尝试线程方法,并明天回来。 –

+0

这很有趣,所以它会出现它支持阿尔法。这里有一些其他的事情要尝试。 尝试制作QTMovieView的子类并覆盖setAlphaValue :,然后使用NSLog记录正在设置的值并查看它是否使用多次看起来像动画值的值进行设置。 而不是运行自己的线程尝试使用[NSAnimation] [1],它可能会节省您一些努力。 [1]: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSAnimation_Class/Reference/Reference.html –

+0

从线程更改UI的方面不是好主意。你应该只从主线程那样做。 (思考实验:如果用户导致您同时淡出电影视图两次,该怎么办?)我接下来是动画超级视图和QTMovieLayer建议。 –

1

我建议NSViewAnimation。这是NSAnimation的一个子类,它会为你淡入淡出。

+0

我删除了这个答案,因为NSViewAnimation不能用于QTMovieView,请参阅:http://lists.apple.com/archives/cocoa-dev/2005/Sep/msg01475.html –