CoreAnimation ne fonctionne pas avec -setAlphaValue sur QTMovieView
-
06-07-2019 - |
Question
J'ai un code de base pour ajouter un QTMovieView. Je veux qu'il se fonde, alors j'ajoute une animation sur setAlphaValue. Le seul problème est que cela ne fonctionne pas. La valeur alpha est instantanément définie sur ce à quoi elle était supposée être animée. Si j'essaye d'animer, par exemple setFrame: ça fonctionne bien. Vous trouverez mon code ci-dessous. C’est le 10.5.7 avec le SDK Mac OS X 10.5.
- (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];
}
Des idées?
La solution 3
J'ai commencé avec QTMovieLayer, mais étant moins puissant (bien entendu) que QTMovieView, il a ouvert une autre boîte de problèmes. La solution consistait à utiliser NSAnimation sur QTMovieView. J'ai un cours NSAnimation qui ressemble un peu à ceci:
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
Ce qui peut alors être utilisé comme ceci:
animation = [[AlphaAnimation alloc] initWithDuration:0.5 effect:AAFadeIn object:movieView];
[animation setAnimationBlockingMode:NSAnimationNonblocking];
[animation startAnimation];
Si vos QTMovieViews sont en plein écran, ce n'est pas très simple, cependant.
Autres conseils
Je n'en suis pas certain, mais il semble que QTMovieView ne prenne pas en charge la composition alpha. Un autre test que je voudrais essayer serait d'appeler setWantsLayer sur la vue d'ensemble du QTMovieView pour voir si cela affecte la capacité de QTMovieViews à composer correctement. Cela ne fonctionnera probablement pas.
Je suggère NSViewAnimation. C'est une sous-classe de NSAnimation qui fera la transition en fondu pour vous.