我有一个名为scroll1的UISCrollView。在scroll1内部,我以编程方式创建了UIImageView,并加载了包装在新UIScrollViews中的图像。所以我的体系结构是scroll1-> subView-> ImageView。我的应用程序的目标是用户滚动父scrollview(scroll1),并且当他滚动uiimages时会发生变化。与UIImageViews一起,出现一个包含文本数据的UITextView。当用户滚动时,文本值随用户滚动而改变。当用户滚动时,我希望UITextView停留在UIImageVIew的顶部,就像该文本视图已嵌入ImageView内一样。与滚动故事书的页面类似,滚动时,文本与图像保持固定,直到图像消失。我正在使用以下代码:
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];
}
上面的代码用于创建小的子视图,leftText是textView。
正如它显示的那样,我尝试了各种方法将_leftText添加到subView,因为我认为添加_leftText会自动使其成为其一部分并嵌入其中,但是要么不显示,要么不起作用。我什至尝试了以下方法:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[_leftText setContentOffset:self.scroll1.contentOffset animated:YES];
}
但是,这些代码使textView表现得很奇怪,并且消失了。我希望有什么办法可以做到。非常感谢
最佳答案
尝试在每个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];
}
因此,有几个文本字段,而不是只有一个。然后,您需要在
scrollViewDidScroll:
委托方法中找出当前当前可见的文本字段。为此,最好使用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.
}
祝好运。
关于ios - 在UIScrollView中修复UITextView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26604280/