2016-11-29 75 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) 
}