我想实现一个功能,当用户将鼠标悬停在特定区域上时,新视图将显示为类似抽屉的动画.而且,当用户离开特定区域时,抽屉应该消失动画.这正是您在OS X中悬停在屏幕底部时看到的内容,其中Dock出现并随动画消失.
但是,如果我使用动画实现该功能,则在完成动画之前重新进入特定区域时,它将无法正常工作mouseExited:.这是我的代码:
let trackingArea = NSTrackingArea(rect: CGRectMake(0, 0, 120, 300), options: NSTrackingAreaOptions.ActiveAlways | NSTrackingAreaOptions.MouseEnteredAndExited, owner: self, userInfo: nil)
underView.addTrackingArea(trackingArea) // underView is the dummy view just to respond to the mouse tracking, since the drawerView's frame is changed during the animation; not sure if this is the clean way...
override func mouseEntered(theEvent: NSEvent) {
let frameAfterVisible = CGRectMake(0, 0, 120, 300)
NSAnimationContext.runAnimationGroup({
(context: NSAnimationContext!) in
context.duration = 0.6
self.drawerView.animator().frame = frameAfterVisible
}, completionHandler: { () …Run Code Online (Sandbox Code Playgroud) 我创建了两个NSAnimation用另一个视图翻转视图的对象.我想同时运行2个这些动画.我无法使用NSViewAnimation,因为它现在是关于任何视图属性的动画.
这是动画创作:
self.animation = [[[TransitionAnimation alloc] initWithDuration:1.0 animationCurve:NSAnimationEaseInOut] autorelease];
[self.animation setDelegate:delegate];
[self.animation setCurrentProgress:0.0];
[self.animation startAnimation];
Run Code Online (Sandbox Code Playgroud)
我试图链接2个动画,但可能由于某种原因它不起作用.我举了一个例子: Apple开发者网站
配置NSAnimation要使用的对象NSAnimationNonblocking根本不显示任何动画...
编辑:第二个动画与第一个动画完全相同,并在创建第一个动画的同一个地方创建.
TransitionAnimation是一个子类NSAnimation,setCurrentProgress看起来像这样:
- (void)setCurrentProgress:(NSAnimationProgress)progress {
[super setCurrentProgress:progress];
[(NSView *)[self delegate] display];
}
Run Code Online (Sandbox Code Playgroud)
的delegate是NSView在这种情况下,在其功能的drawRect施加时间依赖CIFilter于一个CIImage.问题是它运行同步,第二个动画在第一个动画结束后立即开始.有没有办法同时运行它们?
我有一个基本的Mac应用程序,该应用程序具有通过自动版式完成的视图动画:
?动画将使其看起来好像视图从右侧滑入。
动画自动版式更改的推荐方法是:
NSAnimationContext.runAnimationGroup()allowsImplicitAnimation为true动画块内view.layoutSubtreeIfNeeded()在动画块内调用我遵循了此建议,并且在macOS Sierra上一切正常,但是在macOS High Sierra上,动画不再发生。取而代之的是,视图在没有动画的情况下显示在其最终位置。
我找到了一种解决方法:我使用将动画安排在下一个运行循环周期中DispatchQueue.main.async。但是,这似乎是一种hack,我想知道这里是否还缺少其他东西。
这是我的实际代码:
private func appendSlideViewControllerAnimated(_ viewController:NSViewController, to viewToTheLeft:NSView)
{
// Insert the new view on the very right, just outside the parent:
viewController.view.frame = self.view.bounds
viewController.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(viewController.view)
viewController.view.topAnchor.constraint( equalTo: view.topAnchor ).isActive = true
viewController.view.bottomAnchor.constraint( equalTo: view.bottomAnchor ).isActive = true
viewController.view.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
viewController.view.leadingAnchor.constraint(equalTo: viewToTheLeft.trailingAnchor).isActive = true
// Update the layout after we just added the view …Run Code Online (Sandbox Code Playgroud) cocoa autolayout nsanimationcontext nsanimation macos-high-sierra
我正在开发一个简单的 Mac 应用程序。我希望能够在 NSButton 上应用转换并使其更大。它的两倍大小。但是我的代码不起作用,它只是将按钮滑动到角落。有谁知道出了什么问题?
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
[numberButton1.animator.layer setAffineTransform:CGAffineTransformMakeScale(4.0, 4.0)];
numberButton1.layer.anchorPoint = CGPointMake(0.5, 0.5);
[numberButton1.animator.layer setAffineTransform:CGAffineTransformMakeScale(1.0, 1.0)];
} completionHandler:nil];
Run Code Online (Sandbox Code Playgroud)
更新 1
我尝试了以下操作,但它没有做任何事情:(
CGAffineTransform test = CGAffineTransformMakeScale(4.0, 4.0);
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *ctx) {
numberButton1.animator.layer.affineTransform = test;
} completionHandler:^{
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *ctx) {
numberButton1.animator.layer.affineTransform = CGAffineTransformIdentity;
} completionHandler:^{
NSLog(@"Done...");
}];
}];
Run Code Online (Sandbox Code Playgroud)
更新 2
这是我的代码 sudo,希望这会有所帮助:
我的头 (.h) 文件:
#import <Cocoa/Cocoa.h>
#import <QuartzCore/QuartzCore.h>
@interface ViewController : NSViewController {
IBOutlet NSButton *numberButton1;
}
-(IBAction)demo:(id)sender;
@end
Run Code Online (Sandbox Code Playgroud)
我的实现(.m)文件:
#import "ViewController.h"
@implementation ViewController
-(IBAction)demo:(id)sender {
CGRect …Run Code Online (Sandbox Code Playgroud) cocoa ×4
nsanimation ×4
animation ×1
autolayout ×1
core-image ×1
macos ×1
objective-c ×1
osx-lion ×1
swift ×1
transform ×1