码迷,mamicode.com
首页 > 其他好文 > 详细

钟表效果

时间:2015-08-19 14:57:41      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:钟表   cgaffinetransformmak   transform   

最近在做一个项目,上面有钟表的显示。钟表的显示无非用到角度旋转。先看下效果图:

技术分享

制作过程如下:

先切出四张图:表盘,三个指针(沿着边界切

代码如下: 利用CGAffineTransformMakeRotation顺时针旋转,计算好弧度传入

//UI

//注意指针的添加顺序


 UIImageView *watchImageView =[[UIImageViewalloc]initWithFrame:CGRectMake(0,0,self.width,self.height)];

    watchImageView.image =PNGImage(@"watch");

    [selfaddSubview:watchImageView];


    //因为 CGAffineTransformMakeRotation旋转是以中心点进行旋转,所以要在各个指针下创建一个底部视图,然后把指针放上去,定好位置。使得实现指针旋转效果。

    UIView *minuteView = [[UIViewalloc]initWithFrame:CGRectMake((watchImageView.width -6.0)/2.0,0,6.0,160)];

    minuteView.backgroundColor =ClearColor;

    [selfaddSubview:minuteView];

    

    UIImageView *minuteImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,8,6.0,75)];

    minuteImageView.image =PNGImage(@"minute");

    [minuteViewaddSubview:minuteImageView];

    

    UIView *hourView = [[UIViewalloc]initWithFrame:CGRectMake((watchImageView.width -6.0)/2.0,0,6.0,160)];

    hourView.backgroundColor =ClearColor;

    [selfaddSubview:hourView];

    

    UIImageView *hourImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,18,6.0,65)];

    hourImageView.image =PNGImage(@"hour");

    [hourViewaddSubview:hourImageView];


    UIView *secondView = [[UIViewalloc]initWithFrame:CGRectMake((watchImageView.width -6.0)/2.0,0,6.0,160)];

    [selfaddSubview:secondView];

    UIImageView *secondImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,5,6.0,90)];

    secondImageView.image =PNGImage(@"second");

    [secondViewaddSubview:secondImageView];


//根据当前时间初始角度

     NSDate *today = [NSDatedate]; 

    

    NSCalendar *greCalendar = [[NSCalendaralloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    

    NSDateComponents *dateComponents = [greCalendarcomponents:NSCalendarUnitHour |NSCalendarUnitMinute | NSCalendarUnitSecondfromDate:today];

    NSInteger hour = dateComponents.hour

    NSInteger minute = dateComponents.minute;

    NSInteger second = dateComponents.second;

   

    float hourAngle = hour/6.0 *M_PI;

    float minuteAngle = minute/30.0 *M_PI;

    float secondAngle = second/30.0 *M_PI;

    

    hourView.transform =CGAffineTransformMakeRotation(hourAngle);

   minuteView.transform =CGAffineTransformMakeRotation(minuteAngle);

   secondView.transform =CGAffineTransformMakeRotation(secondAngle);


//起定时器,每0.05s刷新一次,实现均匀旋转

[NSTimerscheduledTimerWithTimeInterval:0.05target:selfselector:@selector(watchRotation)userInfo:nilrepeats:YES];


// 计算得知,每0.05s,秒针旋转M_PI/600,分针旋转M_PI/36000,时针旋转M_PI/43200

- (void)watchRotation

{

    _secondAngle +=M_PI/600;

    if (_secondAngle/2.0 >M_PI) {

        _secondAngle =0;  //注意归0

    }

    _watchImageView.secondView.transform =CGAffineTransformMakeRotation(_secondAngle);

    

    _minuteAngle +=M_PI/36000;

    if (_minuteAngle/2.0 >M_PI) {

        _minuteAngle =0;

    }

    _watchImageView.minuteView.transform =CGAffineTransformMakeRotation(_minuteAngle);

    

    _hourAngle +=M_PI/43200;

    if (_hourAngle/2.0 >M_PI) {

        _hourAngle =0;

    }

    _watchImageView.hourView.transform =CGAffineTransformMakeRotation(_hourAngle);

}


这就哦了,这个可以进行封装,在此就不进行赘述了。

版权声明:本文为博主原创文章,未经博主允许不得转载。

钟表效果

标签:钟表   cgaffinetransformmak   transform   

原文地址:http://blog.csdn.net/oik_ios/article/details/47779041

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!