mac 开发的资料太少了, 踩一个坑就记录一次,
--- 一直很不懂NSWindowController , NSViewController , NSWindow 之间的关系. 先放这图吧
http://www.cocoachina.com/bbs/read.php?tid-318386.html
---- 坐标系换了, 左下角是 (0,0)点 , 本来iOS的坐标第一次看的时候很别扭,觉得,该是左下角(0,0), 现在做iOS时间长了, 真到mac上反而蛋疼了... 还有 没有了 center
---- mac 上的 Frame 是 NSMake , 不是CGRect了 ,虽然是一样的,但是入乡随俗吧 . 因为看到了 这个 typedef CGRect NSRect ;
----.窗口和窗口的跳转,Mac开发下的window更像是iOS中的ViewController,窗口大小自己来定,而且转换窗口的办法要自己找合适的
----- NSWindow上添加NSViewDBSCustomView *view = [[DBSCustomView alloc] initWithFrame:NSMakeRect(100, 100, 100, 100)];
[self.window.contentView addSubview:view];
----.常用控件,最恶心的莫过于NSCollectionView,跟UICollectionView根本就是两码事.这里值得一提的是,如果你是新手,用到Mac下的NSIndexPath很有可能会找不到row去哪了...当时气得我要死 总之你直接用.item就行了 .
----. NSView是没有backgroundColor的, 想要设置需要这样
NSView * backColor = [[NSView alloc] initWithFrame:NSMakeRect(500, 300, 100, 100)];
backColor.wantsLayer = YES ;
backColor.layer.backgroundColor = [NSColor orangeColor].CGColor ;
[self.view addSubview:backColor];
这个方法也可以
#define D_GrayColor3 [NSColor colorWithSRGBRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1]
---- NSButton 的事件绑定
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
[D_GrayColor3 setFill];
NSRectFill(dirtyRect);
// Drawing code here.
}
NSButton *button=[[NSButton alloc]initWithFrame:NSMakeRect(100, 100, 100, 100)]; button.target=self; button.action=@selector(click);// 鼠标悬停上去可以添加提示信息, 事实上,tooltip 是NSView的一个属性 , 所有继承自NSview的类都可以有这个效果,为了做这个效果费了老大劲,最后发现只需要设置下属性就行了,哎, 先磨好屠龙宝刀,在干活绝对事半功倍啊button.toolTip = @"虎鼓瑟兮鸾回车" ; [self.view addSubview:button];对于想要定制Button的背景色,圆角, 试了很多方法,重写过 drawRect, 自定义了 Button的cell 还是不行, 一旦加了背景色和圆角 , Button的title 就得自己布局,不能居中显示了, 最后还是没有用代码加背景色和圆角,采用了 一个切好圆角和背景色的图片作为背景. 有知道怎么做的,记得留言,互相学习... http://www.cocoachina.com/bbs/read.php?tid-271752-page-1.html
----. 你会找不到label(去控件里找到label,看继承的类其实是NSTextField),所以如果想用纯代码初始化label就用textField代替
NSTextField * text =[[NSTextField alloc ]initWithFrame:NSMakeRect(100, 100, 200, 100)];
text.backgroundColor = [NSColor redColor]; // 终于有背景色了
text.stringValue = @"hello,world";
text.bezeled = NO ;
text.drawsBackground = NO ;
text.alignment = NSTextAlignmentCenter;
text.editable = NO ; // 是否可编辑, YES,相当于UITextView , NO ,相当于 UILabel
[self.view addSubview:text];
--- imageView 没什么大变化 , 多了一个属性 ,可以拖拽一张图片替换显示的图片 ,还有加圆角
NSImageView * ima = [[NSImageView alloc] initWithFrame:NSMakeRect(300, 400, 100, 100)];
ima.image = [NSImage imageNamed:@"tupian"];
ima.editable = YES ; // 可以拖放图片上去, 想要知道图片被替换的事件,需要继承 NSImageView 然后重写 setImage 就可以了
[self.view addSubview:ima];
//设置圆角
imgView.wantsLayer = YES;
imgView.layer.cornerRadius = 35.0f;
imgView.layer.borderWidth = 2;
imgView.layer.borderColor = [NSColor greenColor].CGColor;
imgView.layer.masksToBounds = YES;
NSImageView 设置填充模式 : NSImageScaleProportionallyDown = 0, // Scale image down if it is too large for destination. Preserve aspect ratio. 这个是保持比例,但是太大的话会把上面切掉
NSImageScaleAxesIndependently, // Scale each dimension to exactly fit destination. Do not preserve aspect ratio. 不保持比例,填满imageView
NSImageScaleNone, // Do not scale. 保持原图的大小显示
NSImageScaleProportionallyUpOrDown, // Scale image to maximum possible dimensions while (1) staying within destination area (2) preserving aspect ratio 保持比例,居中显示
------ //Mac程序是可以有多个window的! //设置当前的主窗口
NSApplication *app=[NSApplication sharedApplication];//获取程序对象
NSLog(@"%@",app.keyWindow);//keyWindow和mainWindow经常是同一个window,它们在程序刚加载的时
候是还没加载的,所以不能在viewDidLoad方法中设置!当app处于不活动的状态或不能接受键盘值时,也可能是空的
NSLog(@"%@",app.mainWindow);
NSWindow *window=app.keyWindow;
window.alphaValue=1;//设置窗口的透明度
window.backgroundColor=[NSColor clearColor];//设置背景颜色为透明
[window setStyleMask:1];//设置窗口的风格是没有边框的,也可以在故事板中的窗口属性中调
[window setFrame:CGRectMake(100, 100, 500, 500) display:YES animate:YES];//移动窗口,立即刷新,动画效果
//创建一个新的窗口,注意窗口控制器应当是个全局的指针,否则窗口会立即被销毁!
NSWindow *new=[[NSWindow alloc]initWithContentRect:CGRectMake(100,
100, 300, 300) styleMask:1 backing:NSBackingStoreRetained defer:NO];
new.backgroundColor=[NSColor redColor];
NSWindowController *wc=[[NSWindowController alloc]initWithWindow:new];
[wc showWindow:new];
[new setLevel:10];//设置窗口显示的优先级
----- NSOpenPanel 弹出选择文件用的控件NSOpenPanel *openPanel=[NSOpenPanel openPanel];//获取NSOpenPanel对象 //设置 [openPanel setCanChooseFiles:YES]; [openPanel setCanChooseDirectories:YES]; //[openPanel setAllowedFileTypes:[NSArray arrayWithObject:(NSString *)kUTTypeDirectory]]; [openPanel setAllowedFileTypes:@[@"mp3"]];//允许的类型 [openPanel setDirectoryURL:[NSURL fileURLWithPath:@"/Users/guodong/Desktop/song"]];//设置默认路径 [openPanel setAllowsMultipleSelection:YES];//允许按住shift/command多选 [openPanel runModal];//显示,并且返回结果 0取消了,1确认了 if ([openPanel runModal] == NSModalResponseOK) { NSURL *url = [openPanel URL]; NSLog(@"%@",[openPanel filenames]); }------ 最常用的tableview , 没有什么大的变化 ,就是接口改变了
//content Mode 这玩意在代码中好像没办法设置 设置Cell-Base/View-Base (必须设置)
//纯代码创建Cell-Base NSTableView
NSTableView *tableView;
tableView=[[NSTableView alloc]initWithFrame:CGRectMake(0, 0, 400, 300)];
tableView.delegate=nil;//代理 NSTableViewDelegate,NSTableViewDataSource
tableView.dataSource=nil;
//[tableView setAutosaveName:@"downloadTableView"];
//[tableView setAutoresizesSubviews:FULLSIZE];
//[tableView setBackgroundColor:[NSColor whiteColor]];
//[tableView setGridColor:[NSColor lightGrayColor]];
//[tableView setGridStyleMask: NSTableViewSolidHorizontalGridLineMask];
//[tableView setUsesAlternatingRowBackgroundColors:YES];
//[tableView setAutosaveTableColumns:YES];
//[tableView setAllowsEmptySelection:YES];
//[tableView setAllowsColumnSelection:YES];
NSScrollView *scrollView=[[NSScrollView alloc]initWithFrame:CGRectMake(0, 0, 400, 300)];
[scrollView setDocumentView:tableView];//设置内容视图
//[scrollView addSubview:tableView];虽然也能添加上,但是不能滑动什么的
//[scrollView setBackgroundColor:[NSColor redColor]];
[self.view addSubview:scrollView];
//设定表头
NSTableHeaderView *tableHeadView=[[NSTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, 300, 20)];
[tableView setHeaderView:tableHeadView];
//以前不知道的。。。设定列,可以设定几列,而且可调
NSTableColumn *column=[[NSTableColumn alloc] initWithIdentifier:@"column"];
[[column headerCell] setStringValue:@"column"];
//[[column headerCell] setAlignment:NSCenterTextAlignment];
[column setWidth:400];
[column setMinWidth:50];
[column setEditable:YES];
//[column setResizingMask:NSTableColumnAutoresizingMask | NSTableColumnUserResizingMask];
[tableView addTableColumn:column];
// Do any additional setup after loading the view.
/*创建Cell-Base NSTableView用的
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
return 100;
}
-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
return @"ecwve";
}
*/
/*创建View-Base NSTableView用的,用代码可能不行。。。
-(CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
{
NSLog(@"eww");
return 100;
}
-(NSView*)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSView *cellView=[[NSView alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
NSTextField *textField=[[NSTextField alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
[textField.cell setTitle:@"wcvc"];
[cellView addSubview:textField];
return cellView;
}
*/
// Do any additional setup after loading the view.
}
在popover中使用tableview时,cell中总是出现阴影,解决办法
self.tableView.appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua] ;
在iOS中我们需要将一个View
放在最上层,我们只需调用bringSubviewToFront:
方法即可,大致代码如下:
UIView *downView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
UIView *upView = [[UIView alloc] initWithFrame:CGRectMake(120, 120, 100, 100)];
downView.backgroundColor = [UIColor redColor];
upView.backgroundColor = [UIColor purpleColor];
[self.view addSubview:downView];
[self.view addSubview:upView];
[self.view bringSubviewToFront:downView];
这样就可以将downView移到最上层来。
但是在OS X中是没有这个方法的,OS X中有一个方法
- (void)addSubview:(NSView *)aView positioned:(NSWindowOrderingMode)place relativeTo:(NSView *)otherView
方法说明:
Inserts a view among the view’s subviews so it’s displayed immediately above or below another view.
参考文档已经说的很明显了,这个方法可以将一个View
放在上层或者下层。其中第二个NSWindowOrderingMode
类型的参数就是选择将View
放在哪里,参数为枚举类型,如下所示:
enum {
NSWindowAbove = 1,
NSWindowBelow = -1,
NSWindowOut = 0
};
typedef NSInteger NSWindowOrderingMode;
英文很直白,所以不再多说。
在OS X中需要将一个View
放在最上层,我们只需要调用
- addSubview:positioned:relativeTo:
方法即可。
想要让程序保持系统的长时间运行, 比如用户在看视频, ,让电脑 不熄灭屏幕, 不睡眠,用这个
// 保持屏幕常亮和后台进程
@property(nonatomic,strong) id activity ;
if (shouldLight) {
_activity = [ [NSProcessInfo processInfo] beginActivityWithOptions:NSActivityIdleDisplaySleepDisabled |NSActivityUserInitiated reason:@"play video"] ;
} else {
[[NSProcessInfo processInfo ] endActivity:_activity];
}
NSLog(@"系统开机时显示的用户名称 %@",[[NSProcessInfo processInfo] userName]);
NSLog(@"系统偏好设置里的共享中的电脑名称 %@",[NSHost currentHost].localizedName);