gpt4 book ai didi

ios - 我将如何创建一个状态消息,该消息会短暂显示 "above"UITableView(如 UIRefreshControl)?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:48:15 25 4
gpt4 key购买 nike

我正在尝试使用 UIView 来补充 UIRefreshControl,其中包含出现在 UITableViewController“上方”的消息 (statusMessageView) 应用启动后。此 View 将通知用户 UITableViewController 已经刷新,不需要刷新。

这是我要完成的任务的分割:

应用启动,UITableViewController 显示正常,然后向下滚动 44px 以显示 statusMessageView(高度为 44px)。

statusMessageView 保持可见 2 秒

UITableViewController 动画滚动到它的原始位置,有效地“隐藏”statusMessageView。 (类似于 UIRefreshControl,但使用代码进行动画处理)

注意:这个statusMessageView会和一个UIRefreshControl一起使用,所以显示后需要“消失” UIRefreshControl可以正常使用

我看过其他第 3 方的“拉动刷新” Controller ,但我认为这太过分了,因为我正在使用 UIRefreshControl

最佳答案

似乎这里的其他答案正在为下拉到 UINavigationBar 下方的通知提供解决方案。如果您仍在寻找位于 UITableView 的 ScrollView 中的解决方案,那么我会向表格 View 添加自定义表格标题(而不是节标题)。

以下是完成此操作所需的粗略步骤:

<强>1。在加载时创建初始标题 View

我通常使用拥有 UITableView 实例变量的 UIViewController 子类(而不是使用 UITableViewController),但您应该能够通过任一设置完成此操作。在您的 tableview 中设置代码(可能在 viewDidLoad 中),您可以在其中设置 backgroundColor、contentInset、separatorStyle 等内容,创建一个 UILabel 将成为您的标题。然后将此 UILabel 设置为您的 tableView 的 tableHeaderView。当然,如果你想为这个“通知部分”做一些更复杂的事情,请随意将它做成一个带有嵌套 UILabel + 其他东西的 UIView。所以像这样:

UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 44.0f)];
headerLabel.backgroundColor = [UIColor clearColor]; // Just in case you have some fancy background/color in your table view
headerLabel.textAlignment = UITextAlignmentCenter;
headerLabel.font = ....
headerLabel.textColor = ....
headerLabel.text = @"You are an awesome user!";
self.tableView.tableHeaderView = headerLabel;

<强>2。将您的表格 View 设置为“正常”加载(即不显示标题)

同样,在 viewDidLoad 中,您需要正确设置 tableview 的 contentOffset 和 alwaysBounceVertical 以在加载时隐藏此标题 View 。设置为标题高度的 contentOffset 将在标题正下方开始 tableview 的 y 坐标。 alwaysBounceVertical 设置为 YES 将允许您的 tableview 正确运行,即使您的 tableview 的内容大小小于您的屏幕尺寸。所以像这样:

self.tableView.contentOffset = (CGPoint){0.0f, 44.0f};
self.tableView.alwaysBounceVertical = YES;

<强>3。添加滑下滑走

好的,现在有几种方法可以做到这一点。在 viewDidAppear 上,您可以创建一个链式 UIView 动画,其中第一个动画向下滑动 tableview(即将 contentOffset 设置为 {0.0f, 0.0f})延迟一秒,第二个动画将 tableview 向上滑动(即设置contentOffset to {0.0f, 44.0f}) 延迟两秒。或者您可以使用 GCD 并将两个动画安排为异步 + 延迟 block 。任何一种方法都可以(并且可能有两三种其他好的方法可以实现此目的),但只是为了让想法得到理解...您可以像这样链接动画:

__weak MyCustomViewController *me = self;
[UIView
animateWithDuration:0.4f
delay:1.0f
options:UIViewAnimationOptionAllowUserInteraction
animations:^
{
me.tableView.contentOffset = (CGPoint){0.0f, 0.0f};
}
completion:^(BOOL finished)
{
if (me.tableView)
{
[UIView
animateWithDuration:0.4f
delay:2.0f
options:UIViewAnimationOptionAllowUserInteraction
animations:^
{
me.tableView.contentOffset = (CGPoint){0.0f, 44.0f};
}
completion:^(BOOL finished)
{
if (me.tableView)
{
me.tableView.tableHeaderView = nil; // If you want to completely get rid of this notification header
me.tableView.contentOffset = (CGPoint){0.0f, 0.0f}; // I'm unsure if this will cause the tableview to jump... if it does, you can animate the headerview away instead of animating the tableview up to hide the header, then setting header to nil and reseting the contentOffset

// or

me.tableView.tableHeaderView.hidden = YES; // If you want to just hide the header
}
}];
}
}];

我编写了示例代码,但没有对其进行任何测试...呵呵,所以它可能无法正常工作。但如果您需要更多帮助,我们很乐意帮助您充实这一点!祝你好运。

更新:允许用户滚动取消动画

我不太确定您希望桌面上的用户交互对动画执行什么操作。如果您只想在用户开始滚动时取消动画,那么我会使用 GCD(请参见下面的代码)。但我可以看到其他方法可以让动画与用户触摸一起工作,所以这取决于你在寻找什么。在任何情况下,假设任何用户触摸都应禁用下一个预定动画,然后可以使用以下两个函数来实现:

- (void)scheduleShowHeaderAnimation
{
__weak MyCustomViewController *me = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0f * NSEC_PER_SEC), dispatch_get_main_queue(), ^ // Start animating after 1 sec delay
{
if (me.tableView) // Make sure the tableview is still around
{
if (! me.tableView.tracking && // Don't show animation if user has begun to touch contentview
! me.tableView.dragging && // Don't show animation if user has begun to drag contentview
! me.tableView.decelerating) // Don't show animation if dragging happened and scrollview is starting to decelerate
{
[UIView
animateWithDuration:0.4f
delay:0.0f
options:UIViewAnimationOptionAllowAnimatedContent // This will make sure user can interact with tableview while animation is going on
animations:^
{
me.tableView.contentOffset = (CGPoint){0.0f, 0.0f};
}
completion:^(BOOL finished)
{
[me scheduleHideHeaderAnimation];
}];
}
}
});
}

- (void)scheduleHideHeaderAnimation
{
__weak MyCustomViewController *me = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0f * NSEC_PER_SEC), dispatch_get_main_queue(), ^ // Start animating after 2 secs delay
{
if (me.tableView) // Make sure the tableview is still around
{
if (! me.tableView.tracking && // Don't show animation if user has begun to touch contentview
! me.tableView.dragging && // Don't show animation if user has begun to drag contentview
! me.tableView.decelerating) // Don't show animation if dragging happened and scrollview is starting to decelerate
{
[UIView
animateWithDuration:0.4f
delay:0.0f
options:UIViewAnimationOptionAllowAnimatedContent // This will make sure user can interact with tableview while animation is going on
animations:^
{
me.tableView.contentOffset = (CGPoint){0.0f, 44.0f};
}
completion:^(BOOL finished)
{
if (me.tableView)
{
me.tableView.tableHeaderView = nil; // If you want to completely get rid of this notification header
me.tableView.contentOffset = (CGPoint){0.0f, 0.0f}; // I'm unsure if this will cause the tableview to jump... if it does, you can animate the headerview away instead of animating the tableview up to hide the header, then setting header to nil and reseting the contentOffset

// or

me.tableView.tableHeaderView.hidden = YES; // If you want to just hide the header
}
}];
}
}
});
}

我会在 viewDidAppear 中调用 scheduleShowHeaderAnimation。

然后为了在用户已经向下滚动表格 View 时支持隐藏标题,我将实现 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate- UIScrollViewDelegate 的 (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 并添加如下内容:

if (self.tableView.tableHeaderView != nil)
{
self.tableView.tableHeaderView = nil;
}

// or

if (self.tableView.tableHeaderView.hidden == NO)
{
self.tableView.tableHeaderView.hidden = YES;
}

如果你想支持更复杂的交互,或者如果你想让动画以不同的方式响应用户触摸,你可能需要覆盖其他 UIScrollViewDelegate 方法,当用户开始与 ScrollView 交互时(这是 TableView 的父类),然后更改动画的行为。

这是否让您更接近您正在寻找的东西?

关于ios - 我将如何创建一个状态消息,该消息会短暂显示 "above"UITableView(如 UIRefreshControl)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16773215/

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