如何在iPhone中创建带有小时和秒钟自定义图像的数字时钟?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在iPhone中创建带有小时和秒钟自定义图像的数字时钟?相关的知识,希望对你有一定的参考价值。
我有10张10-10张图片来设置我的数字时钟的小时和分钟。我对如何将自定义图像设置为数字时钟以使其开始工作感到困惑。
这是我的代码:
- (void)viewDidLoad {
//timer to recursively call the showClock method.
timer=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showClock) userInfo:nil repeats:YES];
[super viewDidLoad];
}
-(void)showClock
{
NSDate *dateShow= [NSDate dateWithTimeIntervalSinceNow:0];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setTimeStyle:NSDateFormatterMediumStyle];
NSString *dateString = [dateFormat stringFromDate:dateShow];
NSDate * date = [NSDate date];
NSCalendar * calendar = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents * components =
[calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSInteger firstHourDigit = hour/60;
NSInteger secondHourDigit = hour%60;
NSInteger firstMinuteDigit = minute/60;
NSInteger secondMinuteDigit = minute%60;
[minfirstImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"0.png", firstHourDigit]]];
[minsecondImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%0.png",secondHourDigit]]];
[secfirstImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%0.png",firstMinuteDigit]]];
[secSecondImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%0.png",secondMinuteDigit]]];
}
当我运行我的应用程序时,图像视图不会与图像一起显示。可能是什么问题?计算中有问题吗?
答案
从0.png-9.png获取图像名称,并尝试使用以下代码,它将帮助您
NSDate *dateShow= [NSDate dateWithTimeIntervalSinceNow:0];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setTimeStyle:NSDateFormatterMediumStyle];
NSString *dateString = [dateFormat stringFromDate:dateShow];
NSDate * date = [NSDate date];
NSCalendar * calendar = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents * components =
[calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSInteger firstHourDigit = hour/10;
NSInteger secondHourDigit = hour%10;
NSInteger firstMinuteDigit = minute/10;
NSInteger secondMinuteDigit = minute%10;
int res=0;
for(int i =0;i<4;i++)
{
if(i==0)
res = firstHourDigit;
else if(i==1)
res = secondHourDigit;
else if (i==2)
res = firstMinuteDigit;
else
res = secondMinuteDigit;
NSString *str_imageName;
switch (res) {
case 0:
str_imageName=@"0.png";
break;
case 1:
str_imageName=@"1.png";
break;
case 2:
str_imageName=@"2.png";
break;
case 3:
str_imageName=@"3.png";
break;
case 4:
str_imageName=@"4.png";
break;
case 5:
str_imageName=@"5.png";
break;
case 6:
str_imageName=@"6.png";
break;
case 7:
str_imageName=@"7.png";
break;
case 8:
str_imageName=@"8.png";
break;
case 9:
str_imageName=@"9.png";
break;
default:
break;
}
if(i==0)
[minfirstImage setImage:[UIImage imageNamed:str_imageName]];
else if(i==1)
[minsecondImage setImage:[UIImage imageNamed:str_imageName]];
else if (i==2)
[secfirstImage setImage:[UIImage imageNamed:str_imageName]];
else
[secSecondImage setImage:[UIImage imageNamed:str_imageName]];
}
另一答案
女王......
我认为使用不同的图像来计时是增加你的应用程序大小,所以我有一个建议,你只需检查这个代码并根据你的要求进行更改我认为这是显示你想要的时间的最佳方式,
对于此代码,您只需创建一个没有文本的背景图像...
- (void)viewDidLoad {
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showClock) userInfo:nil repeats:YES];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)showClock
{
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate* now = [NSDate date];
int h = [[cal components:NSHourCalendarUnit fromDate:now] hour];
int m = [[cal components:NSMinuteCalendarUnit fromDate:now] minute];
int s = [[cal components:NSSecondCalendarUnit fromDate:now] second];
[cal release];
// NSInteger firstHourDigit = hour/60;
// NSInteger secondHourDigit = hour%60;
// NSInteger firstMinuteDigit = minute/60;
// NSInteger secondMinuteDigit = minute%60;
NSLog(@"
Time :%d : %d : %d",h,m,s);
[imgViewHour setImage:[self getImageFor:[NSString stringWithFormat:@"%d",h]]];
[imgViewMnt setImage:[self getImageFor:[NSString stringWithFormat:@"%d",m]]];
[imgViewSec setImage:[self getImageFor:[NSString stringWithFormat:@"%d",s]]];
}
-(UIImage *)getImageFor:(NSString*)txt {
int w =50,h=35;
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(3, 3, w-6, h-6)];
lbl.text = txt;
lbl.textColor = [UIColor whiteColor];
lbl.textAlignment = UITextAlignmentCenter;
lbl.font = [UIFont fontWithName:@"Arial" size:18];
lbl.font = [UIFont boldSystemFontOfSize:14];
[lbl setBackgroundColor:[UIColor clearColor]];
lbl.shadowOffset=CGSizeMake(0,2);
lbl.shadowColor = [UIColor blackColor];
UIView *viewImage = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, h)];
[viewImage setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"imageBackGround.png"]]];
[viewImage addSubview:lbl];
UIGraphicsBeginImageContext(viewImage.bounds.size);
[viewImage.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *Image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[viewImage release];
return Image;
}
谢谢 ... :)
另一答案
维护一组UIImages然后按字符分解字符串以查看其中的数字:
create array for images
load images into array
create array for imageviews
put imageviews into array
loop 5 times
if we're on 2 then skip // dot
else create substring from that character and call integerValue on it
use that as index into array
set imageview at index to image at index
以上是关于如何在iPhone中创建带有小时和秒钟自定义图像的数字时钟?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Android Studio 中创建自定义图像按钮?
如何在 iOS 中创建一个非常自定义的 uibutton,如 UITableViewCellStyleSubtitle
使用 6 行 7 列的 Uibutton 数组在 iphone 中创建自定义日历