我正在开发一个应用程序,允许用户从UITableView中 Select 日期.tableView是静态和分组的.我已经研究了很多问题,包括this one个问题,试图找出如何实现这一点——但似乎没有什么能达到最佳效果.苹果的日历应用程序有一个非常流畅和漂亮的动画,我所经历的所有例子都没有成功地重现.

这是我想要的结果:

就地日期 Select 器

有人能给我指一个教程,或者解释一下我如何用最简洁、最直接的方式完成如此流畅的动画,就像我们在日历应用程序中看到的那样?

非常感谢!

埃里克

推荐答案

我假设您使用的是故事板,示例为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];
}

Objective-c相关问答推荐

将 NSMutableAttributedString 转换为 NSString

viewWillAppear,viewDidAppear 没有被调用,没有触发

如何追踪 SIGABRT 的原因

在 Objective C 中动态调用类方法

为什么在目标 c 的静态上下文中允许 self

UIRefreshControl iOS 6 xcode

在 UITableViewCells 上显示复制弹出窗口的简单方法,如地址簿应用程序

如何使用键值编码判断对象是否存在键?

#if 和 #ifdef Objective-C 预处理器宏有什么区别?

碰撞技术如何运作?

重复符号 _OBJC_METACLASS_$_ClassName

主队列上的 dispatch_sync 在单元测试中挂起

当服务器上的图像文件更改时,我的应用程序中的 SDWebImage 缓存图像会发生什么情况?

ios 11 UITabBar UITabBarItem 定位问题

自定义 colored颜色 我的 UIActivityIndi​​catorView

检测 UITableView 滚动

iOS 7 中的 UITextView 链接检测

通过点击状态栏滚动到 UITableView 的顶部

在 iPhone 上查找用户的 Documents 目录的最佳方法是什么?

@synthesized 保留属性的释放是如何处理的?