UITapGesture使全屏视图不反转

时间:2018-09-20 04:43:59

标签: swift

我有一个textView,我想轻按一下以使其全屏显示,然后再次轻按即可恢复到其原始大小。我可以将其全屏显示,但不能恢复到原始尺寸。

这就是我所拥有的-您能帮助我了解为什么这不起作用吗?

let viewWidth = view.frame.width
let viewHeight = view.frame.height
let textViewWidth = textView.frame.width
let textViewHeight = textView.frame.height
if sender.state == .ended {
    let animator = UIViewPropertyAnimator(duration: 0.5, curve: .easeInOut, animations: {
        sender.view!.frame = CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight)
    })
    animator.startAnimation()
} else if sender.state == .began {
    let animator = UIViewPropertyAnimator(duration: 0.5, curve: .easeInOut, animations: {
    sender.view!.frame = CGRect(x: 0, y: 0, width: textViewWidth, height: textViewHeight)
    })
        animator.startAnimation()
}

根据建议进行了更新:

    var textViewHeight = Int()
    var textViewWidth = Int()

//Mark: - Get initial value of textViewWidth and Height
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let widthOfTextView = textView.frame.width
        textViewWidth = Int(widthOfTextView) 
        let heightOfTextView = textView.frame.height
        textViewHeight = Int(heightOfTextView) 
    }

//Mark: - UITapGestureRecognized for hiding Toolbar, Status&Navigation Bar, slider
    @objc func textViewTapped(_ sender: UITapGestureRecognizer) {
        toolbar.isHidden = toolbar.isHidden ? false : true
        textSlider.isHidden = textSlider.isHidden ? false : true
        navigationController!.setNavigationBarHidden(!isHidden, animated: false)
        isHidden = navigationController!.isNavigationBarHidden
        self.setNeedsStatusBarAppearanceUpdate()

        let viewWidth = view.frame.width
    let viewHeight = view.frame.height
    //let textViewWidth = textView.frame.width
    //let textViewHeight = textView.frame.height
    if textView.frame.width == view.frame.width {
        // text view is full size, animate back to normal size
        let animator = UIViewPropertyAnimator(duration: 0.5, curve: .easeInOut, animations: {
            sender.view!.frame = CGRect(x: 0, y: 0, width: self.textViewWidth, height: self.textViewHeight)
        })
        animator.startAnimation()
    } else {
        // text view is normal size, animate to full size
        let animator = UIViewPropertyAnimator(duration: 0.5, curve: .easeInOut, animations: {
            sender.view!.frame = CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight)
        })
        animator.startAnimation()
    }
}

这是正在发生的事情的屏幕记录。在将textView恢复为原始大小后,其最终位置高于其原始位置-https://imgur.com/a/7Uvylih

0 个答案:

没有答案