如何确定iphone环境中放大/缩小图像的大小?

如何确定iphone环境中放大/缩小图像的大小?,iphone,ios4,iphone-sdk-3.0,ios-simulator,cocos2d-iphone,Iphone,Ios4,Iphone Sdk 3.0,Ios Simulator,Cocos2d Iphone,请给我提供如何在下一页用给定图像固定插针进/出图像大小的逻辑。例如,宽度=100,高度=150,则图像是插针进/出到该级别,并在那里固定,如果用户放大超过此大小,则图像也固定到该宽度,高度 #import "PinchLibView.h" #import <QuartzCore/QuartzCore.h> @implementation PinchLibView @synthesize pinchImage; @synthesize pinchScrl

请给我提供如何在下一页用给定图像固定插针进/出图像大小的逻辑。例如,宽度=100,高度=150,则图像是插针进/出到该级别,并在那里固定,如果用户放大超过此大小,则图像也固定到该宽度,高度

#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;
            //NSLog(@"%d %d",appDelegate.width1,appDelegate.height1);
            //getWidth=[appDelegate.width1 intValue];
            //getHeight=[appDelegate.height1 intValue];
            //NSLog(@"%d %d",getWidth,getHeight);
        }

        - (void)initScrollView{
            //figureImageView = [[UIImageView alloc]init];
            self.userInteractionEnabled = YES;
            //figureImageView.image = pinchImage;
             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];

            /*pinchScrlView.backgroundColor = [UIColor yellowColor];
            contentView.backgroundColor = [UIColor yellowColor];
            figureImageView.backgroundColor = [UIColor redColor];*/
            /*pinchScrlView.backgroundColor = [UIColor whiteColor];
            contentView.backgroundColor = [UIColor whiteColor];
            figureImageView.backgroundColor = [UIColor whiteColor]; */

        }

        - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
                return contentView;
        }


        - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{

            /*  Reset Image if scale in less than   1.0     */
            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);
        }   

        /*  Make image centered if scale in greater than 1.0  */
        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;
            //CGRect scrollerBounds =CGRectMake(50,50,appDelegate.width1,appDelegate.height1);
            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;
            //CGRect scrollerBounds =CGRectMake(50,50,appDelegate.width1,appDelegate.height1);

            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
#导入“PinchLibView.h”
#进口
@实现PinchLibView
@综合钳位;
@合成pinchsrlview;
@综合图形图像视图;
@综合getWidth、getHeight;
-(无效)无效{
Figure直立=自身框架;
contentView=[[UIView alloc]initWithFrame:CGRectMake(0,0,
图1.尺寸、宽度,
图(尺寸、高度);
pinchScrlView=[[CustomScrollView alloc]init];
figureImageView=[[UIImageView alloc]init];
pinchScrlView.minimumZoomScale=1.0;
pinchScrlView.maximumZoomScale=4.0;
//NSLog(@“%d%d”,appDelegate.width1,appDelegate.height1);
//getWidth=[appDelegate.width1 intValue];
//getHeight=[appDelegate.height1 intValue];
//NSLog(@“%d%d”,getWidth,getHeight);
}
-(void)初始化滚动视图{
//figureImageView=[[UIImageView alloc]init];
self.userInteractionEnabled=是;
//figureImageView.image=pinchImage;
figureImageView.contentMode=UIViewContentModeScaleAspectFit;
pinchScrlView.frame=CGRectMake(0,0,
图1.尺寸、宽度,
图形(竖立、大小、高度);
[pinchScrlView setResetFrame:Figure];
pinchScrlView.hidden=否;
pinchScrlView.zoomScale=1.0;
pinchScrlView.delegate=self;
pinchScrlView.showshorizontalscrolindicator=否;
pinchScrlView.showsVerticalScrollIndicator=否;
figureImageView.frame=CGRectMake(0,5,
图竖立。尺寸。宽度,图竖立。尺寸。高度-10);
[contentView添加子视图:figureImageView];
[pinchScrlView添加子视图:contentView];
[自添加子视图:pinchScrlView];
CATTransition*动画=[CATTransition动画];
animation.duration=0.3f;
animation.type=kCATransitionFade;
pinchScrlView.zoomScale=1.0;
[pinchScrlView.layer addAnimation:animation forKey:nil];
/*pinchScrlView.backgroundColor=[UIColor yellowColor];
contentView.backgroundColor=[UIColor yellowColor];
figureImageView.backgroundColor=[UIColor redColor]*/
/*pinchScrlView.backgroundColor=[UIColor whiteColor];
contentView.backgroundColor=[UIColor whiteColor];
figureImageView.backgroundColor=[UIColor whiteColor]*/
}
-(UIView*)视图用于缩放CrollView:(UICrollView*)滚动视图{
返回contentView;
}
-(无效)ScrollViewDiEndZooming:(UIScrollView*)带视图的scrollView:(UIView*)按比例查看:(浮动)比例{
/*如果比例小于1.0,则重置图像*/
if(比例1.0&!pinchScrlView.scrlFlag){
pinchScrlView.frame=self.superview.frame;
self.frame=self.superview.frame;
pinchScrlView.clipsToBounds=否;
pinchScrlView.scrollEnabled=否;
pinchsrlview.contentSize=pinchsrlview.frame.size;
pinchScrlView.scrlFlag=TRUE;
CGRect innerFrame=contentView.frame//////////
CGRect scrollerBounds=pinchScrlView.bounds;
//CGRect scrollerBounds=CGRectMake(50,50,appDelegate.width1,appDelegate.height1);
if((innerFrame.size.widthscrollerBounds.size.width)
||(innerFrame.size.height>scrollerBounds.size.height){
if(innerFrame.size.width>scrollerBounds.size.width){
pinchScrlView.zoomScale=self.frame.size.width/figurpright.size.width;
}else if(innerFrame.size.height>scrollerBounds.size.height){
pinchScrlView.zoomScale=self.frame.size.height/figure.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=一个dgeInset;
}
如果(pinchScrlView.zoomScale>1.0){
CGRect innerFrame=contentView.frame;
        //---you figure out a way to detect double tap--
        //---double-touch---
        //---get info of first touch---
        UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];

        //---get info of second touch---
        UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];

        //---get the points touched---
        CGPoint touch1PT = [touch1 locationInView:[self view]];
        CGPoint touch2PT = [touch2 locationInView:[self view]];

        CGFloat currentDistance = [self distanceBetweenTwoPoints:touch1PT
                                                         toPoint:touch2PT];

        //---zoom in---
        if (currentDistance > originalDistance) {
            imageView.frame = CGRectMake(imageView.frame.origin.x - 2,
                                         imageView.frame.origin.y - 2,
                                         imageView.frame.size.width + 4,
                                         imageView.frame.size.height + 4);
        }
        else {
            //---zoom out---
            imageView.frame = CGRectMake(imageView.frame.origin.x + 2,
                                         imageView.frame.origin.y + 2,
                                         imageView.frame.size.width - 4,
                                         imageView.frame.size.height - 4);
        }
        originalDistance = currentDistance;}