2016-11-29 77 views
0

我想用一个按钮UITextView, 滚动下来,但我遇到了一个问题。斯威夫特 - 使用按钮来手动滚动的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) 
} 

请帮帮我!!!

+0

请添加按钮动作的代码片段。 –

+0

setContentOffset需要一个cgpoint作为参数,但你只是给它一个cgfloat。 contentOffset.y + 100给出cgfloat,而不是cgpoint –

回答

2

试试这个

@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) 
    } 
} 
+0

谢谢。有用!! – Daniel

+0

不客气。 –

+1

但是当我改为10到100仍然滚动出界 – Daniel

1

我已经测试和它的作品。检查了这一点

@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) 
     } 
    } 
0

你可能想检查什么是当前偏移量,只移动你需要的数量。例如,向下滚动,请尝试如下所示。那么你应该能够调整它,并做相反的滚动。

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) 
}