UIScrollView サンプルのヘルプが必要です。
コンテンツをスクロールおよびズームする簡単なプログラムを作成しました ( UIImageView
)。ズームアウトしようとすると、コンテンツが頻繁に右下に消えてしまうことを除けば、問題なく動作します。しかし、minimumZoomScale
1.0f に設定したので、実際にはズームアウトしていません。コンテンツだけがビューから飛び出しています。さらに奇妙なのは、この後スクロールアップできないことです。どうやらコンテンツのサイズもめちゃくちゃです。
サンプルコードでの設定は、下の図のとおりです。
ズームアウトして(試して)ステータスを確認したところ、間違っていることが 2 つあります。
_scrollView.contentSize
480x360 で、1000x1000 より小さくすることはできません_scrollView.bounds
どういうわけかトップにジャンプしました (つまり、_scrollView.bounds.origin.y
常に 0 です)
上記の 2 つの項目に対処するために、UIScrollViewDelegate に次のコードを追加したところ、正常に動作するようになりました。
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
if(scrollView == _scrollView && view == _contentView)
{
// Setting ivars for scrollViewDidZoom
_contentOffsetBeforeZoom = _scrollView.contentOffset;
_scrollViewBoundsBeforeZoom = _scrollView.bounds;
}
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
if(scrollView == _scrollView)
{
// If you zoom out, there are cases where ScrollView content size becomes smaller than original,
// even though minimum zoom scale = 1. In that case, it will mess with the contentOffset as well.
if(_scrollView.contentSize.width < CONTENT_WIDTH || _scrollView.contentSize.height < CONTENT_HEIGHT)
{
_scrollView.contentSize = CGSizeMake(CONTENT_WIDTH, CONTENT_HEIGHT);
_scrollView.contentOffset = _contentOffsetBeforeZoom;
}
// If you zoom out, there are cases where ScrollView bounds goes outsize of contentSize rectangle.
if(_scrollView.bounds.origin.x + _scrollView.bounds.size.width > _scrollView.contentSize.width ||
_scrollView.bounds.origin.y + _scrollView.bounds.size.height > _scrollView.contentSize.height)
{
_scrollView.bounds = _scrollViewBoundsBeforeZoom;
}
}
}
しかし、これに至る必要がありますか?これは非常に単純な手順であり、Apple がこの種の努力を私たちに要求しているとは信じがたいです。だから、私の賭けは、ここで何かが欠けているということです...
以下は私の元のコードです。私が間違っていること(または何かが欠けていること)を見つけるのを手伝ってください!
#define CONTENT_WIDTH 1000
#define CONTENT_HEIGHT 1000
>>>> Snip >>>>
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
_scrollView.contentSize = CGSizeMake(CONTENT_WIDTH, CONTENT_HEIGHT);
_scrollView.backgroundColor = [UIColor blueColor];
_scrollView.maximumZoomScale = 8.0f;
_scrollView.minimumZoomScale = 1.0f;
_scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
_scrollView.scrollEnabled = YES;
_scrollView.delegate = self;
[self.view addSubview:_scrollView];
_contentView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sample.jpg"]]; // sample.jpg is 480x360
CGPoint center = (CGPoint){_scrollView.contentSize.width / 2, _scrollView.contentSize.height / 2};
_contentView.center = center;
[_scrollView addSubview:_contentView];
_scrollView.contentOffset = (CGPoint) {center.x - _scrollView.bounds.size.width / 2, center.y - _scrollView.bounds.size.height / 2};
}
- (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
{
if(scrollView == _scrollView)
{
return _contentView;
}
return nil;
}