scrollViewDidEndDecelerating不执行(scrollViewDidEndDe

2019-07-29 20:47发布

我似乎无法得到scrollViewDidEndDecelerating调用。 我有2次内滚动视图。 现在我需要它的时候了滚动完成滚动第二种观点的值在第一个视图设置为一个标签。

头文件:

@interface ViewController: UIViewController
{
   UIScrollView *scrollView;
   UIView *view1;
   UIView *view2;
}

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIView *view1;
@property (strong, nonatomic) IBOutlet UIView *view2;
@property (weak, nonatomic) IBOutlet UILabel *lbl;

实现文件:

@synthesize scrollView, view1, view2;

-(void)viewDidLoad
{
   self.view1=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
   self.view2=[[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 480)];

   [self.scrollView addSubView:self.view1];
   [self.scrollView addSubView:self.view2];

   self.scrollView.bounces=NO;
   self.scrollView.contentSize=CGSizeMake(640,460);
   [self.scrollView setShowHorizontalScrollIndicator:NO];
   [self.scrollView scrollRectToVisible:CGRectMake(0, 0, 320, 416) animated:NO];
}

-(void)scrollViewDidEndDecelerating:(UIView *)scrollView
{
   lbl.text=@"0";
}

我看不出有什么不对,应该工作。 有人可以帮我吗? 将不胜感激。

Answer 1:

这将做到:

头文件:

@interface ViewController: UIViewController <UIScrollViewDelegate> //promise that you'll act as scrollView's delegate
{
   UIScrollView *scrollView;
   UIView *view1;
   UIView *view2;
}

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIView *view1;
@property (strong, nonatomic) IBOutlet UIView *view2;
@property (weak, nonatomic) IBOutlet UILabel *lbl;

实现文件:

@synthesize scrollView, view1, view2;

-(void)viewDidLoad
{
   self.view1=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
   self.view2=[[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 480)];

   [self.scrollView addSubView:self.view1];
   [self.scrollView addSubView:self.view2];

   self.scrollView.bounces=NO;
   self.scrollView.contentSize=CGSizeMake(640,460);
   [self.scrollView setShowHorizontalScrollIndicator:NO];
   [self.scrollView scrollRectToVisible:CGRectMake(0, 0, 320, 416) animated:NO];
   [self.scrollView setDelegate:self];//Set delegate
}

-(void)scrollViewDidEndDecelerating:(UIView *)scrollView
{
   lbl.text=@"0";
}


Answer 2:

scrollViewDidEndDecelerating不叫,如果用户正在慢慢滚动(即滚动视图不继续滚动触摸起来)。 在这种情况下,委托调用scrollViewDidEndDragging。 因此,当用户停止滚动,滚动视图已停止,你可以将它们结合起来做一些事情:

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
   if(!decelerate) [self endOfScroll];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  [self endOfScroll];
}

-(void)endOfScroll{
  //Do something
}

而在斯威夫特

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
  if !decelerate {
     endOfScroll()
  }
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  endOfScroll()
}

func endOfScroll() {
  //Do something
}


Answer 3:

无论是滚动型的委托属性连接到在界面生成器文件的所有者对象或只是手动设置委托在视图控制器的viewDidLoad。

scrollview.delegate = self


文章来源: scrollViewDidEndDecelerating not executing