2012-11-28 44 views
0

動畫之前的所有代碼:這個動畫有什麼問題來調出pickerview?

int height = 255; 

//create new view 
self.NewView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, 320, height)]; 
self.NewView.backgroundColor = [UIColor colorWithWhite:1 alpha:1]; 

//add toolbar 
UIToolbar * toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 45)]; 
toolbar.barStyle = UIBarStyleBlack; 

//add buttons 
UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:nil action:@selector(dismissCustom:)]; 

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:nil action:@selector(cancelView:)]; 

toolbar.items = [NSArray arrayWithObjects:cancelButton, flexible, infoButtonItem, nil]; 
//add date picker 
self.datePicker = [[UIDatePicker alloc] init]; 
self.datePicker.datePickerMode = UIDatePickerModeDateAndTime ; 
self.datePicker.hidden = NO; 
self.datePicker.date = [NSDate date]; 
self.datePicker.frame = CGRectMake(0, 40, 320, 250); 
[self.datePicker addTarget:self action:nil forControlEvents:UIControlEventValueChanged]; 
[self.NewView addSubview:self.datePicker]; 

[self.NewView addSubview:toolbar]; 

動畫代碼以使視圖可見:

__block CGRect temp = self.NewView.frame; 
    temp.origin.y = CGRectGetMaxY(self.view.bounds); 
    [UIView animateWithDuration:0.5 animations:^{temp.origin.y -= height, self.NewView.frame = temp;} completion:^(BOOL finished) { 
     [self.view addSubview:self.NewView];}]; 

我有幾乎相同的代碼來擺脫它,它工作正常!

int height = 0; 
__block CGRect temp = self.NewView.frame; 
temp.origin.y = CGRectGetMaxY(self.view.bounds); 
[UIView animateWithDuration:0.5 animations:^{temp.origin.y -= height, self.NewView.frame = temp;} completion:^(BOOL finished) { 
    [self.NewView removeFromSuperview];}]; 

任何人都知道什麼是錯的?該視圖只出現在滑動的位置。

回答

1

問題是你只添加NewView作爲子視圖AFTER動畫完成。你必須添加它之前,像這樣:

__block CGRect temp = self.NewView.frame; 
temp.origin.y = CGRectGetMaxY(self.view.bounds); 
[self.view addSubview:self.NewView]; 
[UIView animateWithDuration:0.5 animations:^{temp.origin.y -= height, self.NewView.frame = temp;} completion:nil]; 
+0

哦好吧,救了我很多麻煩:)非常感謝 – agierens