标签:钟表 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);
[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