
本站文章均为 李华明Himi 原创,转载务必在明显处注明:
转载自【黑米GameDev街区】 原文链接: http://www.himigame.com/hibernate/783.html
☞ 点击订阅 ☜ 本博客最新动态!及时将最新博文通知您!
不多说,比较简单,直接给出源码,大家作为参考吧。
对于Cocoa application 话说回来比较easy,如果之前你接触过iOS 的IB的话 ,基本上没什么可说的,上手很快;
因此对于Cocoa Application 不会太快的更新,接着Himi将对之前做的动编彻底的更新一版;后期有时间将继续更新此系列;
言归正传,对于自定义NSVIEw上一篇已经介绍了,而且本篇主要继续深入NSVIEw的drawRect进行继续研究,示例代码如下,比较容易理解,而且都已备注的很清楚;
//// HVIEw.m//// Created by Himi on 12-6-7.// copyright (c) 2012年 Himi. All rights reserved.//#import "HVIEw.h"@implementation HVIEw- (ID)initWithFrame:(NSRect)frame{ self = [super initWithFrame:frame]; if (self) { // Initialization code here. } return self;}- (voID)drawRect:(NSRect)dirtyRect{ //-----------获取整个MyVIEw尺寸------------ NSRect screen = [self bounds]; int SW = screen.size.wIDth; int SH = screen.size.height; //-----------设置整个MyVIEw的颜色------------ [[NScolor graycolor] set]; //-----------填充整个MyVIEw--------------- NSRectFill(screen); //-----------绘制字符串--------------- Nsstring * strH= @"基础绘制 --By Himi"; //--绘制不带属性字符串 [strH drawAtPoint:NSMakePoint(SH*0.5,SH-30) withAttributes:NulL]; //--绘制带属性字符串 NSMutableDictionary *md = [NSMutableDictionary dictionary]; [md setobject:[NSFont FontWithname:@"Times" size:20] forKey:NSFontAttributename]; [strH drawAtPoint:NSMakePoint(SH*0.5,SH-70) withAttributes:md]; //--------绘制矩形---- NSRect rect1 = NSMakeRect(SH*0.5,SH-100,30,20); NSRect rect2 = NSMakeRect(SH*0.5,SH-130,20); //填充矩形 [NSBezIErPath fillRect:rect1]; //绘制矩形 [NSBezIErPath strokeRect:rect2]; //--------绘制线条(十字线)---- [[NScolor greencolor] set]; NSPoint bm =NSMakePoint(SW*0.5,0); NSPoint top =NSMakePoint(SW*0.5,SH); NSPoint lf =NSMakePoint(0,SH*0.5); NSPoint rt =NSMakePoint(SW,SH*0.5); [NSBezIErPath strokelineFromPoint:bm topoint:top]; [NSBezIErPath strokelineFromPoint:lf topoint:rt]; //-------绘制椭圆 [[NScolor redcolor] set]; [[NSBezIErPath bezIErPathWithovalInRect:screen] stroke]; [self setNeedsdisplay:YES];// 强制绘画//【备注】// CGContextRef ref = [[NSGraphicsContext currentContext] graphicsPort];// 获取画布// CGContextFlush(ref);//刷新画布}-(voID) mouseDragged:(NSEvent *)theEvent{ NSPoint mp = [self convertPoint:[theEvent locationInWindow] fromVIEw:nil];// 鼠标新坐标 NSLog(@"mouseDragged~%f,%f",mp.x,mp.y);}-(voID) mouseUp:(NSEvent *)theEvent{ NSLog(@"mouseUp");}-(voID) mouseDown:(NSEvent *)theEvent{ NSLog(@"mouseDown");}@end 注意的是:备注 这两行代码是直接通过获取画布,更新画布进行绘制,其中绘制代码省略,比较容易,这里起个头,推荐使用此种方式绘制;对于做游戏的我们,对于画布刷新画布我想是再熟悉不过了!
运行截图:
总结以上是内存溢出为你收集整理的【Cocoa(mac) Application 开发系列之三】自定义NSView并绘制一些常见的图形及字符串全部内容,希望文章能够帮你解决【Cocoa(mac) Application 开发系列之三】自定义NSView并绘制一些常见的图形及字符串所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)