UIRefreshcontrol在拉下并保持时会紧张不安 [英] UIRefreshcontrol jitters when pulled down and held

查看:188
本文介绍了UIRefreshcontrol在拉下并保持时会紧张不安的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在tableviewcontroller中创建了一个UIRefreshcontrol,如下所示:在viewdidload方法中:

I have created a UIRefreshcontrol in my tableviewcontroller as follows in the viewdidload method :

    refresh = [UIRefreshControl.alloc init];
    refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
    [refresh addTarget:self action:@selector(refreshChatsTableView) forControlEvents:UIControlEventValueChanged];
    self.refreshControl = refresh;

问题是,当我把它拉下来一点长时间,桌子会晃动,给人一种非常不愉快的感觉UI体验。有人可以帮忙吗?仅在横向模式下才会遇到此问题。

the problem is that when I pull it down a little long , the table jitters up giving a pretty unpleasant UI experience. Can anyone please help ? This behavior is experienced only in the Landscape mode.

这是我的代码:

-UIRefreshControl  *refresh;

    -(void)viewDidLoad{
        [super viewDidLoad];
         arr= [[NSArray    alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"A",@"B",@"C",@"D",@"E", nil];

        //refresh control
        refresh = [UIRefreshControl.alloc init];
        refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to   Refresh"];
        [refresh addTarget:self action:@selector(refreshChatsTableView)  forControlEvents:UIControlEventValueChanged];
        self.refreshControl = refresh;
      }


    -(void)refreshChatsTableView{
        [refresh endRefreshing];
    }

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;
    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
       return arr.count;
    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell =[[UITableViewCell alloc]init];
        cell.textLabel.text = [arr objectAtIndex:indexPath.row ];

        return cell;
    }


推荐答案

所以我刚遇到这个同样的问题,我想出了一个解决方案。基本上,如果你将tableView向下拉得足够远并保持它,那么ValueChanged控件事件将在用户释放触摸之前触发。如果您刷新数据并在此时重新加载tableView,表格将会跳起来。

So I just came across this same problem, and I came up with a solution. Basically, if you pull your tableView down far enough and hold it, the ValueChanged control event will fire before the user releases the touch. If you refresh your data and reload the tableView at this point, the table will jump up.

为了解决这个问题,我最终独立于ValueChanged事件刷新了表格。相反,我使用该事件以及scrollView委托方法仅在用户放手后刷新表。

To combat this, I ended up refreshing the table independently of the ValueChanged event. Instead, I used that event, along with the scrollView delegate methods, to only refresh the table after the user had let go.

首先,在viewDidLoad中设置refreshControl 。

So first, set up your refreshControl in viewDidLoad.

override func viewDidLoad()
{
    super.viewDidLoad()

    refreshControl = UIRefreshControl()
    refreshControl?.addTarget(self, action: "refreshControlChanged", forControlEvents: UIControlEvents.ValueChanged)
}

ValueChanged方法很简单。如果我们已经停止拖动,这是在快速向下滑动后发生的,只需使用您自己的自定义方法刷新表。

The ValueChanged method is simple. If we've already stopped dragging, which happens after a quick swipe down, just refresh the table using your own custom method.

internal func refreshControlChanged()
{
    if !tableView.dragging
    {
        refresh()
    }
}

然后,您需要实现didEndDragging。如果你已经拉得足够远,那么refreshControl将会刷新,所以调用刷新。否则,您要么没有拉得足够远,要么拖动到表的另一部分,要么拖得太快以至于还没有触发refreshControl ValueChanged事件,它会在那里刷新表。

Then, you need to implement didEndDragging. If you've pulled down far enough, the refreshControl will be refreshing, so call refresh. Otherwise, you either haven't pulled down far enough, you were dragging on a different part of the table, or you dragged so quickly that the refreshControl ValueChanged event has yet to fire, and it will refresh the table there.

override func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool)
{
    if refreshControl?.refreshing == true
    {
        refresh()
    }
}

就是这样。在您停止拖动之前,表格不会重新加载,这可以避免您注意到的抖动跳跃。我知道这已经有一年了,但我希望这可以帮助其他人在Google上遇到这个问题。

And that's it. The table won't get reloaded until after you've stopped dragging it, which avoids the jittery jump that you noticed. I know this is a year old, but I hope this helps anyone else coming across this on Google.

这篇关于UIRefreshcontrol在拉下并保持时会紧张不安的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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