Ios 在UIScrollView中缩放UIImageView的行为很奇怪
我有以下问题,伙计们。我有一个很像苹果PhotoScroller的应用程序。我想通过滑动屏幕从一个图像跳到另一个图像。我可以,但我不能缩放图像。问题是,我有一个带有图像的数组。如果数组中只有一个对象,则缩放没有问题。但是如果阵列中有更多图像,当我尝试缩放时,它们的行为会很奇怪。图像未被缩放,它将在屏幕外的任何位置显示。这是我的密码:Ios 在UIScrollView中缩放UIImageView的行为很奇怪,ios,imageview,zooming,scrollview,Ios,Imageview,Zooming,Scrollview,我有以下问题,伙计们。我有一个很像苹果PhotoScroller的应用程序。我想通过滑动屏幕从一个图像跳到另一个图像。我可以,但我不能缩放图像。问题是,我有一个带有图像的数组。如果数组中只有一个对象,则缩放没有问题。但是如果阵列中有更多图像,当我尝试缩放时,它们的行为会很奇怪。图像未被缩放,它将在屏幕外的任何位置显示。这是我的密码: int pageWidth = 1024; int pageHeight = 768; int counter = 0; self.view = [[UIView
int pageWidth = 1024;
int pageHeight = 768;
int counter = 0;
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, pageWidth, pageHeight)];
CGRect containerFrame = self.view.frame;
scrollView = [[UIScrollView alloc] initWithFrame:containerFrame];
[self.view addSubview:scrollView];
NSMutableArray *all = [[NSMutableArray alloc] init];
[all addObject:[UIImage imageNamed:@"222.jpg"]];
CGSize scrollSize = CGSizeMake(pageWidth * [all count], pageHeight);
[scrollView setContentSize:scrollSize];
for (UIImage *image in all)
{
UIImage *pageImage = [[UIImage alloc] init];
pageImage = [all objectAtIndex:counter];
CGRect scrollFrame = CGRectMake(pageWidth * counter, 0, pageWidth, pageHeight);
miniScrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
[scrollView addSubview:miniScrollView];
CGSize miniScrollSize = CGSizeMake(pageImage.size.width, pageImage.size.height);
[miniScrollView setContentSize:miniScrollSize];
UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, pageImage.size.width, pageImage.size.height)];
tempImageView.image = [all objectAtIndex:counter];
self.imageView = tempImageView;
miniScrollView.maximumZoomScale = 3.0;
miniScrollView.minimumZoomScale = 1.0;
miniScrollView.clipsToBounds = YES;
miniScrollView.delegate = self;
miniScrollView.showsHorizontalScrollIndicator = NO;
miniScrollView.showsVerticalScrollIndicator = NO;
miniScrollView.bouncesZoom = YES;
[miniScrollView addSubview:imageView];
counter ++;
}
[scrollView setPagingEnabled:YES];
[scrollView setScrollEnabled:YES];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}
你知道怎么了吗?因为我已经尝试了将近两周的时间来完成这项工作。我也开发过类似的应用程序。您可以做的第一件事是获取ScrollView的单独子类,以便轻松处理所有分页和缩放操作。在scrollView类中,可以从以下代码段中获取引用
@interface PhotoScrollView : UIScrollView<UIScrollViewDelegate,UIGestureRecognizerDelegate>
{
int finalPhotoWidth;
int finalPhotoHeight;
}
// to contain image
@property (strong, nonatomic) UIImageView *imageView;
- (id)initWithFrame:(CGRect)frame andImage:(UIImage *)photo
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
[self initializeScrollViewWithImage:photo];
//setting gesture recognizer for single tap
UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewSingleTapped:)];
singleTapRecognizer.delegate = self;
singleTapRecognizer.numberOfTapsRequired = 1;
[self addGestureRecognizer:singleTapRecognizer];
//setting gesture recognizer for Double tap
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewDoubleTapped:)];
doubleTapRecognizer.delegate = self;
doubleTapRecognizer.numberOfTapsRequired = 2;
[self addGestureRecognizer:doubleTapRecognizer];
[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
singleTapActivated = FALSE;
self.backgroundColor = [UIColor blackColor];
self.minimumZoomScale = 1.0f;
self.maximumZoomScale = 15.0f;
self.zoomScale = 1.0f;
self.delegate = self;
}
return self;
}
//for sizing the frame by giving height and width
-(void)initializeScrollViewWithImage:(UIImage*)photo
{
finalPhotoWidth = photo.size.width;
finalPhotoHeight = photo.size.height;
//Pre-checking of frame and setting the height and width accordingly
if (finalPhotoHeight > self.frame.size.height)
{
// if photo height is bigger than frame height, re-adjust the photo height and width accordingly
finalPhotoHeight = self.frame.size.height;
finalPhotoWidth = (photo.size.width/photo.size.height) * finalPhotoHeight;
}
if (finalPhotoWidth > self.frame.size.width)
{
// if photo width is bigger than frame width, re-adjust the photo height and width accordingly
finalPhotoWidth = self.frame.size.width;
finalPhotoHeight = (photo.size.height/photo.size.width) * finalPhotoWidth;
}
if (finalPhotoHeight < self.frame.size.height && finalPhotoWidth < self.frame.size.width)
{
// if the photo is smaller than frame, increase the photo width and height accordingly
finalPhotoWidth = self.frame.size.width;
finalPhotoHeight = self.frame.size.height;
}
//initialising imageView with the thumbnail photo
self.imageView = [[UIImageView alloc] initWithImage:photo];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
//setting frame according to the modified width and height
if(!isnan(finalPhotoWidth) && !isnan(finalPhotoHeight))
{
self.imageView.frame = CGRectMake( (self.frame.size.width - finalPhotoWidth) / 2, (self.frame.size.height - finalPhotoHeight)/2, finalPhotoWidth, finalPhotoHeight);
}
// setting scrollView properties
self.contentSize = self.imageView.frame.size;
// add image view to scroll view
[self addSubview:self.imageView];
}
//to deny the simultaneous working of single and double taps
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return NO;
}
// Gesture handleer for single tap gesture
-(void)scrollViewSingleTapped:(UITapGestureRecognizer *)recognizer
{
if(!singleTapActivated)
{
//do as per requirement
singleTapActivated = TRUE;
}
else
{
//do as per requirement
singleTapActivated = FALSE;
}
}
//for zooming after double tapping
- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer
{
//to check whether image is zoomed
if (self.zoomScale != 1.0f)
{
//if image is zoomed, then zoom out
[self setZoomScale:1.0 animated:YES];
self.imageView.frame = CGRectMake( (self.frame.size.width - finalPhotoWidth) / 2, (self.frame.size.height - finalPhotoHeight)/2, finalPhotoWidth, finalPhotoHeight);
[self.observer photoZoomStopped];
}
else
{
// get the point of image which is double tapped
CGPoint pointInView = [recognizer locationInView:self.imageView];
// Get a zoom scale that's zoomed in slightly, capped at the maximum zoom scale specified by the scroll view
CGFloat newZoomScale = self.zoomScale * 4.0f;
newZoomScale = MIN(newZoomScale, self.maximumZoomScale);
// Figure out the rect we want to zoom to, then zoom to it
CGSize scrollViewSize = self.imageView.frame.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 zoomToRect:rectToZoomTo animated:YES];
}
}
// To re-center the image after zooming in and zooming out
- (void)centerScrollViewContents
{
CGSize boundsSize = self.bounds.size;
CGRect contentsFrame = self.imageView.frame;
if (contentsFrame.size.width < boundsSize.width)
{
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
}
else
{
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height)
{
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
}
else
{
contentsFrame.origin.y = 0.0f;
}
self.imageView.frame = contentsFrame;
}
//for zooming in and zooming out
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
if (self.zoomScale > 1.0f)
{
[self.observer photoZoomStarted];
[self centerScrollViewContents];
}
else
{
self.bouncesZoom = NO;
[self.observer photoZoomStopped];
// for zooming out by pinching
[self centerScrollViewContents];
}
// The scroll view has zoomed, so we need to re-center the contents
}
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
@interface PhotoScrollView:UIScrollView
{
国际最终照片宽度;
国际最终摄影权;
}
//包含图像
@属性(强,非原子)UIImageView*imageView;
-(id)initWithFrame:(CGRect)frame和Image:(UIImage*)照片
{
self=[super initWithFrame:frame];
如果(自我)
{
//初始化代码
[自初始化滚动视图,图像:照片];
//设置单点击手势识别器
UITapGestureRecognizer*singleTapRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:自我操作:@selector(scrollViewSingleTapped:)];
singleTapRecognizer.delegate=self;
singleTapRecognizer.numberOfTapsRequired=1;
[自添加GestureRecognitor:SingleTapRecognitor];
//设置双击手势识别器
UITapGestureRecognizer*doubleTapRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:自我操作:@selector(scrollViewDoubleTapped:)];
doubleTapRecognizer.delegate=self;
DoubleTapRecognitor.numberOfTapsRequired=2;
[自添加GestureRecognitor:DoubleTapRecognitor];
[singleTapRecognizer RequireTestureRecognizerToFail:doubleTapRecognizer];
singleTapActivated=FALSE;
self.backgroundColor=[UIColor blackColor];
self.minimumZoomScale=1.0f;
self.maximumZoomScale=15.0f;
self.zoomScale=1.0f;
self.delegate=self;
}
回归自我;
}
//用于通过给定高度和宽度调整框架大小
-(无效)使用图像初始化滚动视图:(UIImage*)照片
{
finalPhotoWidth=photo.size.width;
最终照片高度=photo.size.height;
//预检查框架并相应设置高度和宽度
如果(最终照片高度>自帧大小高度)
{
//如果照片高度大于边框高度,请相应地重新调整照片高度和宽度
最终照片高度=self.frame.size.height;
最终照片宽度=(photo.size.width/photo.size.height)*最终照片高度;
}
if(finalPhotoWidth>self.frame.size.width)
{
//如果照片宽度大于边框宽度,请相应地重新调整照片高度和宽度
finalPhotoWidth=self.frame.size.width;
最终照片高度=(photo.size.height/photo.size.width)*最终照片宽度;
}
if(最终照片高度