scrollViewDidEndDecelerating not executing

拜拜、爱过 提交于 2019-12-03 06:50:55

This would do:

Header File:

@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;

Implementation File:

@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";
}

scrollViewDidEndDecelerating is not called if the user is scrolling slowly (i.e. the scroll view does not continue to scroll on touch up). In that case the delegate calls scrollViewDidEndDragging. So to do something when the user has stopped scrolling and the scrollview has stopped you can combine them:

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

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

-(void)endOfScroll{
  //Do something
}

And in Swift

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

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

func endOfScroll() {
  //Do something
}

Either connect the delegate property of the scrollview to the File's Owner object in Interface Builder or just set the delegate manually in your ViewController's ViewDidLoad.

scrollview.delegate = self
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!