UIRefreshControl需要被拉得太远 [英] UIRefreshControl needs to be pulled down too far

查看:108
本文介绍了UIRefreshControl需要被拉得太远的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 UICollectionView 上的项目中实现 UIRefreshControl 。它按预期工作,但我需要拉 UICollectionView 大约3倍于 refreshControl 的高度它正在旋转(一旦用户放手)。我已经看过其他应用程序,你只需要在它旋转时下拉到 refreshControl 的高度。如何更改需要下拉的金额?

I'm trying to implement a UIRefreshControl in my project on a UICollectionView. It works as intended, but I need to pull the the UICollectionView about 3x as much as the height of the refreshControl when it's spinning (once the user has let go). I've seen other apps where you only need to pull down to the height of the refreshControl when it's spinning. How do I change the amount I need to pull down?

代码:

(void)viewDidLoad {
  [super viewDidLoad];

  [self.collectionView setBackgroundColor:[UIColor clearColor]];
  self.refreshControl = [UIRefreshControl new];
  [self.refreshControl addTarget:self action:@selector(refreshFavorites) forControlEvents:UIControlEventValueChanged];
  [self.collectionView addSubview:self.refreshControl];
  self.collectionView.alwaysBounceVertical = YES;
} 

附上图片以帮助想象我的意思。我的 collectionView 有一行(纯红色)。红色和 refreshControl 之间的空格只是我试图激活 refreshControl 的空间。

Attached are pictures to help visualize what I mean. My collectionView has one row (solid red). The space between the red and the refreshControl is simply the space I'm pulling down to try to activate the refreshControl.

在这张图片中,我几乎已经激活了控件,我已经在屏幕上拉了相当数量。

In this image I've almost activated the control and I've already pulled down a considerable amount on the screen.

In这个图像我已经激活了控件并且它开始加载(在它跳转~20-30像素之后,但它仍然突出显示这是多么重要)

In this image I've activated the control and it's starting to load (after it jumps ~20-30 pix, but it still highlights how significant this is)

此图像显示其中的控件休息状态在被激活后正在进行动画制作。

This image shows the control in its "resting" state after it's been activated and is doing the animation.

我已尝试设置 UIRefreshControl 的框架以尝试控制高度, 但这不起作用。我也在 collectionView:layout:sizeForItemAtIndexPath 中改变了大小,但没有帮助。

I've tried setting the frame of the UIRefreshControl to try to control the height, but this doesn't work. I've also changed around the size in collectionView:layout:sizeForItemAtIndexPath which hasn't helped.

推荐答案

如果需要将UIRefreshControl拉得太远而无法触发,则可能是iOS SDK的错误。

If a UIRefreshControl needs to be pulled down too far to trigger, it might be a bug of iOS SDK.

我建议解决这种情况使用视图控制器的帮助:

I recommend solve this situation with a view controller’s help:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // Force update the snapping height of refresh control.
    [self.refreshControl didMoveToSuperview];
}

此外,如果您不介意使用私有API并需要更准确的控制,你可以这样写:

Also if you don’t mind use private API and needs more accurate control, you can write like this:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    @try {
        [self.refreshControl setValue:@(60) forKey:@"_snappingHeight"];
    }
    @catch (NSException *exception) {
    }
}

这适用于iOS 7-10。没有App Store评论问题。

This works on iOS 7-10. No App Store review issue.

查看内部

UIRefreshControl有一个名为snappingHeight的属性。此属性控制用户需要拉下的距离。但不幸的是,这个值总是没有正确设置,尤其是你有一个自定义视图层次结构。

UIRefreshControl has a property called snappingHeight. This property controls the distance user needs pull down. But unfortunately, this value not set properly always, especially you have a custom view hierarchy.

UIRefreshControl有一个私有方法调用 _updateSnappingHeight ,此方法也在 didMoveToSuperview 中调用。所以手动 didMoveToSuperview 调用可以更新错误的捕捉值。

UIRefreshControl have a private method calls _updateSnappingHeight, this method also called in didMoveToSuperview. So an manual didMoveToSuperview call can update the wrong snapping value.

这篇关于UIRefreshControl需要被拉得太远的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆