[英]Fixing a UITextView inside a UIScrollView
I have a UISCrollView named scroll1. 我有一个名为scroll1的UISCrollView。 Inside scroll1 i create programmatically UIImageView loaded with images wrapped inside new UIScrollViews.
在scroll1内部,我以编程方式创建UIImageView,其中包含包含在新UIScrollViews中的图像。 So my Architecture is scroll1->subView->ImageView.
所以我的架构是scroll1-> subView-> ImageView。 The object of my application is that the user scrolls the parent scrollview ( scroll1 ) and when he scrolls the uiimages changes.
我的应用程序的对象是用户滚动父滚动视图(scroll1),当他滚动uiimages更改时。
Along with the UIImageViews, a UITextView appears containing text data. 与UIImageViews一起,出现包含文本数据的UITextView。 When the user scrolls the value of the text changes along while the user scrolls.
当用户滚动时,文本的值在用户滚动时改变。
When the user scrolls, i want the UITextView to stay on top of the UIImageVIew as if that text view is embedded inside the ImageView. 当用户滚动时,我希望UITextView保持在UIImageVIew之上,就好像该文本视图嵌入在ImageView中一样。 Similar to scrolling the pages of a story book, when you scroll the text stays fixed with the image until the image disappears.
与滚动故事书的页面类似,当您滚动时,文本会与图像保持一致,直到图像消失。 I am using the following code :
我使用以下代码:
CGSize imageSize;
imageSize.height = 693;
imageSize.width = 512;
self.scroll1.minimumZoomScale = 1.0;
self.scroll1.maximumZoomScale = 1.0;
self.scroll1.frame = CGRectMake(0,0,imageSize.width,imageSize.height);
self.scroll1.scrollEnabled = YES;
self.scroll1.contentSize = CGSizeMake(imageSize.width * imgs.count, imageSize.height );
self.scroll1.pagingEnabled = YES;
self.scroll1.delegate = self;
for (int i = 0; i < pants; i++)
{
CGFloat x = i * 512;
subView = [[UIScrollView alloc]initWithFrame:CGRectMake(x, 0, 512, 693)];
subView2 = [[UIScrollView alloc]initWithFrame:CGRectMake(x + 512, 0, 512, 693)];
subView.clipsToBounds = NO;
subView2.clipsToBounds = NO;
UIImage *img = [imgs objectAtIndex:i];
UIImage *img2;
if(i == pants - 1) img2 = [imgs objectAtIndex:i];
else img2 = [imgs objectAtIndex:i+1];
imageView = [[UIImageView alloc ] initWithImage:img];
imageView2 = [[UIImageView alloc ] initWithImage:img2];
// [imageView insertSubview:_leftText atIndex:-2];
// [imageView insertSubview:_leftText atIndex:1];
//[self.scroll1 insertSubview:_leftText aboveSubview:imageView];
//imageView.frame = subView.bounds;
// [subView insertSubview:_leftText aboveSubview:imageView];
subView.scrollEnabled = NO;
subView2.scrollEnabled = NO;
subView.userInteractionEnabled = NO;
subView2.userInteractionEnabled = NO;
[subView addSubview:imageView];
[subView2 addSubview:imageView2];
[subView setContentSize:CGSizeMake(512, 708)];
[subView2 setContentSize:CGSizeMake(512, 708)];
[self.scroll1 addSubview:subView];
[self.scroll1 addSubview:subView2];
//[self.scroll1 addSubview:_leftText];
}
The above code is used to create the small subviews , leftText is the textView. 上面的代码用于创建小的子视图,leftText是textView。
As it shows i tried various methods to add the _leftText to the subView, because i thought that adding it will automatically make it a part of it and it will embedded, but either it doesn't show or it doesn't work. 因为它显示我尝试了各种方法将_leftText添加到subView,因为我认为添加它会自动使它成为它的一部分,它将嵌入,但要么它不显示或它不起作用。 I even tried the following method :
我甚至尝试了以下方法:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[_leftText setContentOffset:self.scroll1.contentOffset animated:YES];
}
But that code makes the textView behave in a weird way and disappears. 但是该代码使textView以奇怪的方式运行并消失。 I hope there is any way i can accomplish that.
我希望有任何办法可以实现这一目标。 Many Thanks
非常感谢
Try to insert several UITextField instances above each the subView. 尝试在每个subView上面插入几个UITextField实例。
NSMutableArray *textFields=[NSMutableArray array];
for (int i=0; i<pants; i++)
{
CGFloat x=i * 512;
UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:frame];
UIImageView *imageView =[[UIImageView alloc] initWithFrame:imageFrame];
UITextField *textField =[[UITextField alloc] initWithFrame:textFrame];
[scrollView addSubview:imageView];
[scrollView addSubview:textField]; // put it above the imageView
[textFields addObject:textField];
}
So there are several text fields instead of the only one. 所以有几个文本字段而不是唯一的一个。 Then you need to find out which text field is visible at a current time in
scrollViewDidScroll:
delegate method. 然后,您需要在
scrollViewDidScroll:
delegate方法中scrollViewDidScroll:
当前时间可见的文本字段。 For this better to use contentOffset
property of scroll1. 为此,最好使用scroll1的
contentOffset
属性。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect visibleRect;
visibleRect.origin = scrollView.contentOffset;
visibleRect.size = scrollView.bounds.size;
// iterate through the textFields array to find out which textField is currently visible.
}
Good luck. 祝好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.