更改UIScrollview的contentInset会自动为contentOffset设置延迟动画

时间:2018-03-13 16:37:26

标签: ios swift uiscrollview

这是我想要实现的目标:

我有一种形式,在UISCrollView中封装了多个textField。开始编辑字段时,目标是将其设置在键盘上方。

为了实现这一目标:

  • 我正在将scrollView的底部contentInset更改为键盘的高度,以便允许所有内容在其上滚动
  • 计算屏幕中textField的绝对帧数并使用它来计算其在键盘上移动的方式( diff
  • 使用差异
  • 动画化contentOffset

问题是: 设置contentInset会触发带有延迟的contentOffset自动调整。

问题是: 如何禁用此自动行为

    internal func keyboardWillShow(notification: Notification)
    {
        let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
        let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
        let targetFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue


        self.scrollView.contentInset = UIEdgeInsetsMake(self.scrollView.contentInset.top,
                                                        self.scrollView.contentInset.left,
                                                        targetFrame.size.height,
                                                        self.scrollView.contentInset.right)


        let apDelegate = UIApplication.shared.delegate as! AppDelegate
        let absRect = self.currentSelectedField!.superview!.convert(self.currentSelectedField!.frame, to: apDelegate.window)
        if absRect.origin.y + absRect.size.height > targetFrame.origin.y
        {
            let diff = (absRect.origin.y + absRect.size.height) - targetFrame.origin.y

            UIView.animate(withDuration: duration, delay: 0, options: UIViewAnimationOptions(rawValue: curve), animations: {

                self.scrollView.contentOffset = CGPoint(x: self.scrollView.contentOffset.x,
                                                        y: self.scrollView.contentOffset.y + diff)
            }, completion: { (done: Bool) in

            })
        }
    }

0 个答案:

没有答案