如何完全禁用UITextView的滚动? [英] How to entirely disable UITextView's scroll?

查看:162
本文介绍了如何完全禁用UITextView的滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UITextView为iPad开发一个简单的文本编辑应用程序。我总是遇到UIScrollView和UITextView的一些问题。我想我只是期望从这两个中得到太多东西。

I'm developing a simple text editing app for iPad using UITextView. I always had some problems with UIScrollView and UITextView. I think I just expected too much things from these two.

当我将myTextView.text设置为另一个NSString实例时,会自动滚动。我可以通过设置来阻止这种滚动

When I set myTextView.text to another a NSString instance, scrolling automatically occurred. I could prevent this scrolling by setting

myTextView.scrollEnabled = NO;
myTextView.text = newText;
myTextView.scrollEnabled = YES;

但是,如果我更改了myTextView的selectedRange属性,则会发生滚动。

However, if I changed the selectedRange property of myTextView, scrolling occurred.

具体来说,如果selectRange的范围发生在当前屏幕的文本中,则不会进行滚动。

例如,如果我通过点击全选属性选择所有文本,则不会发生滚动。但如果我通过将selectedRange设置为NSMakeRange(0,[myTextView.text length])来选择所有文本,则滚动到END(最后一个插入位置)。

要解决这些问题,
1)我保存了myTextView的原始内容偏移量。

To solve the problems, 1) I saved the original content offset of myTextView.

CGPoint originalOffset = myTextView.contentOffset;
// change myTextView.selectedRange here
myTextView.selectedRange = originalOffset

但是什么都没发生。

2)我使用NSTimer在几秒钟后调用了上面的代码,并正确滚动返回到原始位置(偏移)。但是,首先滚动到最后,然后到顶部..

2) I called above codes after a few seconds using NSTimer, and scroll correctly returned to the original position(offset). However, scrolling to the end first occurred and then to the top..

有没有办法完全阻止UITextView的滚动片刻?

Is there a way to entirely prevent UITextView's scrolling for a moment?

推荐答案

您可以通过将以下方法放入UITextView子类来禁用几乎所有滚动:

You can disable almost all scrolling by putting the following method into your UITextView subclass:

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated {
  // do nothing
}

我说几乎所有滚动的原因是,即使使用上述内容,它仍然接受用户滚动。虽然您可以通过将self.scrollEnabled设置为NO来禁用它们。

The reason I say "almost" all scrolling is that even with the above, it still accepts user scrolls. Though you could disable those by setting self.scrollEnabled to NO.

如果您只想禁用某些滚动,那么制作一个ivar,让我们称之为acceptScrolls,以确定是否你想允许滚动或不滚动。然后你的scrollRectToVisible方法看起来像这样:

If you want to only disable some scrolls, then make an ivar, lets call it acceptScrolls, to determine whether you want to allow scrolling or not. Then your scrollRectToVisible method can look like this:

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated {
   if (self.acceptScrolls)
     [super scrollRectToVisible: rect animated: animated];
}

这篇关于如何完全禁用UITextView的滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆