开发者

CALayer coordinates erratic behaviour

I am having some troubles for a while with CALayers. I create a layer that I add to a UIImageView. I want this layer to get the same position in the screen that another view, as if it was a copy of it. But as the layer is 开发者_Go百科added to the UIImageView, its coordinates are the same in NSLog as the "copied" view, but the layer position is never the same, each time I try I see something different. I should be missing something.

    CALayer*layer=[CALayer layer];
        layer.bounds =copiedImageView.bounds;
        layer.contents=copiedImageView.layer.contents;


       CGPoint theCenter=[targetImageView convertPoint:copiedImageView.center fromView:self.view];            
       [layer setPosition:theCenter];    

        [targetImageView.layer addSublayer:layer];
        copiedImageView.layer.hidden=YES;

If I set the layer.position explicitly it seems to work (ex. to (0,0) the layer center is always on the upper left corner of the targetImageView), which is logical.

Any help is very much appreciated!


Set the frame, not the bounds. Frame determines how a layer is positioned relative to its parent layer.

layer.frame =copiedImageView.frame;

Unless you have a transform applied. Then you are doing it approximately the correct way, by separately setting bounds and position.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜