IOS學習筆記41--圖片的縮放(一)

來源:互聯網
上載者:User

圖片的縮放
 一:Pinch手勢對圖片進行縮放。即用兩根手指往不同方向拖拉照片,照片會被縮小或放大。
我理解的原理:等比縮放

先看如下關鍵代碼:

1.初始化參數


- (void)viewDidLoad

{

    [superviewDidLoad];

   

   lastDistance = 0.0;

   imageStartHeight = self.scaleImage.frame.size.height;

   imageStartWidth = self.scaleImage.frame.size.width;

}

 

2.縮放操作


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

   

   CGPoint point1; //Point

   CGPoint point2;

   

   CGFloat sub_x; //兩手指間的 X距離

   CGFloat sub_y;//兩手指間的 Y距離

   

   CGFloat currentDistance; //當前兩手機間的距離

   CGRect imageFrame; //獲得活動範圍的frame

   

   NSArray *touchArray = [[event allTouches]allObjects];

   

   if ([touchArray count] >= 2) {

       

        point1 = [[touchArrayobjectAtIndex:0]locationInView:self.view];

        point2 = [[touchArrayobjectAtIndex:1]locationInView:self.view];

       

        sub_x = point1.x-point2.x;

        sub_y = point1.y-point2.y;

       

        currentDistance =sqrtf(sub_x * sub_x + sub_y * sub_y);

       

           

       if (lastDistance >0)

        {

            imageFrame =self.scaleImage.frame;

           

           if (currentDistance > lastDistance +2)

            {

//                NSLog(@"放大");

                imageFrame.size.width +=10;

               if (imageFrame.size.width >1000)

                {

                    imageFrame.size.width =1000;

                }

               lastDistance = currentDistance;

            }

           

           if (currentDistance < lastDistance -2)

            {

//                NSLog(@"縮小");

                imageFrame.size.width -=10;

               if (imageFrame.size.width <50)

                {

                    imageFrame.size.width =50;

                }

               lastDistance = currentDistance;

            }

           NSLog(@"currentDistance :%f  lastDistance : %f",currentDistance,lastDistance);

           if (currentDistance == lastDistance) {

               

                imageFrame.size.height =imageStartHeight*imageFrame.size.width/imageStartWidth;

               

               float addwidth = imageFrame.size.width -self.scaleImage.frame.size.width;

               float addheight = imageFrame.size.height -self.scaleImage.frame.size.height;

               

self.scaleImage.frame =CGRectMake(imageFrame.origin.x - addwidth/2.0f, imageFrame.origin.y - addheight/2.0f, imageFrame.size.width, imageFrame.size.height);

            }

        }

       else{

           

           lastDistance = currentDistance;

        }

    }

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

   lastDistance = 0;

}

其實他的關鍵所在就在於:判斷兩手指間的距離,當大於一定的距離的時候就對圖片的frame進行等比縮放,以達到縮放的目的。

有其他見解的同學留言討論。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.