IOS学习笔记41--图片的缩放(一)

图片的缩放

 一: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进行等比缩放,以达到缩放的目的。

有其他见解的同学留言讨论。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值