保存 UIScrollView 的视图

发布于 2024-12-16 13:24:47 字数 1554 浏览 3 评论 0原文

我正在构建一个应用程序,其中的一部分允许您滚动浏览不同的图像以进行叠加。我有一个分段控件来制作一种分层系统,其中每个分段都允许您再次滚动图像并构建图层。由于某种原因,第一个图像总是粘住,当我返回到图层时,视图会重置到其原始位置,但也会保存最后一个位置?

这是我的代码。

- (void)layerControl
{
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger viewCount = 8;
for (int i = 0; i < viewCount; i++) {
    CGFloat yOrigin = i * self.view.frame.size.width;
    UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [filterViewOverlay setImage:[filterManager objectAtIndex:i]];
    [scroll addSubview:filterViewOverlay];
    //[filterViewOverlay release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll autorelease];

//Determine the current filter in view of the scroll view
CGFloat pageWidth = scroll.frame.size.width;
int cFilter = floor((scroll.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
currentFilter = cFilter;

}



//Method to add a filter to the page
- (void)addFilter:(id)sender
{
UISegmentedControl *filterController = (UISegmentedControl *)sender;

switch ([filterController selectedSegmentIndex])
{
    case 0: self.layerControl;

    case 1: self.layerControl;

    case 2: self.layerControl;

    case 3: self.layerControl;

    case 4: self.layerControl;

    case 5: self.layerControl;

    default: NULL;
}
}

I am building an app where part of it allows you to scroll through different images to overlay. I have a segmented control to make a sort of layering system where each segment allows you to scroll through the images again and build up layers. For some reason first image always sticks and when I return to a layer, the view is reset to its original position but also saves the last position?

Here is my code.

- (void)layerControl
{
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger viewCount = 8;
for (int i = 0; i < viewCount; i++) {
    CGFloat yOrigin = i * self.view.frame.size.width;
    UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [filterViewOverlay setImage:[filterManager objectAtIndex:i]];
    [scroll addSubview:filterViewOverlay];
    //[filterViewOverlay release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll autorelease];

//Determine the current filter in view of the scroll view
CGFloat pageWidth = scroll.frame.size.width;
int cFilter = floor((scroll.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
currentFilter = cFilter;

}



//Method to add a filter to the page
- (void)addFilter:(id)sender
{
UISegmentedControl *filterController = (UISegmentedControl *)sender;

switch ([filterController selectedSegmentIndex])
{
    case 0: self.layerControl;

    case 1: self.layerControl;

    case 2: self.layerControl;

    case 3: self.layerControl;

    case 4: self.layerControl;

    case 5: self.layerControl;

    default: NULL;
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后知后觉 2024-12-23 13:24:47

看起来也许每次你调用 self.layerControl 时你都会在视图上创建一个新的 UIScrollView 。
所以旧的仍然存在并且处于旧的位置,但是你添加了另一个。

也许存储对每个 UIScrollView 的引用,并在 layerControl 的开头抓取给定索引的滚动视图。

所以:

- (void)layerControl:(NSInteger)index {
   UIScrollView *scrollView = nil;
   switch (index) {
      case (0): scrollView = self.scrollView1;
      /* Repeat for 1 - 6 */
   }

   if (scrollView == nil) {
      // Do the creation of the scrollview here
   }

然后在你的addFilter方法中调用:

[self layerControl:[filterController selectedSegmentIndex]]

而不是大的switch语句。

顺便说一句,像所有 self.layerControl 调用那样使用点语法调用方法并不是一个好主意。
当您访问合成属性时,您应该保留点语法。

It looks like maybe every time you call self.layerControl you are creating a new UIScrollView on the view.
So the old one is still there and in the old position, but you've added another one.

Perhaps store a reference to each UIScrollView and at the beginning of layerControl grab the scrollview for the given index.

So:

- (void)layerControl:(NSInteger)index {
   UIScrollView *scrollView = nil;
   switch (index) {
      case (0): scrollView = self.scrollView1;
      /* Repeat for 1 - 6 */
   }

   if (scrollView == nil) {
      // Do the creation of the scrollview here
   }

Then in your addFilter method call:

[self layerControl:[filterController selectedSegmentIndex]]

rather than the big switch statement.

As an aside, it's not a great idea to call methods with dot syntax like you have with all those self.layerControl calls.
You should leave dot syntax for times when you're accessing a synthesized property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文