xcode scrollViewDidEndDecelerating 未执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10982568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
scrollViewDidEndDecelerating not executing
提问by iamruskie
I can't seem to get scrollViewDidEndDecelerating called. I have a scrollView with 2 Views inside. Now I need it to set a value to a label in the first view when the scrollview is finished scrolling to the second view.
我似乎无法调用 scrollViewDidEndDecelerating。我有一个带有 2 个视图的滚动视图。现在,当滚动视图完成滚动到第二个视图时,我需要它为第一个视图中的标签设置一个值。
Header File:
头文件:
@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;
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];
}
-(void)scrollViewDidEndDecelerating:(UIView *)scrollView
{
lbl.text=@"0";
}
I don't see anything wrong, it should be working. Can someone help me out? Would appreciate it.
我没有发现任何问题,它应该可以正常工作。有人可以帮我吗?会很感激的。
采纳答案by Ahmed
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";
}
回答by Sten
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:
如果用户滚动缓慢(即滚动视图不会在触摸时继续滚动),则不会调用 scrollViewDidEndDecelerating。在这种情况下,委托调用 scrollViewDidEndDragging。因此,要在用户停止滚动且滚动视图停止时执行某些操作,您可以将它们组合起来:
- (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
}
回答by Dcritelli
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.
将滚动视图的委托属性连接到 Interface Builder 中的 File's Owner 对象,或者只是在 ViewController 的 ViewDidLoad 中手动设置委托。
scrollview.delegate = self