代码之家  ›  专栏  ›  技术社区  ›  jps

CGaffinetTransformMakeScale使uiView在缩放前跳转到原始大小

  •  12
  • jps  · 技术社区  · 14 年前

    我有一个ui视图,我设置它来响应捏手势并更改其大小,但是,一旦你放大它然后再次尝试捏它,它就会跳到原来的大小(正好是100x200)。代码如下:

    @implementation ChartAxisView
    
    
    - (id)initWithFrame:(CGRect)frame {
        if ((self = [super initWithFrame:frame])) {
            // do gesture recognizers
            UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(onPinch:)]; 
            [self addGestureRecognizer:pinch]; 
            [pinch release];
        }
        return self;
    }
    
    - (void)drawRect:(CGRect)rect {
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        CGColorRef redColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0].CGColor;
    
        CGContextSetFillColorWithColor(context, redColor);
        CGContextFillRect(context, self.bounds);
    }
    
    - (void)onPinch: (UIPinchGestureRecognizer*) gesture {  
        self.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
    }
    
    - (void)dealloc {
        [super dealloc];
    }
    
    
    @end
    

    有什么想法吗?

    3 回复  |  直到 11 年前
        1
  •  27
  •   Mihir Mathuria    14 年前

    因此,有两种类型的缩放(或一般的转换)功能: CGAffineTransformMakeScale CGAffineTransformScale

    第一个, CGAffineTransformMakeScale 您使用的,总是根据图像的原始大小进行转换。这就是为什么在缩放发生之前,您会看到跳转到它的原始大小。

    第二个, CGAffineTransformScale ,从图像的当前位置转换。这就是你需要的。为此,它需要一个额外的“transform”参数。您案例中的“transform”参数表示放大的图像。

    this 关于转换的信息非常丰富的博客文章。

        2
  •  2
  •   elp jekmac    12 年前
    - (void)onPinch: (UIPinchGestureRecognizer*) gesture {  
    
      if ([gesture state] == UIGestureRecognizerStateBegan) 
      {
        curTransform = self.transform;
      }
    
      self.transform = CGAffineTransformScale(curTransform,gesture.scale, gesture.scale);
    }
    
    • 使用CGaffinetTransformScale而不是CGaffinetTransformMakeScale
    • 您需要一个成员->cgaffinettransform curtransform;

    ;)

        3
  •  2
  •   ygweric    11 年前

    可以使用以下代码设置转换:

    ivClone setTransform:CGAffineTransformMakeScale(scale, scale)];
    

    您可以使用以下COEE获得当前转换:

    newV.transform.a //x scale
    newV.transform.d // y scale
    
    推荐文章