2

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;
}
}
4

1 回答 1

0

看起来可能每次您调用 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 调用一样使用点语法调用方法并不是一个好主意。当您访问综合属性时,您应该保留点语法。

于 2011-11-17T00:27:16.097 回答