iPhone UIDatePicker 非常慢

发布于 2024-12-18 11:00:57 字数 3554 浏览 1 评论 0原文

我在 ModeTime 上使用 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

那么为什么我的选择器很慢并且卡住了? 我错过了什么?

附:我已经评论了 nsDateFormatter 以查找这是否是问题所在,但没有改变......

非常感谢!

Im using a UIDatePicker on ModeTime,

but is very slow??

here the code:

#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

So why is my picker slow and getting stucked?
what im i missing??

ps. I have commented the nsDateFormatter to find if that was the problem, but no change...

thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日记撕了你也走了 2024-12-25 11:00:57

我最初的反应:

分配一个 NSDateFormatter 并不是一个便宜的前景。在您的情况下,您一遍又一遍地使用相同的格式字符串,因此您应该真正分配一个 NSDateFormatter,将其保存为实例变量,然后在 >UIDatePicker 操作触发。这应该会大大加快速度。

My initial reaction:

Allocating an NSDateFormatter isn't a cheap prospect. In your case, you're using the same format string over and over again, so you should REALLY just be allocating a single NSDateFormatter, saving it as an instance variable, and then using that when the UIDatePicker action fires. That should speed things up considerably.

旧人哭 2024-12-25 11:00:57

要添加到之前的答案的另一件事是您忘记提交动画。我什至建议您使用 UIView 类的 animateWith... 方法而不是 beginAnimation/commitAnimation ,因为它应该更快(至少苹果如此)。
还有一件事要走。手势识别器...为什么不在初始化时使用视图创建它?为什么每次调用选择器时都要创建它?

Another thing to add to previous answer is that you've forgot to commit your animation. I'll even suggest you to use animateWith... methods of the UIView class rather than beginAnimation/commitAnimation as it should be faster (at least by apple).
And one more to go. The gesture recognizer... why not create it with the view at the init time? why create it every time you call the picker?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文