我实现了自定义刷新控件(我自己的类,而不是子类),并且由于某些原因,因为移动到iOS 8,设置滚动视图的contentInset(特别是UICollectionView)以启动刷新动画会导致奇怪的跳转/口吃。这是我的代码:
- (void)containingScrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat scrollPosition = scrollView.contentOffset.y + scrollView.contentInset.top;
if( scrollPosition > 0 || self.isRefreshing )
{
return;
}
CGFloat percentWidth = fabs( scrollPosition ) / self.frame.size.height / 2;
CGRect maskFrame = self.maskLayer.frame;
maskFrame.size.width = self.imageLayer.frame.size.width * percentWidth;
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
self.maskLayer.frame = maskFrame;
[CATransaction commit];
}
- (void)containingScrollViewDidEndDragging:(UIScrollView *)scrollView
{
if( ( self.maskLayer.frame.size.width >= self.imageLayer.frame.size.width ) && !self.isRefreshing )
{
self.isRefreshing = YES;
[self setLoadingScrollViewInsets:scrollView];
[self startAnimation];
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
}
- (void)setLoadingScrollViewInsets:(UIScrollView *)scrollView
{
UIEdgeInsets loadingInset = scrollView.contentInset;
loadingInset.top += self.frame.size.height;
UIViewAnimationOptions options = UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState;
[UIView animateWithDuration:0.2 delay:0 options:options animations:^
{
scrollView.contentInset = loadingInset;
}
completion:nil];
}
基本上,一旦用户发布刷新,我就会将contentInset设置为刷新控件的高度。我认为动画会减少口吃/跳跃,这是在iOS 7中做的。但是在iOS 8中,当scrollView从拖动中释放出来,而不是仅仅动画到contentInset时,滚动视图内容从发布点真实地跳下来快速,然后顺利动画。我不确定这是iOS 8中的错误还是什么。我也尝试过添加:
scrollView.contentOffset = CGPointZero;
在动画块中,没有改变任何东西。
有没有人有任何想法?任何帮助将受到高度赞赏。谢谢!
答案 0 :(得分:32)
我用动画块将方法更改为:
- (void)setLoadingScrollViewInsets:(UIScrollView *)scrollView
{
UIEdgeInsets loadingInset = scrollView.contentInset;
loadingInset.top += self.view.frame.size.height;
CGPoint contentOffset = scrollView.contentOffset;
[UIView animateWithDuration:0.2 animations:^
{
scrollView.contentInset = loadingInset;
scrollView.contentOffset = contentOffset;
}];
}
答案 1 :(得分:5)
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
CGPoint point = scrollView.contentOffset;
static CGFloat refreshViewHeight = 200.0f;
if (scrollView.contentInset.top == refreshViewHeight) {
//openning
if (point.y>-refreshViewHeight) {
//will close
//必须套两层animation才能避免闪动!
[UIView animateWithDuration:0 animations:NULL completion:^(BOOL finished) {
[UIView animateWithDuration:0.25 animations:^{
scrollView.contentOffset = CGPointMake(0.0f, 0.0f);
scrollView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);
} completion:NULL];
}];
}
}
else{
//closing
static CGFloat openCriticalY = 40.0f;//会执行打开的临界值
if(point.y<-openCriticalY){
//will open
[UIView animateWithDuration:0 animations:NULL completion:^(BOOL finished) {
[UIView animateWithDuration:0.25 animations:^{
scrollView.contentInset = UIEdgeInsetsMake(refreshViewHeight, 0.0f, 0.0f, 0.0f);
scrollView.contentOffset = CGPointMake(0.0f, -refreshViewHeight);
} completion:NULL];
}];
}
}
}
你可以尝试这个,关键是使用两个动画。
答案 2 :(得分:3)
为了消除跳跃,ryanthon的答案将成功。在我的iOS9.0应用程序中,甚至不需要动画块来移除跳转,只需在设置contentInset后重置contentOffset即可。
如果你想在隐藏刷新视图时控制tableView的滚动速度,那么在user3044484的回答中的双动画块技巧将会起到魔力,一个动画块是不够的。
if (self.tableView.contentInset.top == 0)
{
UIEdgeInsets tableViewInset = self.tableView.contentInset;
tableViewInset.top = -1.*kTableHeaderHeight;
[UIView animateWithDuration: 0 animations: ^(void){} completion:^(BOOL finished) {
[UIView animateWithDuration: 0.5 animations:^{
//no need to set contentOffset, setting contentInset will change contentOffset accordingly.
self.tableView.contentInset = tableViewInset;
}];
}];
}
答案 3 :(得分:2)
我遇到了同样的问题,并将我的动画块移到了......
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
委托方法代替。不完美,但我还不能确定问题。看起来像在iOS8下更频繁地调用scrollView的-layoutSubviews
方法,导致跳动动画。
答案 4 :(得分:2)
两种解决方案:
答案 5 :(得分:1)
我已经解决了: -
- 将ContentInset更新为contentOffsetY时禁用Bounce。
- 跟踪contentOffset并更新到tableView ContentInset。
var contentOffsetY:CGFloat = 100
func tracScrollContent(location: UIScrollView) {
if(location.contentOffset.y == -contentOffsetY ) {
tblView.isScrollEnabled = true
tblView.bounces = false
tblView.contentInset = UIEdgeInsets(top: contentOffsetY, left: 0, bottom: 0, right: 0)
}else if(location.contentOffset.y >= 0 ){
tblView.bounces = true
tblView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
}
答案 6 :(得分:0)
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
中的动画对我来说不起作用。无论如何,滚动视图都会反弹。
根据Bounds automatically changes on UIScrollView with content insets,这对我来说似乎是一个iOS8错误......