Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

ios - UIRefreshControl needs to be pulled down too far

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?

Code:

(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;
} 

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.

enter image description here

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)

enter image description here

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

enter image description here

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];
}

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) {
    }
}

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


Look inside

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 have a private method calls _updateSnappingHeight, this method also called in didMoveToSuperview. So an manual didMoveToSuperview call can update the wrong snapping value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...