在我的应用程序中,我在UIScrollView中使用了UIImageView,我使用了代码。
- (void)viewDidLoad
{
scrollV.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
[super viewDidLoad];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}
代码工作正常但是当我将UIButton放在scrollView的imageView上并且在我缩放scrollV之后,imageView完全根据scrollView缩放而不是UIButton而不是缩放。
我希望当我缩放图像视图时,我放在imageView上的按钮也可以相对于缩放的imageView进行缩放。
答案 0 :(得分:1)
将代理UIView实例创建为scrollview的子视图,并将要放大的所有UI元素放入其中。然后从viewForZoomingInScrollView
方法
答案 1 :(得分:0)
CGSize imageView = [photo size];
UIScrollView *scrollView = (UIScrollView *)[self view];
[scrollView setDelegate:self];
[scrollView setContentSize:photoSize];
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setShowsHorizontalScrollIndicator:NO];
[scrollView setShowsVerticalScrollIndicator:NO];
// Create the image view.
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
[imageView setImage:photo];
[scrollView addSubview:imageView];
// Configure zooming.
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
CGFloat widthRatio = screenSize.width / photoSize.width;
CGFloat heightRatio = screenSize.height / photoSize.height;
CGFloat initialZoom = (widthRatio > heightRatio) ? heightRatio : widthRatio;
[scrollView setMaximumZoomScale:3.0];
[scrollView setMinimumZoomScale:initialZoom];
[scrollView setZoomScale:initialZoom];
[scrollView setBouncesZoom:YES];
// Center the photo.
CGFloat topInset = (screenSize.height - photoSize.height * initialZoom) / 2.0;
if (topInset > 0.0)
{
[scrollView setContentInset:UIEdgeInsetsMake(topInset - 44.0, 0.0, 0.0, 0.0)];
}
}
#pragma mark -
#pragma mark UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
CGSize photoSize = [photo size];
CGFloat topInset = (screenSize.height - photoSize.height * [scrollView zoomScale]) / 2.0;
if (topInset < 0.0)
{
topInset = 0.0;
}
[scrollView setContentInset:UIEdgeInsetsMake(topInset, 0.0, -topInset, 0.0)];
}
答案 2 :(得分:0)
我成功找到了上述问题的解决方案。我为此写了,
- (void)viewDidLoad
{
bgViewOfImage.contentStretch = CGRectMake(ImageView.frame.origin.x, ImageView.frame.origin.y, ImageView.frame.size.width, ImageView.frame.size.height);
scrollV.contentSize = CGSizeMake(bgViewOfImage.frame.size.width, bgViewOfImage.frame.size.height);
[super viewDidLoad];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return bgViewOfImage;
}
我创建了一个在主视图上插入UIView的层次结构,然后在其上拖动其他控件,如UIImageview和UIButtons。现在它完美地工作并缩放每个控件。