我正在开发一个应用程序,允许用户从UITableView中 Select 日期.tableView是静态和分组的.我已经研究了很多问题,包括this one个问题,试图找出如何实现这一点——但似乎没有什么能达到最佳效果.苹果的日历应用程序有一个非常流畅和漂亮的动画,我所经历的所有例子都没有成功地重现.
这是我想要的结果:
有人能给我指一个教程,或者解释一下我如何用最简洁、最直接的方式完成如此流畅的动画,就像我们在日历应用程序中看到的那样?
非常感谢!
埃里克
我正在开发一个应用程序,允许用户从UITableView中 Select 日期.tableView是静态和分组的.我已经研究了很多问题,包括this one个问题,试图找出如何实现这一点——但似乎没有什么能达到最佳效果.苹果的日历应用程序有一个非常流畅和漂亮的动画,我所经历的所有例子都没有成功地重现.
这是我想要的结果:
有人能给我指一个教程,或者解释一下我如何用最简洁、最直接的方式完成如此流畅的动画,就像我们在日历应用程序中看到的那样?
非常感谢!
埃里克
我假设您使用的是故事板,示例为UIPickerView
:
接下来,将UIPickerView via Outlet连接到viewcontroller,并将以下属性添加到viewcontroller.h:
@property (weak, nonatomic) IBOutlet UIPickerView *statusPicker;
@property BOOL statusPickerVisible;
在ViewController中.viewWillAppear
米
self.statusPickerVisible = NO;
self.statusPicker.hidden = YES;
self.statusPicker.translatesAutoresizingMaskIntoConstraints = NO;
添加两种方法:
- (void)showStatusPickerCell {
self.statusPickerVisible = YES;
[self.tableView beginUpdates];
[self.tableView endUpdates];
self.statusPicker.alpha = 0.0f;
[UIView animateWithDuration:0.25
animations:^{
self.statusPicker.alpha = 1.0f;
} completion:^(BOOL finished){
self.statusPicker.hidden = NO;
}];];
}
- (void)hideStatusPickerCell {
self.statusPickerVisible = NO;
[self.tableView beginUpdates];
[self.tableView endUpdates];
[UIView animateWithDuration:0.25
animations:^{
self.statusPicker.alpha = 0.0f;
}
completion:^(BOOL finished){
self.statusPicker.hidden = YES;
}];
}
heightForRowAtIndexPath
年后
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = self.tableView.rowHeight;
if (indexPath.row == 1){
height = self.statusPickerVisible ? 216.0f : 0.0f;
}
return height;
}
didSelectRowAtIndexPath
年后
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
if (self.statusPickerVisible){
[self hideStatusPickerCell];
} else {
[self showStatusPickerCell];
}
}
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}