Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios ScrollViewDelegate不调用scrollViewDidScroll或ScrollViewWillBeginDraging_Ios_Swift_Uitableview_Uiscrollview_Uiscrollviewdelegate - Fatal编程技术网

Ios ScrollViewDelegate不调用scrollViewDidScroll或ScrollViewWillBeginDraging

Ios ScrollViewDelegate不调用scrollViewDidScroll或ScrollViewWillBeginDraging,ios,swift,uitableview,uiscrollview,uiscrollviewdelegate,Ios,Swift,Uitableview,Uiscrollview,Uiscrollviewdelegate,我从中获得了解决方案,回答了如何测试用户是否在向上或向下滚动,并在我的代码中实现了该解决方案。在第一个包含UITableView的viewcontroller中,此代码工作得非常好,但对于另一个包含类似UITableView的viewcontroller,此代码不起作用。我已经检查了我可能犯的任何错误,例如在不同的函数中意外引用这些错误,或者如果我在定义类时没有引用UIScrollViewDelegate,但是根据解决方案,这是不必要的,因为“UITableViewDelegate已经是UISc

我从中获得了解决方案,回答了如何测试用户是否在向上或向下滚动,并在我的代码中实现了该解决方案。在第一个包含UITableView的viewcontroller中,此代码工作得非常好,但对于另一个包含类似UITableView的viewcontroller,此代码不起作用。我已经检查了我可能犯的任何错误,例如在不同的函数中意外引用这些错误,或者如果我在定义类时没有引用UIScrollViewDelegate,但是根据解决方案,这是不必要的,因为“UITableViewDelegate已经是UIScrollViewDelegate的子类。”

以下是我应该调用的代码,但它没有调用(我计划根据用户的上下移动在viewcontroller上设置某些组件的动画):

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)
})
}否则{
}
}
到目前为止,我已经尝试打印一个文本,只是想看看它是否只是我的动画中的一个错误,但什么也没有发生。以下是关于我代码其余部分的更多背景信息:

  • 我在viewController中为另一个功能实现了两个UISweep手势和一个UITap手势

  • 在这段代码之前,我定义并设置了tableView,但这不会有什么不同

  • 我的tableView收集的信息是通过一些web请求收集的,这并不需要很长时间,但仍然需要一些时间——我已经为此编写了一个函数

  • 有时,一个blurViewController和一个ContainerViewController完全覆盖了tableView,但我已经开发了一个代码,根据请求将这些设置到后台,以不损害tableView的功能

  • 这不是UITableViewController,而是部分覆盖UIViewController的UITableView


  • 您是否将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.