如何配置UIScrollView以使可见区域永远不会超出背景图像?(How to configure a UIScrollView so the visible area never goes bey

编程入门 行业动态 更新时间:2024-10-26 16:28:47
如何配置UIScrollView以使可见区域永远不会超出背景图像?(How to configure a UIScrollView so the visible area never goes beyond a background image?)

在花了半个晚上试图让我自己工作后,我终于转向StackWisdom了:

我正在尝试使用一种视差滚动背景图像(类似于Windows Phone 7上的UI)实现水平,分页UIScrollView。 我已经设法使用scrollViewDidScroll让滚动工作正常。 对于3页的例子,我使用的是1460x480 / 2920x960像素的图像(3 * 320点+ 2 * 250填充宽度)​​。 根据我在scrollViewDidScroll中使用的因素,滚动视图的右边缘也可以正常工作。

但是,当在第1页上并尝试向左滚动时,可以看到背景图像在屏幕的边界处结束,并且窗口(灰色,白色,无论如何)背景变得可见。 如何配置滚动​​视图,以便在内容区域左侧有一个额外的250像素的背景图像? 我已经尝试了插入,偏移,重新定位内容区域和无数组合,但无济于事。

此外,这是早上5:30,我很累。 -_-

资源:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; CGRect screenRect = [[self window] bounds]; // Create and configure scroll view UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:screenRect]; [scrollView setDelegate:self]; [scrollView setPagingEnabled:YES]; [scrollView setShowsHorizontalScrollIndicator:NO]; [[self window] addSubview:scrollView]; pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(50, 50, 200, 50)]; [pageControl setNumberOfPages:3]; [pageControl setCurrentPage:0]; [pageControl setBackgroundColor:[UIColor clearColor]]; [[self window] addSubview:pageControl]; backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"desert_panorama_cropped.png"]]; [scrollView addSubview:backgroundImageView]; // Create first content view screenRect.origin.x = 0; ContentView *firstContentView = [[ContentView alloc] initWithFrame:screenRect string:@"Page 1"]; [scrollView addSubview:firstContentView]; // Create second content view screenRect.origin.x = screenRect.size.width; ContentView *secondContentView = [[ContentView alloc] initWithFrame:screenRect string:@"Page 2"]; [scrollView addSubview:secondContentView]; // Create third content view screenRect.origin.x = screenRect.size.width * 2.0; ContentView *thirdContentView = [[ContentView alloc] initWithFrame:screenRect string:@"Page 3"]; [scrollView addSubview:thirdContentView]; CGRect contentRect = screenRect; contentRect.size.width *= 3.0; [scrollView setContentSize:contentRect.size]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { float x = scrollView.contentOffset.x; NSLog(@"Scrollview did scroll to offset: %f", x); CGRect backgroundImageFrame = backgroundImageView.frame; backgroundImageFrame.origin.x = x/1.5; backgroundImageView.frame = backgroundImageFrame; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { int newOffset = scrollView.contentOffset.x; int newPage = (int)((newOffset)/(scrollView.frame.size.width)); [pageControl setCurrentPage:newPage]; }

任何帮助将非常感激。

After having spent half the night trying to get this to work on my own I finally turn to StackWisdom:

I'm trying to implement a horizontal, paging UIScrollView with a sort of parallax scrolling background image (similar to the UI on Windows Phone 7). I've managed to get the scrolling working just fine using scrollViewDidScroll. For a 3 page example I am using an image with 1460x480/2920x960 pixels (3*320 points + 2*250 padding for width). And depending on the factor I use in scrollViewDidScroll, the right edge of the scroll view works just fine as well.

However, when on page 1 and trying to scroll further left, it becomes visible that the background image ends at the border of the screen and the window's (grey, white, whatever) background becomes visible. How do I configure the scroll view so that there's an extra, say, 250 pixels of the background image to the left of the content area? I've tried insets, offsets, repositioning the content area and countless combinations thereof, but to no avail.

Also, it's 5:30 in the morning here and I'm tired. -_-

Source:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; CGRect screenRect = [[self window] bounds]; // Create and configure scroll view UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:screenRect]; [scrollView setDelegate:self]; [scrollView setPagingEnabled:YES]; [scrollView setShowsHorizontalScrollIndicator:NO]; [[self window] addSubview:scrollView]; pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(50, 50, 200, 50)]; [pageControl setNumberOfPages:3]; [pageControl setCurrentPage:0]; [pageControl setBackgroundColor:[UIColor clearColor]]; [[self window] addSubview:pageControl]; backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"desert_panorama_cropped.png"]]; [scrollView addSubview:backgroundImageView]; // Create first content view screenRect.origin.x = 0; ContentView *firstContentView = [[ContentView alloc] initWithFrame:screenRect string:@"Page 1"]; [scrollView addSubview:firstContentView]; // Create second content view screenRect.origin.x = screenRect.size.width; ContentView *secondContentView = [[ContentView alloc] initWithFrame:screenRect string:@"Page 2"]; [scrollView addSubview:secondContentView]; // Create third content view screenRect.origin.x = screenRect.size.width * 2.0; ContentView *thirdContentView = [[ContentView alloc] initWithFrame:screenRect string:@"Page 3"]; [scrollView addSubview:thirdContentView]; CGRect contentRect = screenRect; contentRect.size.width *= 3.0; [scrollView setContentSize:contentRect.size]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { float x = scrollView.contentOffset.x; NSLog(@"Scrollview did scroll to offset: %f", x); CGRect backgroundImageFrame = backgroundImageView.frame; backgroundImageFrame.origin.x = x/1.5; backgroundImageView.frame = backgroundImageFrame; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { int newOffset = scrollView.contentOffset.x; int newPage = (int)((newOffset)/(scrollView.frame.size.width)); [pageControl setCurrentPage:newPage]; }

Any help would be much appreciated.

最满意答案

我认为你所要做的就是为这一行添加一个常量,如下所示:

backgroundImageFrame.origin.x = x/1.5 - 250;

I think all you have to do is add a constant to this line like so:

backgroundImageFrame.origin.x = x/1.5 - 250;

更多推荐

本文发布于:2023-07-05 15:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1039155.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:永远不会   图像   背景   区域   UIScrollView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!