CALayer - 在保持蒙版静态的同时翻译图像

时间:2014-02-21 10:43:39

标签: ios core-graphics mask

我有一个包含图像的CALayer,并且顶部有一个遮罩。

  

CALayer * firstLayer = [CALayer layer];

firstLayer.backgroundColor = [UIColor clearColor].CGColor;
firstLayer.frame =  curveView.frame;
firstLayer.position = CGPointMake(curveView.frame.origin.x + 100, curveView.frame.origin.y + 100);
firstLayer.name = @"FirstLayer";
[self.view.layer addSublayer:firstLayer];

CALayer *firstImageLayer = [CALayer layer];
firstImageLayer.frame = firstLayer.bounds;
firstImageLayer.masksToBounds = NO;   
firstImageLayer.name = @"FirstImageLayer";
firstImageLayer.contents = (id)first.CGImage;
[firstLayer addSublayer:firstImageLayer];
     

firstImageLayer.mask = [cM setMaskOnLayer:firstImageLayer   :@" FirstColor" :&安培;错误];

我希望翻译/平移/拖动基础图层中包含的图像,同时保持蒙版固定。 (有点像从固定窗口内查看图像)

  for (CALayer *subLayer in firstLayer.sublayers) {

    // p is the touch coordinate

/*First Thing I tried*/          
CAShapeLayer *mask = (CAShapeLayer *)subLayer.mask;
 mask.position = p;
/*Moves the mask, not the image*/


/*My second approach*/
     subLayer.frame = CGRectMake(subLayer.frame.origin.x + p.x, subLayer.frame.origin.y+p.y, subLayer.frame.size.width, subLayer.frame.size.height);
/*Moves the entire thing… image+mask - both*/


/*Final desperate approach*/
             for (CALayer *subLayer2 in subLayer.sublayers)
             {
                 sublayer2.position = p;
             }
/*Nothing happens*/
 }

帮助我。

1 个答案:

答案 0 :(得分:1)

您是否尝试过将ImageLayer的逆变换应用于MaskLayer?

CALayer* imageLayer = ...;
 CALayer* maskLayer = ...;
 CGAffineTransform inverse = CGAffineTransformInvert(imageLayer.affineTransform);
 maskLayer.affineTransfrom = inverse; //assign everytime imageLayer move or change