作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用从顶部滑动动画将一个 View 替换为另一个 View 。我有点遵循 http://gentlebytes.com/2011/09/auto-layout-in-lion/ 中发布的指南但没有 NSConstraints。
不幸的是,我的动画的委托(delegate)方法animationDidStop:finished:在动画结束时没有被调用。
这是我的代码:
- (void)slideViewControllerFromTop:(NSViewController *)controller {
[self replacePlaceholderViewWith:controller block:^(NSView *placeholder, NSView *currentView, NSView *newView) {
NSRect initialFrameNewView = NSMakeRect(placeholder.bounds.origin.x, 2*placeholder.bounds.size.height - newView.frame.size.height - DISTANCE_FROM_TOP, placeholder.bounds.size.width, newView.frame.size.height);
[newView setFrame:initialFrameNewView];
NSRect finalFrameNewView = NSMakeRect(placeholder.bounds.origin.x, placeholder.bounds.size.height - newView.frame.size.height - DISTANCE_FROM_TOP, 0.91*placeholder.bounds.size.width, newView.frame.size.height);
NSRect finalFrameCurrentView = NSMakeRect(currentView.frame.origin.x, currentView.frame.origin.y-placeholder.bounds.size.height, currentView.frame.size.width, currentView.frame.size.height);
CABasicAnimation *animation = [CABasicAnimation animation];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[animation setDelegate:self];
[animation setDuration:0.5];
animation.removedOnCompletion = YES;
[newView setAnimations:[NSDictionary dictionaryWithObject:animation forKey:@"frame"]];
[currentView setAnimations:[NSDictionary dictionaryWithObject:animation forKey:@"frame"]];
[placeholder addSubview:newView];
[NSAnimationContext beginGrouping];
[[newView animator] setFrame:finalFrameNewView];
[[currentView animator] setFrame:finalFrameCurrentView];
[NSAnimationContext endGrouping];
}];
}
- (void)replacePlaceholderViewWith:(NSViewController *)controller block:(GBPlaceholderReplaceBlock)handler {
if (controller == self.currentViewController) return;
self.previousViewController = self.currentViewController;
self.currentViewController = controller;
NSView *placeholderView = self.contentView;
NSView *currentView = self.contentView.subviews.lastObject;
NSView *newView = self.currentViewController.view;
handler(placeholderView, currentView, newView);
}
- (void)animationDidStop:(CABasicAnimation *)anim finished:(BOOL)flag {
if (!flag || self.contentView.subviews.count < 2) return;
[[self.contentView.subviews objectAtIndex:0] removeFromSuperview];
self.previousViewController = nil;
}
最佳答案
尝试替换这两个
[newView setAnimations:[NSDictionary dictionaryWithObject:animation forKey:@"frame"]];
[currentView setAnimations:[NSDictionary dictionaryWithObject:animation forKey:@"frame"]];
至
[newView.layer addAnimation:animation forKey:@"frame"];
[currentView.layer addAnimation:animation forKey:@"frame"];
并且不要忘记添加 #import <QuartzCore/QuartzCore.h>
在类文件的顶部。
关于cocoa - 动画DidStop :finished: not called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10204801/
我现在正在编写一个Bonjour服务监听器类,根据文档here: 目前,它似乎有效,我可以正确接收“netServiceBrowserWillSearch:”和“didFindService:more
我是一名优秀的程序员,十分优秀!