UIScrollView放大后子视图从背景返回后获得不同的位置

时间:2014-02-27 17:54:16

标签: ios objective-c uiscrollview

所有

我有一个带ScrollView的viewController。在这个scrollView上,我有一个UIImage作为子视图。

UIImage *image = [UIImage imageNamed:@"greyblue_numbered_15x15_900x900.png"];
self.boardImage = [[UIImageView alloc] initWithImage:image];
self.boardImage.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
[self.boardScrollView addSubview:self.boardImage];

self.boardScrollView.contentSize = image.size;

当我缩放时,使用主页按钮然后再次调用应用程序,一切都很好。

现在,我通过将对象拖到scrollView:

来放置其他子视图
- (void)myTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touchPointScreen = [[touches anyObject] locationInView:self.view];
    CGPoint touchPointImage  = [[touches anyObject] locationInView:self.boardImage];

 if (touchPointScreen.x > self.boardScrollView.frame.origin.x &&
     touchPointScreen.x < self.boardScrollView.frame.origin.x + self.boardScrollView.frame.size.width &&
     touchPointScreen.y > self.boardScrollView.frame.origin.y &&
     touchPointScreen.y < self.boardScrollView.frame.origin.y + self.boardScrollView.frame.size.height)
    {
        self.tmpDragObject.frame = CGRectMake(x,y,60,60);

        [self.boardImage addSubview:self.tmpDragObject];
    }
  self.tmpDragObject = nil;
}

缩放时,子视图也会缩放。

- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer {
CGPoint pointInView = [recognizer locationInView:self.boardScrollView];

CGFloat newZoomScale = self.boardScrollView.zoomScale * 1.5f;
newZoomScale = MIN(newZoomScale, self.boardScrollView.maximumZoomScale);

if (newZoomScale != 1.0f) {  
    CGSize scrollViewSize = self.boardScrollView.bounds.size;

    CGFloat w = scrollViewSize.width / newZoomScale;
    CGFloat h = scrollViewSize.height / newZoomScale;
    CGFloat x = pointInView.x - (w / 2.0f);
    CGFloat y = pointInView.y - (h / 2.0f);

    CGRect rectToZoomTo = CGRectMake(x, y, w, h);

    [self.boardScrollView zoomToRect:rectToZoomTo animated:YES];
}

问题当我使用主页按钮然后再次调用应用程序时,该应用程序会重新定位ScrollView的子视图。因此,所有子视图都会向下替换,关于tmpDragObject的大小或导航栏的大小。

为什么会这样?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

显然,我必须将视图控制器上的automatedAdjustsScrollViewInsets设置为NO。