2011-05-04 1 views
3

pls는 주어진 이미지와 함께 다음 페이지에서 이미지의 입/출력을 고정하는 방법에 대한 논리를 제공합니다. width = 100 및 height = 150 이미지는 pintch in/out upto입니다. 해당 레벨을 수정하고 사용자가이 크기보다 크게 확대 된 다음 이미지가 그 너비, 높이로 고정되어 있으면 수정하십시오.아이폰 풍경의 확대/축소 이미지의 크기를 고정하는 방법은 무엇입니까?

#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 
+2

피곤할 수도 있지만 질문을 이해할 수 없습니다. 구두점을 추가 할 수 있습니까? – nash

+0

방금 ​​이미지를 확대하여 이미지를 확대 할 수 있도록 수정했습니다. 동적 폭 및 높이가 비슷합니다. –

+3

안녕하세요 저는 여기 있습니다. 귀하의 질문이 전혀 이해되지 않기 때문에 귀하가 무엇을 요구하는지 모르겠습니다. 이와 같은 의견은 개선을 위해 아무 것도하지 않으며, 사람들이 당신을 도우려고 시도하는 것을 막을 수있는 모든 것을 방해합니다. –

답변

0

핀치 거리에 대해이 작업을 시도해보십시오

- (CGFloat) distanceBetweenTwoPoints : (CGPoint) fromPoint 포인트 - 투 - 포인트 : (CGPoint) 포인트 - 투 - 포인트 { 플로트 lengthX = fromPoint.x - toPoint.x; 부유 길이 Y = fromPoint.y - toPoint.Y; 반환 SQRT ((lengthX * lengthX) + (긴) 긴 *);}

- (무효) touchesBegan : (NSSet *)는 withEvent 접촉 : (UIEvent를 *) 이벤트 { // --- 당신은 알아낼 }

// --- 발사 할 때 사용자 [touch2PT : touch1PT 투 - 포인트 자체 distanceBetweenTwoPoints] 기록 originalDistance = touches-- 견인에 의해 만들어진 거리 - 방법은 두 tap-- //를 감지 --- 화면에 자신의 손가락을 (를) 이동 - (무효)가있는 touchesMoved : (NSSet *)는 withEvent 접촉 : (UIEvent를 *) 이벤트 {

 //---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;}