2011-10-03 78 views
0
當我運行我的應用程序的ImageView
i am having 10 images from 0-10 for setting the hour and minutes of my digital clock .i am getting confused on how to set a custom image to digital clock so that it will start working. Please anybody help me in solving my problem .Thanks. 

this is my code 

- (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]]]; 


} 

不與圖像顯示。什麼可能是problem.Is在iphone小時,秒的自定義圖像數字時鐘在計算他們的任何問題如何創建具有

+0

你知道^ h與UIImageView一起工作? – taskinoor

+0

@taskinoor是的,我知道與uiimageview – Rani

+1

工作實際上是什麼問題?你不知道如何維護一個計時器,或者你不知道如何獲得當前時間,或者你不知道如何根據計時器改變圖像?請更具體一些。 – taskinoor

回答

0

拍攝圖像的名字從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: 
     [email protected]"0.png"; 
     break; 
    case 1: 
     [email protected]"1.png"; 
     break; 
    case 2: 
     [email protected]"2.png"; 
     break; 
    case 3: 
     [email protected]"3.png"; 
     break; 
    case 4: 
     [email protected]"4.png"; 
     break; 
    case 5: 
     [email protected]"5.png"; 
     break; 
    case 6: 
     [email protected]"6.png"; 
     break; 
    case 7: 
     [email protected]"7.png"; 
     break; 
    case 8: 
     [email protected]"8.png"; 
     break; 
    case 9: 
     [email protected]"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]]; 


} 
+0

只是現在檢查之前,我只是忘記分號 – Narayana

+0

項目代碼可能會更好 – Gank

0

保持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 
+0

我完全變得困惑。請你可以詳細解釋我或給我一些示例代碼 – Rani

1

拉尼...

我認爲時機使用不同的圖像是增加你的應用程序的大小,所以我有一個建議,您剛纔檢查這個代碼更改爲按照您的要求,我認爲這是最好的的方式來顯示你想要的時間,,

此代碼,你必須只創建一個沒有文字的背景圖片...

- (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(@"\n 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; 

    } 

謝謝... :)

+0

檢查我試試這個代碼與演示應用程序..它的工作完美... – Raj