【问题标题】:Swift - Use button to manually scroll UITextViewSwift - 使用按钮手动滚动 UITextView
【发布时间】:2017-04-13 03:36:09
【问题描述】:

我想在UITextView中使用一个按钮向下向上滚动, 但我遇到了问题。

使用textView.setContentOffset(CGPoint(x: 0, y: MyTextView.contentOffset.y + 100), animated: true)可以让我的textview向下滚动,但是当我点击文本末尾的按钮时它仍然滚动...

像这样..

e
f
g




===================  

但我想要的是这个..

a
b
c
d
e 
f 
g
===================  

我的代码是

@IBAction func down(_ sender: UIButton) {
   MyTextView.setContentOffset(CGPoint(x: 0, y: MyTextView.contentOffset.y - 100), animated: true)
}

@IBAction func up(_ sender: UIButton) {
   MyTextView.setContentOffset(CGPoint(x: 0, y: MyTextView.contentOffset.y - 100), animated: true)
}

请帮帮我!!!

【问题讨论】:

  • 请添加按钮操作的代码sn-p。
  • setContentOffset 需要一个 cgpoint 作为参数,但你只给它一个 cgfloat 。 contentOffset.y + 100 给出 cgfloat ,而不是 cgpoint

标签: ios swift uiscrollview uibutton uitextview


【解决方案1】:

试试这个

@IBAction func down(_ sender: UIButton) {
    if (textView.contentSize.height>(textView.frame.size.height+textView.contentOffset.y+100)){

        textView.setContentOffset(CGPoint(x:0,y:textView.contentOffset.y + 100), animated: true);
    }
    else{
        textView.setContentOffset(CGPoint(x:0,y:(textView.contentSize.height - textView.frame.size.height)), animated: true)
    }
}

【讨论】:

  • 但是当我更改为 10 到 100 时仍然滚动到边界之外
  • ? 非常感谢!!
【解决方案2】:

我已经测试过了,它可以工作。看看这个

 @IBOutlet weak var textView: UITextView!

    @IBAction func up(_ sender: UIButton) {
        if textView.contentOffset.y < 100{
            textView.setContentOffset(CGPoint.zero, animated: true)
        }else{
            textView.setContentOffset(CGPoint(x: 0, y: textView.contentOffset.y-100), animated: true)
        }
    }
  
    @IBAction func down(_ sender: UIButton) {
        if textView.contentOffset.y >  textView.contentSize.width - textView.frame.size.height{
            textView.setContentOffset(CGPoint(x: 0, y: textView.contentSize.height-textView.frame.size.height), animated: true)
        }else{
            textView.setContentOffset(CGPoint(x: 0, y: textView.contentOffset.y+100), animated: true)
        }
    }

【讨论】:

    【解决方案3】:

    您可能想检查当前偏移量是多少,并且只移动您需要的量。例如向下滚动,请尝试以下操作。然后你应该能够调整它并为向上滚动做相反的事情。

    struct Constants {
        static let preferredScrollAmount: CGFloat = 100.0
    }
    
    @IBAction func downButtonTapped(_ sender: UIButton) {
        // Get the current offset, content height, text view height and work out the scrollable distance for the text view
        let currentOffset: CGFloat = textView.contentOffset.y
        let contentHeight: CGFloat = textView.contentSize.height
        let textViewHeight: CGFloat = textView.frame.size.height
        let scrollableDistance = contentHeight - textViewHeight
    
        // Check that the current offset isn't beyond the scrollable area otherwise return (no need to scroll)
        guard currentOffset < scrollableDistance else { return }
    
        // Work out how far we can move
        let distanceWeCanMove = scrollableDistance - currentOffset
    
        // Get the distance we should move (the smaller value so it doesn't go past the end)
        let distanceToScroll = min(distanceWeCanMove, Constants.preferredScrollAmount)
    
        // Do the scrolling
        textView.setContentOffset(CGPoint(x: 0, y: currentOffset + distanceToScroll), animated: true)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 2021-01-01
      • 1970-01-01
      相关资源
      最近更新 更多