どちらの側にも UIScrollView に含まれるビューの一部を表示する UIScrollView を作成しようとしています。
これが写真です。
この図は、親ビューまたはメイン ビューである灰色の領域を示しています。紫色の領域は UIScrollView です。白い領域は、ボックスのイメージ、準備完了のタイトル、および急いでボタンを含むビューです。
私が抱えている問題は、この白い領域がスペース全体を占めており、画面にスクロールすることさえできるという視覚的な兆候がなく、さらにコンテンツがあることです.
理想的には、次のように、UIScrollView 内に複数の白いサブビューがあり、各サブビューの一部を両側に表示できるようにすることです。
この概念では、サブビューのカットオフの一部があることがわかりますが、ほとんどの場合、UIScrollView に複数のサブビューが表示されます。
サブビュー (白いセクションに含まれるビュー) は 75 (幅) x 160 (高さ) です。
ただし、これを機能させることはできないようです。clipToBounds を yes と no の両方に設定しようとしましたが、コンテンツ サイズを強制しようとしました。どちらも予期しない結果をもたらします。
UIScrollView に複数の UIView、または UIScrollView に UIView の一部を表示するにはどうすればよいですか?
どうもありがとう。
これが私のコードです。
// Code follows
- (void)viewDidLoad
{
// Scrollview
int recordsFound = 6; // 6 products
float scrollViewWidth = (self.scrollView.frame.size.width * recordsFound);
float scrollViewHeight = (self.scrollView.frame.size.height);
[self.scrollView setContentSize:CGSizeMake(scrollViewWidth, scrollViewHeight)];
[self.scrollView setClipsToBounds:YES];
[self.scrollView setScrollEnabled:YES];
[self.scrollView setPagingEnabled:YES];
[self.scrollView setDirectionalLockEnabled:YES];
[self.scrollView setUserInteractionEnabled:YES];
[self.scrollView setShowsHorizontalScrollIndicator:YES];
[self.scrollView setShowsVerticalScrollIndicator:NO];
[self.scrollView setScrollEnabled:YES];
[self.scrollView setDelegate:self];
// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < recordsFound; i++)
{
[controllers addObject:[NSNull null]];
} // next
//self.scrollView.bounds = CGRectMake(0, 0, 100, scrollView.frame.size.height);
self.viewControllers = controllers;
[controllers release];
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}
-(void)loadScrollViewWithPage:(int)page
{
int numberOfRecords = 6;
if (page < 0)
return;
if (page >= numberOfRecords)
return;
// replace the placeholder if necessary
SecondPageVC *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
controller = [[SecondPageVC alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (controller.view.superview == nil)
{
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
-(void)scrollViewDidScroll:(UIScrollView *)sender
{
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (pageControlBeingUsed)
{
// do nothing - the scroll was initiated from the page control, not the user dragging
return;
}
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// A possible optimization would be to unload the views+controllers which are no longer visible
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
}