Problem in zooming of UIScrollView?
In my application i used an UIImageView in UIScrollView and i used code for that.
- (void)viewDidLoad
{
scrollV.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame开发者_JAVA百科.size.height);
[super viewDidLoad];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}
the code working fine but when i placing UIButton on the imageView of scrollView and after i zooming the scrollV, the imageView zoom perfectly according to scrollView but UIButton and not be zoomed.
I want when i zoom the imageview, the buttons i placed on imageView also be zoom with respect to zoomed imageView.
Create a proxy UIView instance as scrollview's subview and place all UI elements you want to be zoomed into it. Then return that view from viewForZoomingInScrollView
method
I got success to find out the solution of above question.I written the for that,
- (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;
}
I creating an hierarchy inserting an UIView on main view then i dragging other controls like UIImageview's and UIButtons on it.Now it perfectly working and zooming each control.
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)];
}
精彩评论