Tôi muốn được trợ giúp với mẫu UIScrollView của tôi.nội dung nhảy lên thu nhỏ với UIScrollView
Tôi đã tạo một chương trình đơn giản để cuộn và thu phóng nội dung (UIImageView
). Nó hoạt động tốt, ngoại trừ nội dung thường xuyên biến mất ở phía dưới bên phải khi tôi cố gắng thu nhỏ. Nhưng kể từ khi tôi đặt minimumZoomScale
thành 1.0f, nó thực sự không thu nhỏ, chỉ có nội dung nhảy ra khỏi chế độ xem. Và điều kỳ lạ hơn nữa là tôi không thể cuộn lên sau này được. Rõ ràng kích thước nội dung cũng bị sai lệch.
Các thiết lập tôi có trong mẫu mã của tôi là như trong hình bên dưới.
Khi tôi kiểm tra tình trạng sau (cố gắng) thu nhỏ, tôi thấy hai điều sai trái.
_scrollView.contentSize
là 480x360, mà không nên nhỏ hơn 1000x1000_scrollView.bounds
nhảy lên hàng đầu bằng cách nào đó (ví dụ,_scrollView.bounds.origin.y
luôn là 0)
Để đối phó với hai mục ở trên, tôi đã thêm mã sau vào UIScrollViewDelegate của tôi và bây giờ nó hoạt động tốt.
- (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;
}
}
}
Tuy nhiên, bạn có cần phải thực hiện việc này không? Đây là một chuỗi rất đơn giản, và thật khó để tin rằng Apple yêu cầu chúng ta phải đặt loại nỗ lực này. Vì vậy, đặt cược của tôi là tôi đang thiếu một cái gì đó ở đây ...
Sau đây là mã ban đầu của tôi. Xin vui lòng giúp tôi tìm thấy những gì tôi đang làm sai (hoặc thiếu một cái gì đó)!
#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;
}
Bạn đã thử đặt '_contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; '? – Adam
Cảm ơn bạn đã đề xuất. Nhưng không, nó không thay đổi tình hình ... – barley
Tôi có thể hỏi tại sao 'contentSize' của bạn là 1000x1000? – tia