0

我制作了一个提示按钮,可以调出这个子视图,一切正常,但动画看起来很糟糕。这是启动它的代码:

    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, 40)];
toolbar.barStyle = UIBarStyleBlack;

//add button
UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:nil action:@selector(dismissCustom:)];
toolbar.items = [NSArray arrayWithObjects: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];

//add popup view
[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];}];

有谁知道出了什么问题?

谢谢!

4

1 回答 1

0

在动画之前将 NewView 添加到 self.view

CGFloat height = 255.0;

// create new view
self.NewView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.view.bounds), 320, height)];

// init and add toolbar, button and pickerView as in your code
// ...

// add popup view
[self.view addSubview:self.NewView];
CGRect newViewFrame = self.NewView.frame;
newViewFrame.origin.y -= newViewFrame.size.height;
[UIView animateWithDuration:0.5 animations:^{
    self.NewView.frame = newViewFrame;
} completion:nil];
于 2013-08-10T14:51:22.570 回答