我有一个名为scroll1的UISCrollView。在scroll1内部,我以编程方式创建UIImageView,其中包含包含在新UIScrollViews中的图像。所以我的架构是scroll1-> subView-> ImageView。我的应用程序的对象是用户滚动父滚动视图(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,因为我认为添加它将自动使其成为它的一部分并且它将嵌入,但要么它没有显示,要么它没有工作。我甚至尝试了以下方法:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[_leftText setContentOffset:self.scroll1.contentOffset animated:YES];
}
但该代码使textView以奇怪的方式运行并消失。我希望有任何办法可以实现这一目标。非常感谢
答案 0 :(得分:1)
尝试在每个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.
}
祝你好运。