平移缩放区域

最近有个需求:

将一张图片放到一个UIView上,图片可以缩放,可以平移,但是必须让UIView显示图片,不能让背景露出来,这样就需要在图片将要出去的时候,限制图片的平移或缩放

一、添加平移和缩放手势到UIView
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panggesture:)];
        pan.minimumNumberOfTouches = 2;
        [self addGestureRecognizer:pan];
        
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
        [self addGestureRecognizer:pinch];

  • 毕竟跟手势有关,肯定都会想到用transform来实现,那么约束方面,就是判断self.x == 0, self.y ==0 , self.x + self.width == Width , self.y + self.height == Heigh。
  • 当边界条件出现的时候,我们不改变transform就可以了,让后并没有这么简单。原因是UIPinchGestureRecognizer执行他的方法是有一定时间间隔的,对于边界的约束是致命的,这样很难约束住边界,让图片停在边界。
  • 后来我利用self. transform属性有6个值(1、4控制缩放,2、3控制旋转、5、6控制平移),当平移的时候,缩放是固定的,我通过固定的缩放值,来计算需要多大的平移才能出去边界,当出去边界的时候,我设置这个边界的transform,就可以约束住图片了。

- (void)panggesture:(UIPanGestureRecognizer *)pangesture{

    // 如果没有选中东西
    
        CGPoint position = [pangesture translationInView:self];
        // xxxxx
        if (self.x > -zero + self.contentSize.width / 2.0 && self.x< zero + self.contentSize.width / 2.0) {
            if (position.x > 0) {
                return;
            }
        }
        if (self.x > zero + self.contentSize.width / 2.0) {
            self.transform = CGAffineTransformMake(self.transform.a, self.transform.b, self.transform.c, self.transform.d, self.transform.a * self.contentSize.width / 2.0 , self.transform.ty);
            [self.delegate getTransForm:self.transform];
            return;
        }
        // yyyyy
        if (self.y > -zero + self.contentSize.height / 2.0 && self.y < zero + self.contentSize.height / 2.0) {
            if (position.y > 0) {
                return;
                
            }
        }
        if (self.y > zero + self.contentSize.height / 2.0) {
            self.transform = CGAffineTransformMake(self.transform.a, self.transform.b, self.transform.c, self.transform.d,self.transform.tx, self.transform.a * self.contentSize.height/2);
            [self.delegate getTransForm:self.transform];
            
            return;
            
        }
        // width
        if (self.x + self.width <  zero + self.contentSize.width / 2.0 && self.x + self.width >  -zero + self.contentSize.width / 2.0) {
            if (position.x < 0) {
                return;
            }
            
        }
        if (self.x + self.width <  - zero + self.contentSize.width / 2.0) {
            self.transform = CGAffineTransformMake(self.transform.a, self.transform.b, self.transform.c, self.transform.d,  - self.transform.a * self.contentSize.width / 2.0 , self.transform.ty);
            
            [self.delegate getTransForm:self.transform];
            
            return;
        }
        // height
        if (self.y + self.height <   zero + self.contentSize.height / 2.0 && self.y + self.height > self.contentSize.height - zero - self.contentSize.height / 2.0) {
            if (position.y < 0) {
                return;
            }
            
        }
        if (self.y + self.height < self.contentSize.height -zero - self.contentSize.height / 2.0) {
            self.transform = CGAffineTransformMake(self.transform.a, self.transform.b, self.transform.c, self.transform.d,self.transform.tx,  - self.transform.a * self.contentSize.height / 2.0 );
            [self.delegate getTransForm:self.transform];
            return;
        }
        
        // transform
        self.transform = CGAffineTransformTranslate(self.transform, position.x, position.y);
        [self.delegate getTransForm:self.transform];
        [pangesture setTranslation:CGPointZero inView:self];

    
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 10,519评论 5 13
  • Core Animation其实是一个令人误解的命名。你可能认为它只是用来做动画的,但实际上它是从一个叫做Laye...
    小猫仔阅读 9,210评论 1 4
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 12,712评论 6 30
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 7,303评论 1 14
  • 每一个咨询师都会有的和出租车司机的故事 早上下雨打伞,出门滴滴赶车 司机电话过来确定定位:你是在**路吗? 我回答...
    小黑鱼的真实世界阅读 1,807评论 0 5