Objective c Can';无法让[[id Animator]setAlphaValue:0.0]工作

Objective c Can';无法让[[id Animator]setAlphaValue:0.0]工作,objective-c,cocoa,macos,animation,Objective C,Cocoa,Macos,Animation,我正在尝试为NSImageView的不透明度淡出+帧大小更改设置动画。帧大小工作平稳,但由于某些原因,alpha值在动画开始时直接跳到其最终值。我已经尝试过将superview重新分层,但我的想法已经没有了。这是我的代码示例-> AABLCLogo -> NSImageView IBOutlet speed -> 1.0f 我怀疑这是在主线程上设置动画的问题。在大多数情况下,如果要同时设置两个对象的动画,则需要使用NSViewAnimation。使用此代码作为指

我正在尝试为NSImageView的不透明度淡出+帧大小更改设置动画。帧大小工作平稳,但由于某些原因,alpha值在动画开始时直接跳到其最终值。我已经尝试过将superview重新分层,但我的想法已经没有了。这是我的代码示例->

AABLCLogo   -> NSImageView IBOutlet
speed       -> 1.0f 


我怀疑这是在主线程上设置动画的问题。在大多数情况下,如果要同时设置两个对象的动画,则需要使用NSViewAnimation。使用此代码作为指导,它在淡出部分内容的同时调整窗口大小

NSRect newWindowFrame = NSMakeRect(self.window.frame.origin.x, self.window.frame.origin.y, 640, self.window.frame.size.height);
NSMutableArray *viewAnimations = [NSMutableArray array];
NSDictionary *windowSizeDict = [NSDictionary dictionaryWithObjectsAndKeys:self.window, NSViewAnimationTargetKey, [NSValue valueWithRect:newWindowFrame], NSViewAnimationEndFrameKey, nil];
[viewAnimations addObject:windowSizeDict];

NSDictionary *animateOutDict = [NSDictionary dictionaryWithObjectsAndKeys:self.dataSidebarController.containerView, NSViewAnimationTargetKey, NSViewAnimationFadeOutEffect, NSViewAnimationEffectKey, nil];
[viewAnimations addObject:animateOutDict];

NSViewAnimation *animation = [[NSViewAnimation alloc] initWithViewAnimations:viewAnimations];
[animation setDuration:0.35];
[animation setAnimationBlockingMode:NSAnimationNonblocking];
[animation setDelegate:self];
[animation startAnimation];

您希望将setWantsLayer:YES发送到正在设置动画的视图,而不是其superview

NSView的setAlphaValue:仅适用于层备份或层宿主实例,因为它将值转发到NSView层的不透明度属性


有关完整的故事,请阅读苹果文档中NSView的setAlphaValue:的“讨论”。

我相信您请求的图层在下次绘制视图之前不会创建

因此,在添加动画之前,您需要强制显示superview或等待运行循环的下一次迭代

NSRect newWindowFrame = NSMakeRect(self.window.frame.origin.x, self.window.frame.origin.y, 640, self.window.frame.size.height);
NSMutableArray *viewAnimations = [NSMutableArray array];
NSDictionary *windowSizeDict = [NSDictionary dictionaryWithObjectsAndKeys:self.window, NSViewAnimationTargetKey, [NSValue valueWithRect:newWindowFrame], NSViewAnimationEndFrameKey, nil];
[viewAnimations addObject:windowSizeDict];

NSDictionary *animateOutDict = [NSDictionary dictionaryWithObjectsAndKeys:self.dataSidebarController.containerView, NSViewAnimationTargetKey, NSViewAnimationFadeOutEffect, NSViewAnimationEffectKey, nil];
[viewAnimations addObject:animateOutDict];

NSViewAnimation *animation = [[NSViewAnimation alloc] initWithViewAnimations:viewAnimations];
[animation setDuration:0.35];
[animation setAnimationBlockingMode:NSAnimationNonblocking];
[animation setDelegate:self];
[animation startAnimation];