f.p*_*ion 4 objective-c uitableview uiscrollview ios
我试图复制在iOS应用程序Rdio中看到的屏幕的大小调整行为.有问题的屏幕提供了所选专辑的概述,上半部分包含UIView,下半部分包含UITableView.当滚动tableView时,它首先向上调整大小以填充屏幕,然后在达到最大高度时开始正常滚动其内容.
经过一番搜索,我发现了这个问题:拖动UITableView基本上要求同样的东西,但是它接受的方法与我最初的想法和试验相同,这是使用UIPanGestureRecognizer并根据翻译的大小调整tableviews高度.泛.
这并没有提供我要找的行为.使用此方法只允许您静态地向上或向下拖动tableviews高度,并且它添加了panGesture的问题,覆盖tableViews的问题,然后阻止滚动内容.
的RDIO应用功能的调整行为,并认为正是像一个UIScrollView,它有惯性.您可以将其完全拖动,向上或向下轻拂,并平滑地调整大小.当tableView达到其全尺寸或原始半尺寸时,剩余的惯性似乎在tableview上传递,导致单元格像往常一样滚动该数量.我知道他们必须操纵UIScrollViews,我只是无法弄清楚如何.
作为最后一点,最终我将在这个屏幕上使用AutoLayout,所以我想知道这将如何可能阻碍或帮助这种情况.
更新
这种方法让我最接近我到目前为止所寻找的行为.向上轻拂tableView的行为与我想要的完全相同(调整惯性并在达到最大高度时继续滚动),尽管灵敏度低于我想要的.然而,向下轻拂,没有惯性,立即停止.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect scrollViewFrame = scrollView.frame;
CGFloat scrollViewYOffset = scrollView.contentOffset.y;
if (scrollViewFrame.origin.y <= kTableViewMaxYOrigin || scrollViewFrame.origin.y <= _originalTableViewFrame.origin.y)
{
scrollViewFrame.origin.y -= scrollViewYOffset;
if(scrollViewFrame.origin.y >= kTableViewMaxYOrigin && scrollViewFrame.origin.y <= _originalTableViewFrame.origin.y)
{
scrollViewFrame.size.height += scrollViewYOffset;
scrollView.frame = scrollViewFrame;
scrollView.contentOffset = CGPointZero;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我制作了一个使用autolayout的版本.我花了一段时间的试验和错误才能让这一次正确!
请使用评论并询问我答案是否不清楚.
在viewDidLoad保存布局约束的初始高度时,确定您希望滚动视图的最低值.
- (void)viewDidLoad
{
...
_initialTopLayoutConstraintHeight = self.topLayoutConstraint.constant;
...
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
BOOL leaveScrollAlone = self.topLayoutConstraint.constant == _initialTopLayoutConstraintHeight && scrollView.contentOffset.y <= 0;
if (leaveScrollAlone)
{
// This allows for bounce when swiping your finger downwards and reaching the stopping point
return;
}
// Do some capping of that layout constraint to keep it from going past the range you want it to be.
// In this case, I use self.topLayoutGuide.length so that my UICollectionView scales all the way until
// it hits the bottom of the navigation bar
CGFloat topLayoutConstraintLength = _initialTopLayoutConstraintHeight - scrollView.contentInset.top;
topLayoutConstraintLength = MAX(topLayoutConstraintLength, self.topLayoutGuide.length);
topLayoutConstraintLength = MIN(topLayoutConstraintLength, _initialTopLayoutConstraintHeight);
self.topLayoutConstraint.constant = topLayoutConstraintLength;
// Keep content seemingly still while the UICollectionView resizes
if (topLayoutConstraintLength > self.topLayoutGuide.length)
{
scrollView.contentInset = UIEdgeInsetsMake(scrollView.contentInset.top + scrollView.contentOffset.y,
scrollView.contentInset.left,
scrollView.contentInset.bottom,
scrollView.contentInset.right);
scrollView.contentOffset = CGPointZero;
}
// This helps get rid of the extraneous contentInset.top we accumulated for keeping
// the content static while the UICollectionView resizes
if (scrollView.contentOffset.y < 0)
{
self.topLayoutConstraint.constant -= scrollView.contentOffset.y;
scrollView.contentInset = UIEdgeInsetsMake(scrollView.contentInset.top + scrollView.contentOffset.y,
scrollView.contentInset.left,
scrollView.contentInset.bottom,
scrollView.contentInset.right);
}
// Prevents strange jittery artifacts
[self.view layoutIfNeeded];
}
Run Code Online (Sandbox Code Playgroud)