MAC开发--仿QQ登录界面的下拉式抽屉效果
2017-11-21 本文已影响162人
背靠背的微笑
#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