iPhone中自绘实现步骤
2012-02-16 17:02
155 查看
1. 继承@interface MyView : UIView {
2. 实现- (void)drawRect:(CGRect)rect
3. 调用addSubView把新生成的view加入进来显示
addSubView[window addSubview:viewController.view];
4.示例代码
- (void)drawRect:(CGRect)rect {
// create the bitmap context
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(nil,100,100,8,400, colorSpace,kCGImageAlphaPremultipliedLast);
CFRelease(colorSpace);
// create an arrow image
// set the fill color
CGColorRef fillColor = [[UIColor blackColor] CGColor];
CGContextSetFillColor(context, CGColorGetComponents(fillColor));
CGContextBeginPath(context);
CGContextMoveToPoint(context, 8.0f, 13.0f);
CGContextAddLineToPoint(context, 24.0f, 4.0f);
CGContextAddLineToPoint(context, 24.0f, 22.0f);
CGContextClosePath(context);
CGContextFillPath(context);
CGContextSelectFont ( context, "Arial", 10.f, kCGEncodingMacRoman );
CGContextSetRGBFillColor ( context, 0.0f, 0.0f, 0.f, 1.f );
CGContextSetShouldAntialias ( context, 0 );
CGContextShowText(context, "hh", 2);
// convert the context into a CGImageRef
CGImageRef image = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage* image2 = [UIImage imageWithCGImage:image];
[image2 drawInRect:CGRectMake(0, 0, 120, 160)];
NSString* myStr = @"中文";
UIFont* font = [UIFont systemFontOfSize:12.0];
[myStr drawInRect: CGRectMake(160, 240, 100, 130) withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
渐变
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGGradientRef glossGradient;
CGColorSpaceRef rgbColorspace;
size_t num_locations = 3;
CGFloat locations[3] = { 0.0, 0.5, 1.0 };
CGFloat components[12] = { 208/255.0, 208/255.0, 208/255.0, 0.95, // Start color
208/255.0, 208/255.0, 208/255.0, 0.60,
208/255.0, 208/255.0, 208/255.0, 0.10 }; // End color
rgbColorspace = CGColorSpaceCreateDeviceRGB();
glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
CGContextFillRect(currentContext, self.frame);
CGRect currentBounds = self.bounds;
CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f);
CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMaxY(currentBounds));
CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, midCenter, 0);
CGGradientRelease(glossGradient);
CGColorSpaceRelease(rgbColorspace);
}
2. 实现- (void)drawRect:(CGRect)rect
3. 调用addSubView把新生成的view加入进来显示
addSubView[window addSubview:viewController.view];
4.示例代码
- (void)drawRect:(CGRect)rect {
// create the bitmap context
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(nil,100,100,8,400, colorSpace,kCGImageAlphaPremultipliedLast);
CFRelease(colorSpace);
// create an arrow image
// set the fill color
CGColorRef fillColor = [[UIColor blackColor] CGColor];
CGContextSetFillColor(context, CGColorGetComponents(fillColor));
CGContextBeginPath(context);
CGContextMoveToPoint(context, 8.0f, 13.0f);
CGContextAddLineToPoint(context, 24.0f, 4.0f);
CGContextAddLineToPoint(context, 24.0f, 22.0f);
CGContextClosePath(context);
CGContextFillPath(context);
CGContextSelectFont ( context, "Arial", 10.f, kCGEncodingMacRoman );
CGContextSetRGBFillColor ( context, 0.0f, 0.0f, 0.f, 1.f );
CGContextSetShouldAntialias ( context, 0 );
CGContextShowText(context, "hh", 2);
// convert the context into a CGImageRef
CGImageRef image = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage* image2 = [UIImage imageWithCGImage:image];
[image2 drawInRect:CGRectMake(0, 0, 120, 160)];
NSString* myStr = @"中文";
UIFont* font = [UIFont systemFontOfSize:12.0];
[myStr drawInRect: CGRectMake(160, 240, 100, 130) withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
渐变
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGGradientRef glossGradient;
CGColorSpaceRef rgbColorspace;
size_t num_locations = 3;
CGFloat locations[3] = { 0.0, 0.5, 1.0 };
CGFloat components[12] = { 208/255.0, 208/255.0, 208/255.0, 0.95, // Start color
208/255.0, 208/255.0, 208/255.0, 0.60,
208/255.0, 208/255.0, 208/255.0, 0.10 }; // End color
rgbColorspace = CGColorSpaceCreateDeviceRGB();
glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
CGContextFillRect(currentContext, self.frame);
CGRect currentBounds = self.bounds;
CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f);
CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMaxY(currentBounds));
CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, midCenter, 0);
CGGradientRelease(glossGradient);
CGColorSpaceRelease(rgbColorspace);
}
相关文章推荐
- iPhone中自绘实现步骤
- 在iphone中使用AudioQueue来实现音频播放功能时最主要的步骤
- Android自绘实现步骤
- 多步骤进度,多个提交的实现
- iphone开发之UIButton按钮的使用(三)拖线实现 center和bounds实现控制组件的大小和位置
- Jquery UI震动效果实现原理及步骤
- WINCE下实现类似iphone滑动效果
- 【iphone游戏开发】iphone-Cocos2d游戏开发之四:精灵实现缩放,旋转,跳动,移动等动画
- iphone实现本地通知(类似推送)
- ios iphone ipad 开发应用内实现多语言自由切换 超级好用
- 不使用HOOK实现系统菜单的自绘
- 小白Windows7/10 64Bit安装Theano并实现GPU加速(没有MinGw等,详细步骤)
- 如何用代码实现iPhone手机软件注销和手机重启
- ZedBoard-自定义IP核实现+PS成功调用【详细步骤+流程介绍+源码】
- 实现自绘菜单
- iPhone消息推送机制实现与探讨
- CKEditor图片上传实现详细步骤(使用Struts 2)
- XST综合、实现过程包含哪些步骤
- 在App中实现iPhone主屏那样手指滑动翻页效果的代