我的应用程序中已经有大约5 UIScrollView
个,它们都加载了多个.xib
个文件。我们现在想要使用UIRefreshControl
。它们构建为与UITableViewControllers一起使用(根据UIRefreshControl类引用)。我不想重新做所有5 UIScrollView
的工作。我已经尝试在我的UIRefreshControl
中使用UIScrollView
,除了一些内容之外,它可以按预期工作。
在刷新图像变成加载程序之后,UIScrollView
会向下跳跃大约10个像素,这种情况只有在我非常小心地将UIScrollview
非常缓慢地向下拖动时才会发生。
当我向下滚动并开始重新加载时,放开UIScrollView
,UIScrollView
停留在我放手的地方。重新加载完成后,UIScrollView
跳到顶部,没有动画。
这是我的代码:
-(void)viewDidLoad
{
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[myScrollView addSubview:refreshControl];
}
-(void)handleRefresh:(UIRefreshControl *)refresh {
// Reload my data
[refresh endRefreshing];
}
有什么方法可以节省大量时间并在UIRefreshControl
中使用UIScrollView
吗?
谢谢!!!
答案 0 :(得分:92)
我有UIRefreshControl
使用UIScrollView
:
- (void)viewDidLoad
{
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
scrollView.userInteractionEnabled = TRUE;
scrollView.scrollEnabled = TRUE;
scrollView.backgroundColor = [UIColor whiteColor];
scrollView.contentSize = CGSizeMake(500, 1000);
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(testRefresh:) forControlEvents:UIControlEventValueChanged];
[scrollView addSubview:refreshControl];
[self.view addSubview:scrollView];
}
- (void)testRefresh:(UIRefreshControl *)refreshControl
{
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refreshing data..."];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[NSThread sleepForTimeInterval:3];//for 3 seconds, prevent scrollview from bouncing back down (which would cover up the refresh view immediately and stop the user from even seeing the refresh text / animation)
dispatch_async(dispatch_get_main_queue(), ^{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMM d, h:mm a"];
NSString *lastUpdate = [NSString stringWithFormat:@"Last updated on %@", [formatter stringFromDate:[NSDate date]]];
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdate];
[refreshControl endRefreshing];
NSLog(@"refresh end");
});
});
}
需要在单独的线程上进行数据更新,否则它将锁定主线程(UI用于更新UI)。因此,当主线程忙于更新数据时,UI也会被锁定或冻结,您永远不会看到平滑的动画或微调器。
编辑:好的,我正在做与OP相同的事情,我现在已经添加了一些文本(即“拉到刷新”),它确实需要回到主线程来更新该文本更新了答案。
答案 1 :(得分:39)
添加上述答案,在某些情况下,您无法设置contentSize(可能使用自动布局?)或者contentSize的高度小于或等于UIScrollView的高度。在这些情况下,UIRefreshControl将无法工作,因为UIScrollView不会反弹。
要解决此问题,请将属性 alwaysBounceVertical 设置为 TRUE 。
答案 2 :(得分:14)
如果您有幸支持iOS 10+,您现在只需设置refreshControl
的{{1}}即可。这与UIScrollView
上以前存在的refreshControl
的工作方式相同。
答案 3 :(得分:12)
自 iOS 10 起,UIScrollView已具有refreshControl属性。当您创建UIRefereshControl并将其分配给该属性时,将显示此refreshControl。
无需将UIRefereshControl添加为子视图。
url.substring(url.indexOf("oauth_verifier=") + 15 , url.length());
UIRefreshControl对象是附加到任何UIScrollView对象的标准控件
来自https://developer.apple.com/documentation/uikit/uirefreshcontrol的代码和报价
答案 4 :(得分:10)
以下是在C#/ Monotouch中执行此操作的方法。我无法在任何地方找到C#的任何样本,所以这里是..谢谢Log139!
public override void ViewDidLoad ()
{
//Create a scrollview object
UIScrollView MainScrollView = new UIScrollView(new RectangleF (0, 0, 500, 600));
//set the content size bigger so that it will bounce
MainScrollView.ContentSize = new SizeF(500,650);
// initialise and set the refresh class variable
refresh = new UIRefreshControl();
refresh.AddTarget(RefreshEventHandler,UIControlEvent.ValueChanged);
MainScrollView.AddSubview (refresh);
}
private void RefreshEventHandler (object obj, EventArgs args)
{
System.Threading.ThreadPool.QueueUserWorkItem ((callback) => {
InvokeOnMainThread (delegate() {
System.Threading.Thread.Sleep (3000);
refresh.EndRefreshing ();
});
});
}
答案 5 :(得分:2)
对于跳跃问题,Tim Norman's answer解决了这个问题。
如果你使用swift2,这是swift版本:
import UIKit
class NoJumpRefreshScrollView: UIScrollView {
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
override var contentInset:UIEdgeInsets {
willSet {
if self.tracking {
let diff = newValue.top - self.contentInset.top;
var translation = self.panGestureRecognizer.translationInView(self)
translation.y -= diff * 3.0 / 2.0
self.panGestureRecognizer.setTranslation(translation, inView: self)
}
}
}
}
答案 6 :(得分:2)
如何在Swift 3中执行此操作:
override func viewDidLoad() {
super.viewDidLoad()
let scroll = UIScrollView()
scroll.isScrollEnabled = true
view.addSubview(scroll)
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(pullToRefresh(_:)), for: .valueChanged)
scroll.addSubview(refreshControl)
}
func pullToRefresh(_ refreshControl: UIRefreshControl) {
// Update your conntent here
refreshControl.endRefreshing()
}
答案 7 :(得分:1)
我在UIRefreshControl
内使UIScrollView
正常工作。我继承UIScrollView
,阻止更改contentInset并覆盖contentOffset setter:
class ScrollViewForRefreshControl : UIScrollView {
override var contentOffset : CGPoint {
get {return super.contentOffset }
set {
if newValue.y < -_contentInset.top || _contentInset.top == 0 {
super.contentOffset = newValue
}
}
}
private var _contentInset = UIEdgeInsetsZero
override var contentInset : UIEdgeInsets {
get { return _contentInset}
set {
_contentInset = newValue
if newValue.top == 0 && contentOffset.y < 0 {
self.setContentOffset(CGPointZero, animated: true)
}
}
}
}
答案 8 :(得分:0)
对于“跳跃”问题,重写contentInset仅在iOS 9之前解决。 我只是尝试了一种避免跳跃问题的方法:
let scrollView = UIScrollView()
let refresh = UIRefreshControl()
// scrollView.refreshControl = UIRefreshControl() this will cause the jump issue
refresh.addTarget(self, action: #selector(handleRefreshControl), for: .valueChanged)
scrollView.alwaysBounceVertical = true
//just add refreshControl and send it to back will avoid jump issue
scrollView.addSubview(refresh)
scrollView.sendSubviewToBack(refresh)
可在iOS 9 10 11上运行,依此类推,我希望他们(苹果公司)能解决此问题。
答案 9 :(得分:0)
从iOS 10开始,UIScrollView具有refreshControl属性,您可以将其设置为UIRefreshControl。与往常一样,您不需要管理控件的框架。只需配置控件,为valueChanged事件设置目标动作,然后分配给属性即可。
无论使用的是普通滚动视图,表视图还是集合视图,创建刷新控件的步骤都是相同的。
if #available(iOS 10.0, *) {
let refreshControl = UIRefreshControl()
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
refreshControl.addTarget(self,
action: #selector(refreshOptions(sender:)),
for: .valueChanged)
scrollView.refreshControl = refreshControl
}
@objc private func refreshOptions(sender: UIRefreshControl) {
// Perform actions to refresh the content
// ...
// and then dismiss the control
sender.endRefreshing()
}
答案 10 :(得分:-3)
您可以简单地创建刷新控件的实例,并将其添加到滚动视图的顶部。然后,在委托方法中,根据您的要求调整其行为。