iOS开发之手动绘制时钟模拟器

本文介绍如何利用iOS的Core Graphics框架,在应用中实现一个动态更新的时钟模拟器。模拟器包括表盘、时针、分针和秒针,能够根据系统时间实时调整显示。同时,提供了开始和暂停功能,可以控制时钟的转动。通过自定义clock,设置颜色和尺寸,以及使用贝塞尔曲线绘制指针,最终呈现出逼真的时钟效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


用系统提供的corez2D框架,将我们现实生活中的钟表在xode中画出来,并显示在应用程序上,钟表上包括表盘、时针、分针、秒针,当我们运行程序的时候,程序将根据系统当前时间去设置该钟表的时间,当点击暂停按钮后,时间模拟器将暂停,当点击开始按钮后不会从你停止的那个时间转动,会跳到系统当前时间再转动。当系统时间发生改变的时候,将获取当前系统的时间,计算出指针的旋转角度,并做出相应旋转,从而达到现实生活中指针转动的效果。


实现原理:

1.自定义clock,通过外面将一些属性值传进来,通过这些值设置表盘的颜色,时针、分针、秒针的颜色

@property(nonatomic,strong)UIColor *clockTintColor;

@property(nonatomic,assign)CGFloat clockBorderWidth;
@property(nonatomic,strong)UIColor *clockBorderColor; 

@property(nonatomic,strong)UIColor *hourHandColor;
@property(nonatomic,assign)CGFloat hourHandWidth;

@property(nonatomic,strong)UIColor *minuteHandColor; 
@property(nonatomic,assign)CGFloat minuteHandWidth;

@property (nonatomic, strong) UIColor *secondHandColor;         
@property (nonatomic, assign) CGFloat secondHandWidth;          

@property (nonatomic, strong) NSDictionary *momentAttribute;    
@property (nonatomic, strong) NSArray *momentList;              

@property (nonatomic, strong) UIColor *centerPointColor;        
@property (nonatomic, assign) CGFloat centerPointRadius;   

2.获取上下文对象,通过外部给表盘设置的大小,计算表盘的半径,并将表盘的边框和背景绘制

    CGContextRef context = UIGraphicsGetCurrentContext();
        
    //绘制表盘边框和背景色
    CGContextSetStrokeColorWithColor(context, _clockBorderColor.CGColor);
    CGContextSetFillColorWithColor(context, _clockTintColor.CGColor);
    CGContextSetLineWidth(context, _clockBorderWidth);
    
    _radius = (self.frame.size.width < self.frame.size.height)?self.frame.size.width/2.f : self.frame.size.height/2.f;
    _radius = _radius - _clockBorderWidth;
    CGContextAddArc(context, CGRectGetWidth(self.frame)/2.f, CGRectGetHeight(self.frame)/2.f, _radius, 0.f, 2*M_PI, 0.f);
    CGContextDrawPath(context, kCGPathFillStro
dCol='000000';//date colour. fCol='000000';//face colour. sCol='000000';//seconds colour. mCol='000000';//minutes colour. hCol='000000';//hours colour. ClockHeight=40; ClockWidth=40; ClockFromMouseY=0; ClockFromMouseX=100; //Alter nothing below! Alignments will be lost! d=new Array("SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"); m=new Array("JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"); date=new Date(); day=date.getDate(); year=date.getYear(); if (year < 2000) year=year+1900; TodaysDate=" "+d[date.getDay()]+" "+day+" "+m[date.getMonth()]+" "+year; D=TodaysDate.split(''); H='...'; H=H.split(''); M='....'; M=M.split(''); S='.....'; S=S.split(''); Face='1 2 3 4 5 6 7 8 9 10 11 12'; font='Arial'; size=1; speed=0.6; ns=(document.layers); ie=(document.all); Face=Face.split(' '); n=Face.length; a=size*10; ymouse=0; xmouse=0; scrll=0; props=""; props2=""; Split=360/n; Dsplit=360/D.length; HandHeight=ClockHeight/4.5 HandWidth=ClockWidth/4.5 HandY=-7; HandX=-2.5; scrll=0; step=0.06; currStep=0; y=new Array();x=new Array();Y=new Array();X=new Array(); for (i=0; i < n; i++){y[i]=0;x[i]=0;Y[i]=0;X[i]=0} Dy=new Array();Dx=new Array();DY=new Array();DX=new Array(); for (i=0; i < D.length; i++){Dy[i]=0;Dx[i]=0;DY[i]=0;DX[i]=0} if (ns){ for (i=0; i < D.length; i++) document.write(''+props2+D[i]+''); for (i=0; i < n; i++) document.write(''+props+Face[i]+''); for (i=0; i < S.length; i++) document.write(''+S[i]+''); for (i=0; i < M.length; i++) document.write(''+M[i]+''); for (i=0; i < H.length; i++) document.write(''+H[i]+''); } if (ie){ document.write(''); for (i=0; i < D.length; i++) document.write(''+props2+D[i]+''); document.write(''); document.write(''); for (i=0; i < n; i++) document.write(''+props+Face[i]+''); document.write(''); document.write(''); for (i=0; i < H.length; i++) document.write(''+H[i]+''); document.write(''); document.write(''); for (i=0; i < M.length; i++) document.write(''+M[i]+''); document.write('') document.write(''); for (i=0; i < S.length; i++) document.write(''+S[i]+''); document.write('') } (ns)?window.captureEvents(Event.MOUSEMOVE):0; function Mouse(evnt){ ymouse = (ns)?evnt.pageY+ClockFromMouseY-(window.pageYOffset):event.y+ClockFromMouseY; xmouse = (ns)?evnt.pageX+ClockFromMouseX:event.x+ClockFromMouseX; } (ns)?window.onMouseMove=Mouse:document.onmousemove=Mouse; function ClockAndAssign(){ time = new Date (); secs = time.getSeconds(); sec = -1.57 + Math.PI * secs/30; mins = time.getMinutes(); min = -1.57 + Math.PI * mins/30; hr = time.getHours(); hrs = -1.575 + Math.PI * hr/6+Math.PI*parseInt(time.getMinutes())/360; if (ie){ Od.style.top=window.document.body.scrollTop; Of.style.top=window.document.body.scrollTop; Oh.style.top=window.document.body.scrollTop; Om.style.top=window.document.body.scrollTop; Os.style.top=window.document.body.scrollTop; } for (i=0; i < n; i++){ var F=(ns)?document.layers['nsFace'+i]:ieFace[i].style; F.top=y[i] + ClockHeight*Math.sin(-1.0471 + i*Split*Math.PI/180)+scrll; F.left=x[i] + ClockWidth*Math.cos(-1.0471 + i*Split*Math.PI/180); } for (i=0; i < H.length; i++){ var HL=(ns)?document.layers['nsHours'+i]:ieHours[i].style; HL.top=y[i]+HandY+(i*HandHeight)*Math.sin(hrs)+scrll; HL.left=x[i]+HandX+(i*HandWidth)*Math.cos(hrs); } for (i=0; i < M.length; i++){ var ML=(ns)?document.layers['nsMinutes'+i]:ieMinutes[i].style; ML.top=y[i]+HandY+(i*HandHeight)*Math.sin(min)+scrll; ML.left=x[i]+HandX+(i*HandWidth)*Math.cos(min); } for (i=0; i < S.length; i++){ var SL=(ns)?document.layers['nsSeconds'+i]:ieSeconds[i].style; SL.top=y[i]+HandY+(i*HandHeight)*Math.sin(sec)+scrll; SL.left=x[i]+HandX+(i*HandWidth)*Math.cos(sec); } for (i=0; i < D.length; i++){ var DL=(ns)?document.layers['nsDate'+i]:ieDate[i].style; DL.top=Dy[i] + ClockHeight*1.5*Math.sin(currStep+i*Dsplit*Math.PI/180)+scrll; DL.left=Dx[i] + ClockWidth*1.5*Math.cos(currStep+i*Dsplit*Math.PI/180); } currStep-=step; } function Delay(){ scrll=(ns)?window.pageYOffset:0; Dy[0]=Math.round(DY[0]+=((ymouse)-DY[0])*speed); Dx[0]=Math.round(DX[0]+=((xmouse)-DX[0])*speed); for (i=1; i < D.length; i++){ Dy[i]=Math.round(DY[i]+=(Dy[i-1]-DY[i])*speed); Dx[i]=Math.round(DX[i]+=(Dx[i-1]-DX[i])*speed); } y[0]=Math.round(Y[0]+=((ymouse)-Y[0])*speed); x[0]=Math.round(X[0]+=((xmouse)-X[0])*speed); for (i=1; i < n; i++){ y[i]=Math.round(Y[i]+=(y[i-1]-Y[i])*speed); x[i]=Math.round(X[i]+=(x[i-1]-X[i])*speed); } ClockAndAssign(); setTimeout('Delay()',20); } if (ns||ie)window.onload=Delay;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值