ScrollViewDelegate 不调用 scrollViewDidScroll 或 scrollViewWillBeginDragging
ScrollViewDelegate not calling scrollViewDidScroll or scrollViewWillBeginDragging
我已经从 中获取了解决方案,回答了如何测试用户是否向上或向下滚动并已在我的代码中实现。在第一个带有 UITableView 的 viewcontroller 中,这段代码工作得很好,但对于另一个带有类似 UITableView 的代码,这不起作用。我已经检查了我可能犯的任何可能的错误,例如在不同的函数中不小心引用了这些错误,或者如果我在定义我的 class 时没有引用 UIScrollViewDelegate,但根据解决方案,这是没有必要的作为 "UITableViewDelegate is already a subclass of UIScrollViewDelegate."
这是我应该调用的代码,但它没有(我计划根据用户向上或向下移动 viewcontroller 上的某些组件设置动画):
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 {
}
}
到目前为止,我已经尝试打印文本只是为了看看它是否只是我的动画错误,但没有任何反应。以下是关于我的其余代码的更多背景信息:
我确实为 viewController
中的另一个功能实现了两个 UISwipeGesture 和一个 UITapGesture
我在这段代码之前定义并设置了 tableView,但这应该没有什么区别
我的 tableView 收集的信息是通过几个 web 请求,时间不长但还需要一些时间 - 我已经为它写了一个函数
有时,一个 blurViewController 和一个 ContainerViewController 完全覆盖了 tableView,但我已经开发了一个代码来根据要求将它们设置到后台,以免损害 tableView 的功能
这不是 UITableViewController,而是部分覆盖 UIViewController 的 UITableView
您是否将 scrollView 委托设置为 self ?
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.
我已经从
这是我应该调用的代码,但它没有(我计划根据用户向上或向下移动 viewcontroller 上的某些组件设置动画):
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 {
}
}
到目前为止,我已经尝试打印文本只是为了看看它是否只是我的动画错误,但没有任何反应。以下是关于我的其余代码的更多背景信息:
我确实为 viewController
中的另一个功能实现了两个 UISwipeGesture 和一个 UITapGesture
我在这段代码之前定义并设置了 tableView,但这应该没有什么区别
我的 tableView 收集的信息是通过几个 web 请求,时间不长但还需要一些时间 - 我已经为它写了一个函数
有时,一个 blurViewController 和一个 ContainerViewController 完全覆盖了 tableView,但我已经开发了一个代码来根据要求将它们设置到后台,以免损害 tableView 的功能
这不是 UITableViewController,而是部分覆盖 UIViewController 的 UITableView
您是否将 scrollView 委托设置为 self ?
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.