自己定义UIView以实现自绘
有时候我们须要自绘uiview以实现自己的需求,比方依据坐标点绘制出连续的曲线(股票走势图),就须要自绘uiview了。
原理:继承uiview类(customView),并实现custom view的drawRect即可。
首先看一下效果图:
代码例如以下:
// .h
#import <UIKit/UIKit.h>
@interface CustomView :
UIView
@end
//.m
#import "CustomView.h"
@implementation CustomView
-(id)initWithFrame:(CGRect)frame{
//重写initWithFrame时,不要忘了以下一句
self = [superinitWithFrame:frame];
if (self) {
self.backgroundColor = [UIColorwhiteColor];
}
return
self;
}
//重写drawRect方法(不须要调用)。绘制想要的图像
-(void)drawRect:(CGRect)rect{
CGContextRef context =
UIGraphicsGetCurrentContext();//context:一块内存区域。将它看做当前view的画布即可了。
NSDictionary *attribute = [NSDictionary
dictionaryWithObjectsAndKeys:[UIFont
systemFontOfSize:
15.0f],
NSFontAttributeName, [UIColor
redColor],
NSForegroundColorAttributeName, nil];
[@"股市走势图"
drawInRect,
,
)
withAttributes:attribute] ;//绘画标题
//曲线
CGContextMoveToPoint(context,
,
);
CGContextAddLineToPoint(context,
,
);
CGContextAddLineToPoint(context,
,
);
CGContextAddLineToPoint(context,
,
);
//CGContextStrokePath(context);
//三角形
CGContextMoveToPoint(context,
,
);
CGContextAddLineToPoint(context,
,
);
CGContextAddLineToPoint(context,
,
);
CGContextClosePath(context);
CGContextStrokePath(context);
//CGContextFillPath(context);
//矩形
CGContextAddRect(context,
CGRectMake,
,
));
[[UIColor
colorWithRed
green
blue
alpha]
set];
//CGContextStrokePath(context);//空心
CGContextFillPath(context);//实心
//圆
, , ,
, *M_PI,
);
CGContextStrokePath(context);
}
@end
然后在须要用到该view的view controller的
viewDidLoad下加入西;以下代码就能够了!
CustomView *view = [[CustomView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.view addSubview view];
自己定义UIView以实现自绘的更多相关文章
- 关于UIView及其子类重绘drawRect
转载自:https://nezha.gitbooks.io/ios-developmentarticles/content/UIView%E7%9A%84drawRect%E9%87%8D%E7%BB ...
- iOS----自定义UIView,绘制一个UIView
绘制一个UIVIew最灵活的方式就是由它自己完成绘制.实际上你不是绘制一个UIView,你只是子类化了UIView并赋予子类绘制自己的能力.当一个UIVIew需要执行绘图操作的时,drawRect:方 ...
- 【Android】利用自己定义View的重绘实现拖动移动,获取组件的尺寸
以下利用一个app来说明怎样利用自己定义View的重绘实现拖动移动.获取组件的尺寸. 例如以下图,触摸拖动,或者轻轻点击屏幕都能移动图片.假设碰到文字,则会弹出提示. 这里是利用自己定义View的重绘 ...
- UIView的剖析(转)
转自:http://blog.csdn.net/mengtnt/article/details/6716289 前面说过UIViewController,但是UIView也是在MVC中非常重要的一层 ...
- UIView的生命周期和layout方法总结
生命周期 1. loadView 什么时候调用? 每次访问UIViewController的view时候并且view == nil时候调用. 如何实现? 1> 如果在初始化UIViewContr ...
- 01-事件处理简介/UIView拖拽
1.ios当中常见的事件? 触摸事件 加速计事件 远程控制事件2.什么是响应者对象? 继承了UIResponds的对象我们称它为响应者对象 UIA ...
- iOS之 重绘机制
最近在看Core Animation , 今天来谈谈CALayer 和 UIView 中的重绘的一些认识: 我们都知道UIView里面有个成员layer,利用这个这个layer我们可以设置一些圆角,阴 ...
- 8. UIViewController
1. UIViewController 的认识 UIViewController在iOS开发中占据很重要的位置,iOS的整个UI开发的核心思想也是MVC的架构,从UIViewController的命名 ...
- Swift - 键盘弹起,遮挡输入框
extension LoginViewController:UITextFieldDelegate { func textFieldShouldReturn(textField: UITextFiel ...
随机推荐
- BZOJ 3231: [Sdoi2008]递归数列 (JZYZOJ 1353) 矩阵快速幂
http://www.lydsy.com/JudgeOnline/problem.php?id=3231 和斐波那契一个道理在最后加一个求和即可 #include<cstdio> #i ...
- [ZROI #316] ZYB玩字符串
Introduction 每次在一开始为空的串$S$的任意位置插入串$p$ 给出最终的$S$,求长度最短(相同时字典序最小)的串$p$ Solution: 样例出锅差评啊,让我这种直接看样例选手挂掉5 ...
- DP经典 BZOJ 1584: [Usaco2009 Mar]Cleaning Up 打扫卫生
BZOJ 1584: [Usaco2009 Mar]Cleaning Up 打扫卫生 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 419 Solve ...
- 05-树6. Path in a Heap (25) 小根堆
05-树6. Path in a Heap (25) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.patest.cn/contes ...
- bosondata/chrome-prerender: Render JavaScript-rendered page as HTML/PDF/mhtml/png/jpeg using headless Chrome
bosondata/chrome-prerender: Render JavaScript-rendered page as HTML/PDF/mhtml/png/jpeg using headles ...
- Django 中文显示
Django中文显示: 1.如要在.py文件中显示中文,在文件首行加上:# -*- coding: utf-8 -*- 2.如要在html文件中显示中文,要将文件保存为UTF-8格式 3.在setti ...
- nginx出现504 Gateway Time-out的解决思路
http://www.xbc.me/nginx-fix-504-gateway-timeout/ 在安装完Nginx+PHP-fpm+Mysql后 (如何安装LNMP环境,请参考快速配置LNMP环境N ...
- STM32 USB VBUS 监控
OTG_FS general core configuration register (OTG_FS_GCCFG) Bit 21 NOVBUSSENS: VBUS sensing disable op ...
- Retrieving ST-Link/V2 Firmware from Update Utility
http://www.taylorkillian.com/2013/01/retrieving-st-linkv2-firmware-from.html http://forum.easyelectr ...
- 扩展名为的proto的文件
扩展名为的proto的文件 1. 编写proto文件 首先需要一个proto文件,其中定义了我们程序中需要处理的结构化数据: // Filename: addressbook.proto synta ...