pls give me the logic of how to fix the pinch size to / from the image on the next page with the given image .eg .... width = 100 and height = 150, then the image will be inserted / exited to this level and fixed there if the user is enlarged more than this size, and then the image will be fixed at this width, height.
#import "PinchLibView.h"
#import <QuartzCore/QuartzCore.h>
@implementation PinchLibView
@synthesize pinchImage;
@synthesize pinchScrlView;
@synthesize figureImageView;
@synthesize getWidth,getHeight;
-(void)initPinch{
figureRect = self.frame;
contentView = [[UIView alloc]initWithFrame:CGRectMake(0,0,
figureRect.size.width,
figureRect.size.height)];
pinchScrlView = [[CustomScrollView alloc]init];
figureImageView = [[UIImageView alloc]init];
pinchScrlView.minimumZoomScale =1.0;
pinchScrlView.maximumZoomScale = 4.0;
}
- (void)initScrollView{
self.userInteractionEnabled = YES;
figureImageView.contentMode = UIViewContentModeScaleAspectFit;
pinchScrlView.frame = CGRectMake(0,0,
figureRect.size.width,
figureRect.size.height);
[pinchScrlView setResetFrame:figureRect];
pinchScrlView.hidden = NO;
pinchScrlView.zoomScale = 1.0;
pinchScrlView.delegate = self;
pinchScrlView.showsHorizontalScrollIndicator = NO;
pinchScrlView.showsVerticalScrollIndicator = NO;
figureImageView.frame = CGRectMake(0,5,
figureRect.size.width,figureRect.size.height-10);
[contentView addSubview:figureImageView];
[pinchScrlView addSubview:contentView];
[self addSubview:pinchScrlView];
CATransition *animation = [CATransition animation];
animation.duration = 0.3f;
animation.type = kCATransitionFade;
pinchScrlView.zoomScale = 1.0;
[pinchScrlView.layer addAnimation: animation forKey: nil];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return contentView;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{
if(scale <= 1.0){
UIEdgeInsets anEdgeInset = { 0, 0, 0, 0};
pinchScrlView.contentInset = anEdgeInset;
pinchScrlView.scrlFlag = FALSE;
pinchScrlView.zoomScale = 0.0;
pinchScrlView.scrollEnabled = NO;
CATransition *animation = [ CATransition animation ];
animation.duration = 0.3f;
animation.type = kCATransitionFade;
self.frame = figureRect;
pinchScrlView.frame = figureRect;
[self.layer addAnimation: animation forKey: nil ];
[figureImageView.layer addAnimation: animation forKey: nil ];
contentView.frame = figureRect;
pinchScrlView.contentSize = figureRect.size;
contentView.center = CGPointMake(self.frame.size.width/2,
self.frame.size.height/2);
pinchScrlView.center = CGPointMake(self.frame.size.width/2,
self.frame.size.height/2);
}
if(pinchScrlView.zoomScale > 1.0 && !pinchScrlView.scrlFlag){
pinchScrlView.frame = self.superview.frame;
self.frame = self.superview.frame;
pinchScrlView.clipsToBounds = NO;
pinchScrlView.scrollEnabled = NO;
pinchScrlView.contentSize = pinchScrlView.frame.size;
pinchScrlView.scrlFlag = TRUE;
CGRect innerFrame = contentView.frame;
CGRect scrollerBounds =pinchScrlView.bounds;
if ( ( innerFrame.size.width < scrollerBounds.size.width ) || ( innerFrame.size.height < scrollerBounds.size.height ) )
{
CGFloat tempx = contentView.center.x - ( scrollerBounds.size.width / 2 );
CGFloat tempy = contentView.center.y - ( scrollerBounds.size.height / 2 );
CGPoint myScrollViewOffset = CGPointMake(tempx, tempy);
pinchScrlView.contentOffset = myScrollViewOffset;
}
else if ((innerFrame.size.width > scrollerBounds.size.width)
|| (innerFrame.size.height > scrollerBounds.size.height)){
if(innerFrame.size.width > scrollerBounds.size.width){
pinchScrlView.zoomScale = self.frame.size.width/figureRect.size.width;
}else if(innerFrame.size.height > scrollerBounds.size.height){
pinchScrlView.zoomScale = self.frame.size.height/figureRect.size.height;
}
CGFloat tempx = contentView.center.x - (scrollerBounds.size.width / 2 );
CGFloat tempy = contentView.center.y - (scrollerBounds.size.height / 2 );
CGPoint myScrollViewOffset = CGPointMake( tempx, tempy);
pinchScrlView.contentOffset = myScrollViewOffset;
}
UIEdgeInsets anEdgeInset = { 0, 0, 0, 0};
if(scrollerBounds.size.width > innerFrame.size.width)
{
anEdgeInset.left = (scrollerBounds.size.width - innerFrame.size.width) / 2;
anEdgeInset.right = -anEdgeInset.left;
}
if(scrollerBounds.size.height > innerFrame.size.height)
{
anEdgeInset.top = (scrollerBounds.size.height - innerFrame.size.height) / 2;
anEdgeInset.bottom = -anEdgeInset.top;
}
pinchScrlView.contentInset = anEdgeInset;
}
if(pinchScrlView.zoomScale > 1.0){
CGRect innerFrame = contentView.frame;
CGRect scrollerBounds = pinchScrlView.bounds;
if((innerFrame.size.width < scrollerBounds.size.width)
|| (innerFrame.size.height < scrollerBounds.size.height)){
CGFloat tempx = contentView.center.x - (scrollerBounds.size.width / 2 );
CGFloat tempy = contentView.center.y - (scrollerBounds.size.height / 2 );
CGPoint myScrollViewOffset = CGPointMake( tempx, tempy);
pinchScrlView.contentOffset = myScrollViewOffset;
}
else if((innerFrame.size.width > scrollerBounds.size.width)
|| (innerFrame.size.height > scrollerBounds.size.height)){
if(innerFrame.size.width > scrollerBounds.size.width){
pinchScrlView.zoomScale = self.frame.size.width/figureRect.size.width;
}else if(innerFrame.size.height > scrollerBounds.size.height){
pinchScrlView.zoomScale = self.frame.size.height/figureRect.size.height;
}
CGFloat tempx = contentView.center.x - (scrollerBounds.size.width / 2 );
CGFloat tempy = contentView.center.y - (scrollerBounds.size.height / 2 );
CGPoint myScrollViewOffset = CGPointMake( tempx, tempy);
pinchScrlView.contentOffset = myScrollViewOffset;
}
UIEdgeInsets anEdgeInset = { 0, 0, 0, 0};
if (scrollerBounds.size.width > innerFrame.size.width){
anEdgeInset.left = (scrollerBounds.size.width - innerFrame.size.width) / 2;
anEdgeInset.right = -anEdgeInset.left;
}
if (scrollerBounds.size.height > innerFrame.size.height){
anEdgeInset.top = (scrollerBounds.size.height - innerFrame.size.height) / 2;
anEdgeInset.bottom = -anEdgeInset.top;
}
pinchScrlView.contentInset = anEdgeInset;
}
}
@end
user728751
source
share