2011-11-28 88 views
2

我在ModeTime使用UIDatePicker,iPhone UIDatePicker很慢

但是速度很慢?

這裏的代碼:

#import "U2YAccountController.h" 

@implementation U2YAccountController 
@synthesize timePicker = _timePicker; 
@synthesize timeLabel= _timeLabel; 
@synthesize scheduleLabel = _scheduleLabel; 

- (id)init 
{ 
self = [super init]; 
if (self) { 
    // Set title 
    self.title = @"Reminder Settings"; 


    CGRect dosageImageRect = CGRectMake(10, 30, 300, 62); 
    UIImageView *dosageImage = [[UIImageView alloc] initWithFrame:dosageImageRect]; 
    [dosageImage setImage:[UIImage imageNamed:@"dosageReminderButton.png"]]; //image was a button, now just image change name of png! 
    dosageImage.opaque = YES; // explicitly opaque for performance 
    [self.view addSubview:dosageImage]; 
    [dosageImage release]; 




    UIButton *timeReminderButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [timeReminderButton setImage:[UIImage imageNamed:@"reminderTimeButton.png"] forState:UIControlStateNormal]; 
    [timeReminderButton setFrame:CGRectMake(10, 110, 300, 44)]; 
    [timeReminderButton addTarget:self action:@selector(timeReminderButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [timeReminderButton setAdjustsImageWhenHighlighted:NO]; 
    [self.view addSubview:timeReminderButton]; 


    self.timePicker = [[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 415, 220, 180)]autorelease]; 
    self.timePicker.datePickerMode = UIDatePickerModeTime; 
    [self.timePicker addTarget:self action:@selector(timePickerChanged:) forControlEvents:UIControlEventValueChanged]; 
    [self.view addSubview:self.timePicker]; 
    //[self.timePicker setHidden:YES]; 

    self.scheduleLabel = [[[UILabel alloc]initWithFrame:CGRectMake(22, 64, 200, 20)]autorelease]; 
    self.scheduleLabel.textColor = [UIColor colorWithRed:12/255.0 green:113/255.0 blue:186/255.0 alpha:1]; 
    self.scheduleLabel.text = @"Schedule 6 of 7 days"; //updated from web site!! 
    [self.view addSubview:self.scheduleLabel]; 

    self.timeLabel = [[[UILabel alloc]initWithFrame:CGRectMake(213, 122, 80, 20)]autorelease]; 
    self.timeLabel.textColor = [UIColor colorWithRed:12/255.0 green:113/255.0 blue:186/255.0 alpha:1]; 
    self.timeLabel.backgroundColor = [UIColor clearColor]; 
    self.timeLabel.text = @""; 
    [self.view addSubview:self.timeLabel]; 


    dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"hh:mma"]; 

} 
return self; 
} 


- (void) timeReminderButtonPressed :(id) sender { 


recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)] ; 


[UIView beginAnimations:@"PickerUp" context:nil]; 
[UIView setAnimationDuration:0.3f]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 

self.timePicker.frame = CGRectMake(0, 237, 320, 180); 

[self.view addGestureRecognizer:recognizer]; 

[recognizer release]; 



} 

- (void) timePickerChanged:(id) sender { 


today = [sender date]; 


NSString *dateString = [dateFormat stringFromDate:today]; 

self.timeLabel.text = dateString; 


} 

- (void) viewTapped :(id) sender { 
[UIView beginAnimations:@"PickerDown" context:nil]; 
[UIView setAnimationDuration:0.3f]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 

self.timePicker.frame = CGRectMake(0, 415, 320, 180); 

[self.view removeGestureRecognizer:recognizer]; 

} 


- (void) dealloc { 

[dateFormat release]; 


[_scheduleLabel release]; 
[_timeLabel release]; 
[_timePicker release]; 
[super dealloc]; 
} 

@end 

那麼,爲什麼我選擇器慢,被卡住? 我在想什麼?

ps。我已經評論nsDateFormatter,以發現是否是問題,但沒有變化...

非常感謝!

回答

3

添加到以前的答案的另一件事是,你忘了提交你的動畫。我甚至會建議你使用UIView類的animateWith ...方法,而不是beginAnimation/commitAnimation,因爲它應該更快(至少通過蘋果)。 還有一個要去。手勢識別器...爲什麼不在創建時使用視圖創建它?爲什麼每次打電話給選取器時都要創建它?

+0

謝謝!,那就是問題!,我創建了手勢識別器當選取器出現,因爲我需要響應水龍頭並隱藏選取器,並在選取器未顯示爲使用按鈕時將其禁用,謝謝! – MaKo

+0

您可以讓手勢識別器保持活動狀態。只需在需要時採取行動(當屏幕上的選取器)。有一點 - 如果 - (void)timeReminderButtonPressed:(id)發送者;你會阻止沉重的創造/毀滅。 – Ariel

6

我最初的反應:

分配一個NSDateFormatter是不是一個便宜的前景。在你的情況下,你一次又一次地使用相同的格式字符串,所以你應該只是分配一個NSDateFormatter,將它保存爲一個實例變量,然後在觸發UIDatePicker動作時使用它。這應該會大大加快速度。

+0

嗨,謝謝,我已經改變了在DateFormatter中使用實例變量的代碼,但是仍然很慢,,,還有什麼可以改變的嗎?,歡呼聲 – MaKo