scrollViewDidEndDecelerating

scrollViewDidEndDecelerating

我似乎无法调用scrollViewDidEndDecelerating。
我有一个内部有2个 View 的scrollView。现在,当scrollview完成滚动到第二个 View 时,我需要它为第一个 View 中的标签设置一个值。

头文件:

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

我没有发现任何错误,它应该可以正常工作。有人可以帮我吗?会很感激的。

最佳答案

这样可以:

头文件:

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

关于iphone - scrollViewDidEndDecelerating不执行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10982568/

10-10 20:26