iphone自定义view画面(绘画)

 

1. 自定义父类 View, 将游戏画面共同属性函数放在父类

//  游戏画面控制对象

1.1BaseView.h 头文件

@interface BaseView : UIView {

       // 上下文环境

  CGContextRef currentContext;

 

  BOOL mbLoop;// 绘图控制标记

  BOOL mFlag;// 控制需要的时候绘图

  int stationId;// 状态 ID

  CGContextRef baseCGContext;// 绘画上下文环境     

}

@property (nonatomic, retain) NSTimer *pingMe;

@property long offlineTime;// 掉线时间

@property CGContextRef baseCGContext;// 绘画上下文环境

@property BOOL mbLoop;// 绘图控制标记

@property BOOL mFlag;// 控制需要的时候绘图

@property int stationId;// 状态 ID

1.2BaseVeiw.m 实现类

#import "BaseView.h"

#import "NetControl.h"

#import "GameRoomConst.h"

#import "SuperMahjongNet.h"

extern CEncTcp *g_pSi;    // 后台通信线程全局对象

@implementation BaseView;

@synthesize baseCGContext;// 画面上下文的 二级缓存

@synthesize mbLoop;// 绘图控制标记

@synthesize mFlag;// 控制需要的时候绘图

@synthesize stationId;// 状态 ID

- (id)initWithFrame:(CGRect)frame {                 // 所有的绘画在此函数或子类的此函数中完成

  NSLog(@"Enter BaseView!!!");

    if (self = [super initWithFrame:frame]) {

 

   

  // 数据初始化

  [self initBaseData];

 

  // 指定色彩空间为 RGB

  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

 

  // 创建 BitmapContext 大小为 View 自身大小

  baseCGContext = CGBitmapContextCreate(NULL, self.frame.size.width, self.frame.size.height, 8, 4*self.frame.size.width, colorSpace, kCGImageAlphaPremultipliedFirst);

 

  // 释放色彩空间

  CGColorSpaceRelease(colorSpace);

 

  mFlag = false;

  // 开始绘制图片

  mbLoop = true;

 

  // 启动心跳线程

  if(pingMe == nil){

   pingMe = [[NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(goPingMe) userInfo:nil repeats:YES] retain];

  }

  }

  [self setNeedsDisplay];

    return self;

}

// 绘制控制主方法

-(void)draw{

  NSAutoreleasePool *drawPool = [[NSAutoreleasePool alloc] init];

 

  [self setNeedsDisplay];// 刷新画面,自动调用 drawRect ()函数

 

  [drawPool release];

}

}

 

 

2. 写画面子类,完成具体游戏的绘画

#import <UIKit/UIKit.h>

#import "BaseView.h"

1.MahjongGameView.h 文件

@interface MahjongGameView : BaseView {

  NSTimer *drawFrame;// 控制画面刷新

  UIFont *paintFont;

  /********** 坐标数据 ********* /

  CGRect myScreenCGRect;  // 屏幕矩形大小数据

  CGRect drawCGRect;  // 绘画大小数据

  int STATUBAR_Height;// 状态栏高度

}

2.MahjongGameView.m 文件

#import "MahjongGameView.h"

#import "BaseControl.h"

@implementation MahjongGameView

-(id)initWithFrame:(CGRect)frame {

  NSLog(@"Enter SuperMahjongView!!!");

    if (self = [super initWithFrame:frame]) {

 

  // 图片资源初始化

  [self initBasicGameCanvas];

   

  // 坐标数据初始化

  [self initBaseData];

 

  // 设置本人椅子号

  self.myTableID = g_myGamePlayer.wTableID;// 设置桌子号

  [self findPlayerChairs:g_myGamePlayer.cbChairID];// 设置桌子号

 

  // 初始化玩家

  [self updateGamePlayer];

 

  // 启动绘画线程,调用父类的 draw 函数,子类不重写此函数

  drawFrame = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(draw) userInfo:nil repeats:YES] retain];

 

  // 切屏幕

  [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];

 

// 切换坐标始终为左上点

  CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI/2.0f);

  self.transform = transform;

  self.center = CGPointMake(160, 240);

 

  [[UIApplication sharedApplication ] setStatusBarHidden:YES];

  }

    return self;

 

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云台095

省钱后记得打赏哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值