利用UIGraphics绘制一个会走的时钟


Controller

@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);

}

运行结果



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值