MAC开发--仿QQ登录界面的下拉式抽屉效果

#import "ViewController.h"
@import QuartzCore;

#define kChildWindowH 76
@interface ViewController()
@property (nonatomic, strong) NSWindow *childWindow;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSButton *drawerButton = [[NSButton alloc] initWithFrame:NSMakeRect((self.view.bounds.size.width - 20) / 2.0, 20, 20, 20)];
    [drawerButton setButtonType:NSToggleButton];
    [drawerButton setBordered:NO];
    [drawerButton setImage:[NSImage imageNamed:@"down"]];
    [drawerButton setAlternateImage:[NSImage imageNamed:@"up"]];
    [drawerButton setImagePosition:NSImageOnly];
    [drawerButton setState:NSControlStateValueOff];
    [drawerButton setTarget:self];
    [drawerButton setAction:@selector(drawerButtonClick:)];
    [self.view addSubview:drawerButton];
}

-(NSWindow *)childWindow
{
    if (!_childWindow) {
        NSRect rect = CGRectMake(self.view.window.frame.origin.x, self.view.window.frame.origin.y - kChildWindowH, self.view.window.frame.size.width, kChildWindowH);
        _childWindow = [[NSWindow alloc] initWithContentRect:rect styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskFullSizeContentView backing:NSBackingStoreBuffered defer:NO];
        _childWindow.titlebarAppearsTransparent = YES;
        _childWindow.titleVisibility = NSWindowTitleHidden;
        [self.view.window addChildWindow:_childWindow ordered:NSWindowBelow];
    }
    return _childWindow;
}

- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];
    
    // Update the view, if already loaded.
}

- (void)drawerButtonClick:(id)sender {
    
    NSButton *button = (NSButton *)sender;
    if (button.state == NSOffState) { //隐藏
        self.childWindow.hasShadow = NO;
        [NSAnimationContext runAnimationGroup:^(NSAnimationContext * _Nonnull context) {
            context.allowsImplicitAnimation = YES;
            context.duration = 0.3;
            context.timingFunction = [CAMediaTimingFunction functionWithName:@"easeOut"];
            
            NSRect rect = [self contentWindowFrameWithclosed:YES];
            [self.childWindow setFrame:rect display:YES];
        } completionHandler:^{
            
        }];
        
    }
    else { //显示
        [NSAnimationContext runAnimationGroup:^(NSAnimationContext * _Nonnull context) {
            context.allowsImplicitAnimation = YES;
            context.duration = 0.3;
            context.timingFunction = [CAMediaTimingFunction functionWithName:@"easeIn"];
            
            NSRect rect = [self contentWindowFrameWithclosed:NO];
            [self.childWindow setFrame:rect display:YES];
        } completionHandler:^{
            self.childWindow.hasShadow = YES;
        }];
    }
    
}

- (NSRect)contentWindowFrameWithclosed:(BOOL)bClosed
{
    NSRect rectClose = NSZeroRect;
    NSRect parentFrame = self.view.window.frame;
    NSRect contentFrame = NSMakeRect(0, 0, self.view.window.frame.size.width, kChildWindowH + 22);
    NSRect fixRect = NSInsetRect(parentFrame, 0.5 * (NSWidth(parentFrame) - NSWidth(contentFrame)), 0.5 * (NSHeight(parentFrame) - NSHeight(contentFrame)));
    fixRect.origin.y = NSMinY(parentFrame) - (!bClosed ? NSHeight(contentFrame) - 22 : 0);
    rectClose = fixRect;
    
    return rectClose;
}
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容