有什么办法可以从其他视图滚动 UITextView 吗? [英] Is there any way I can scroll UITextView from other view?

查看:18
本文介绍了有什么办法可以从其他视图滚动 UITextView 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITextView 和一个简单的空白视图,其中包含 UIPanGestureRecognizer.这背后的想法如下:我在空白视图中做了一个平移手势,手势识别器检测到它并滚动文本视图(通过 scrollToVisibleRect 或类似的方法).有没有其他办法?我问是因为这个尚未解决.

解决方案

基本上,Logan 的回答是正确的.虽然,我想补充一点:您需要减去(而不是添加)平移的 y 分量.所以代码应该是这样的:

currentOffset.y -= [pan translationInView:pan.view].y;

然后你必须防止 contentOffset 超过你的 textView 的 contentSize.height.只需添加:

CGFloat minOffset = 0.0;CGFloat maxOffset = self.myTextView.contentSize.height - self.myTextView.bounds.size.height;self.myTextView.contentOffset = CGPointMake(0,fmax(minOffset, fmin(currentOffset.y, maxOffset)));

是的,这一行非常重要

`[gestureRecognizer setTranslation:CGPointZero inView:gestureRecognizer.view];`

为什么?Apple 在 :

中说

- (CGPoint)translationInView:(UIView *)view

x 和 y 值报告随时间的总平移.它们不是上次报告翻译的增量值.应用翻译值到视图状态手势被第一次识别时——不要连接值每次调用处理程序时.

有关更多信息,您最好阅读this.>

I have a UITextView and a simple blank view with UIPanGestureRecognizer in it. The idea behind this was the following: I make a pan gesture in blank view, gesture recognizer detects it and scrolls the text view (by scrollToVisibleRect or smth like that). Is there any other way? I am asking because this is not resolved yet.

解决方案

Basically, Logan's answer is right. Although, I would like to add this: You need to subtract(not add) the y component of translation. So the code should look like this:

currentOffset.y -= [pan translationInView:pan.view].y;

And then you must prevent contentOffset's being more than your textView's contentSize.height.Just add this:

CGFloat minOffset = 0.0;
CGFloat maxOffset = self.myTextView.contentSize.height - self.myTextView.bounds.size.height;

self.myTextView.contentOffset = CGPointMake(0,fmax(minOffset, fmin(currentOffset.y, maxOffset))); 

And yes,this line is VERY important

`[gestureRecognizer setTranslation:CGPointZero inView:gestureRecognizer.view];`

Why? Apple says in the :

- (CGPoint)translationInView:(UIView *)view

The x and y values report the total translation over time. They are not delta values from the last time that the translation was reported. Apply the translation value to the state of the view when the gesture is first recognized—do not concatenate the value each time the handler is called.

For further info, you'd better read this.

这篇关于有什么办法可以从其他视图滚动 UITextView 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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