gpt4 book ai didi

iPhone UIDatePicker 很慢

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:00:04 27 4
gpt4 key购买 nike

我在 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 以查找是否是问题所在,但没有改变...

非常感谢!

最佳答案

我的第一 react :

分配一个 NSDateFormatter 并不便宜。在你的例子中,你一遍又一遍地使用相同的格式字符串,所以你真的应该只分配一个 NSDateFormatter,将它保存为一个实例变量,然后在 UIDatePicker Action 触发。这应该会大大加快速度。

关于iPhone UIDatePicker 很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8291912/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com