Ios ScrollViewDelegate不调用scrollViewDidScroll或ScrollViewWillBeginDraging
我从中获得了解决方案,回答了如何测试用户是否在向上或向下滚动,并在我的代码中实现了该解决方案。在第一个包含UITableView的viewcontroller中,此代码工作得非常好,但对于另一个包含类似UITableView的viewcontroller,此代码不起作用。我已经检查了我可能犯的任何错误,例如在不同的函数中意外引用这些错误,或者如果我在定义类时没有引用UIScrollViewDelegate,但是根据解决方案,这是不必要的,因为“UITableViewDelegate已经是UIScrollViewDelegate的子类。” 以下是我应该调用的代码,但它没有调用(我计划根据用户的上下移动在viewcontroller上设置某些组件的动画):Ios ScrollViewDelegate不调用scrollViewDidScroll或ScrollViewWillBeginDraging,ios,swift,uitableview,uiscrollview,uiscrollviewdelegate,Ios,Swift,Uitableview,Uiscrollview,Uiscrollviewdelegate,我从中获得了解决方案,回答了如何测试用户是否在向上或向下滚动,并在我的代码中实现了该解决方案。在第一个包含UITableView的viewcontroller中,此代码工作得非常好,但对于另一个包含类似UITableView的viewcontroller,此代码不起作用。我已经检查了我可能犯的任何错误,例如在不同的函数中意外引用这些错误,或者如果我在定义类时没有引用UIScrollViewDelegate,但是根据解决方案,这是不必要的,因为“UITableViewDelegate已经是UISc
var lastContentOffset:CGFloat=0
func scrollView将开始刷新(scrollView:UIScrollView){
self.lastContentOffset=scrollView.contentOffset.y
}
func scrollViewDidScroll(scrollView:UIScrollView){
if(self.lastContentOffsetVoid in
self.listTableView.frame=CGRect(x:0,y:36,宽度:self.view.bounds.width,高度:self.view.bounds.height-36)
})
}else if(self.lastContentOffset>scrollView.contentOffset.y){
动画(持续时间:0.2,动画:{()->Void in
self.listTableView.frame=CGRect(x:0,y:131,宽度:self.view.bounds.width,高度:self.view.bounds.height-131)
})
}否则{
}
}
到目前为止,我已经尝试打印一个文本,只是想看看它是否只是我的动画中的一个错误,但什么也没有发生。以下是关于我代码其余部分的更多背景信息:
您是否将scrollView委托设置为self
您是否将scrollView委托设置为self
非常感谢你。最终起作用的是“self.listTableView.delegete=self”。然而,奇怪的是,我并没有在我的其他代码中这样做,这也行得通。一个问题:为什么我需要这样做?在我看来,这似乎是多余的,但我似乎弄错了。@CyprianZander通过执行delegate=self,您确认了scrollView委托方法的协议。换句话说,您正在告诉您的控制器,您希望接收与此协议相关的函数的回调。非常感谢,这使此步骤更加清晰!非常感谢你。最终起作用的是“self.listTableView.delegete=self”。然而,奇怪的是,我并没有在我的其他代码中这样做,这也行得通。一个问题:为什么我需要这样做?在我看来,这似乎是多余的,但我似乎弄错了。@CyprianZander通过执行delegate=self,您确认了scrollView委托方法的协议。换句话说,您正在告诉您的控制器,您希望接收与此协议相关的函数的回调。非常感谢,这使此步骤更加清晰!
var lastContentOffset: CGFloat = 0
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
self.lastContentOffset = scrollView.contentOffset.y
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (self.lastContentOffset < scrollView.contentOffset.y) {
UIView.animate(withDuration: 0.2, animations: { () -> Void in
self.listTableView.frame = CGRect(x:0 ,y: 36,width: self.view.bounds.width,height: self.view.bounds.height - 36)
})
} else if (self.lastContentOffset > scrollView.contentOffset.y) {
UIView.animate(withDuration: 0.2, animations: { () -> Void in
self.listTableView.frame = CGRect(x:0 ,y: 131,width: self.view.bounds.width,height: self.view.bounds.height - 131)
})
} else {
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.scrollView.delegate = self
}
To use delegate method of the UIScrollView, you have to set the delegate for it
You can set the delegate method in two ways:-
1) By Storyboard - Select UIScrollView and Drag and drop to File Owner of the View Controller the delegate method will set for it.
2) By Code - In ViewDidLoad() method you can use the following code:-
- (void) viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.scrollview.delegate = self;
}
In this way your ScrollView delegate method will be called.