简体   繁体   中英

Fixing a UITextView inside a UIScrollView

I have a UISCrollView named scroll1. Inside scroll1 i create programmatically UIImageView loaded with images wrapped inside new UIScrollViews. So my Architecture is scroll1->subView->ImageView. The object of my application is that the user scrolls the parent scrollview ( scroll1 ) and when he scrolls the uiimages changes.
Along with the UIImageViews, a UITextView appears containing text data. 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. 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.
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. 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. I hope there is any way i can accomplish that. Many Thanks

Try to insert several UITextField instances above each the subView.

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. For this better to use contentOffset property of scroll1.

- (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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM