利用UIGraphics绘制一个会走的时钟
2016-06-02 20:36
483 查看
Controller
.h
.m
运行结果
@implementation VCRoot - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _clockView=[[CustomView alloc]init]; _clockView.frame=CGRectMake(40, 40, 240, 400); _clockView.backgroundColor=[UIColor cyanColor]; _clockView.alpha=0.5f; //通知系统让cview视图重新绘制; [self.view addSubview:_clockView]; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if (_mTimer==nil){ _mTimer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES]; }else{ [_mTimer invalidate]; _mTimer=nil; } } -(void)updateTimer:(NSTimer*)mTimer { //每秒钟走6度;也就是 1/30*PI static int i=0; NSLog(@"%d",i); float a=0.0f; a=M_PI*i/30; NSLog(@"a=%f",a); NSLog(@"sina=%f",sin(a)); NSLog(@"cosa=%f",cos(a)); //秒针的终点坐标 _clockView.pointEndX1=120+sinf(a)*100; _clockView.pointEndY1= 200-cosf(a)*100; //重新加载 [_clockView setNeedsDisplay]; i++; //每到60要归0; if (i==60) { i=0; } }
.h
#import <UIKit/UIKit.h> #define LEN 20; #define RADIOUS 100; #define PI 3.14159; @interface CustomView : UIView { //中心点 float _centerX; float _centerY; } //背景 @property(assign,nonatomic)float pointStartX; @property(assign,nonatomic)float pointStartY; @property(assign,nonatomic)float pointEndX; @property(assign,nonatomic)float pointEndY; //用于接收传过来的值.秒针的终点坐标 @property(assign,nonatomic)float pointEndX1; @property(assign,nonatomic)float pointEndY1; @end
.m
@implementation CustomView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code //(120,200) 内径100; _centerX=120; _centerY=200; CGContextRef content=UIGraphicsGetCurrentContext(); float angle=0; while (angle<=360){ _pointStartX= _centerX+cosf(angle/180*M_PI)*RADIOUS; _pointStartY=_centerY+sinf(angle/180*M_PI)*RADIOUS; _pointEndX= _pointStartX+cosf(angle/180*M_PI)*LEN; _pointEndY=_pointStartY+sinf(angle/180*M_PI)*LEN; UIColor*colorLine=[UIColor blackColor]; CGContextMoveToPoint(content, _pointStartX, _pointStartY); [colorLine setStroke]; CGContextAddLineToPoint(content, _pointEndX, _pointEndY); CGContextStrokePath(content); angle+=30; } //计时器每加一,以下的部分执行一次; UIColor*colorLine=[UIColor blueColor]; CGContextMoveToPoint(content, _centerX, _centerY); [colorLine setStroke]; CGContextAddLineToPoint(content, _pointEndX1, _pointEndY1); CGContextStrokePath(content); }
运行结果
相关文章推荐
- 解决Guidline muste specify type方案
- easyUI中文乱码
- leetcode 300. Longest Increasing Subsequence-最长子序列|动态规划
- 触摸UITextView找到该触摸点的文字
- Qt之操作数据库(SQLite) [此博文包含图片] (2013-12-09 16:58:05) 转载 ▼ 标签: qt sqlite qsqldatabase qsqlquery qsqlerro
- NGUI制作流光效果
- NGUI组件整理总结
- UIView与CALayer
- Build Your Own Lisp chapter4的小错误 error: editline/history.h: No such file or directory的解决
- iOS UITableView+FDTemplateLayoutCell 配合AutoLayout分分钟教你实现动态高度自适应
- 【UGUI实战】游戏右上角CoinBar的自动调整
- UIView Class Reference
- Android UI
- UITableViewCellAccessoryType
- Scala Java Error: value filter is not a member of *
- JSP中Out和Request对象详解
- ueditor浏览器 无法上传文件.问题
- build-helper-maven-plugin插件
- require与require.async区别
- UIscrollVIew代理方法调用时机