自定义NSView

【Cocoa(mac) Application 开发系列之三】自定义NSView并绘制一些常见的图形及字符串;

本站文章均为 李华明Himi 原创,转载务必在明显处注明:(作者新浪微博: @李华明Himi 
转载自【黑米GameDev街区】 原文链接: http://www.himigame.com/mac-cocoa-application/908.html
 

          �7�4 点击订阅 �7�2 本博客最新动态!及时将最新博文通知您!

                 

不多说比较简单直接给出源码大家作为参考吧。

对于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, 30, 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

�0�2注意的是:备注�0�2这两行代码是直接通过获取画布更新画布进行绘制其中绘制代码省略比较容易这里起个头推荐使用此种方式绘制;对于做游戏的我们对于画布刷新画布我想是再熟悉不过了!


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值