Mc-
Mc-

Reputation: 4056

Weird behavior moving CALayer via setPosition

I am trying to move a set of CALayers according to the Pan gesture.

I'm using this code:

-(void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {
    float newX = 0;  

    for (CALayer *layer in self.layer.sublayers) 
    {        
        [ layer setPosition:CGPointMake(layer.frame.origin.x + 50, layer.frame.origin.y)];
    }
}

But the CALayers are moving all towards the side of the screen

My question is, Am I doing something wrong?

Are CALayers not supposed to be moved this way?

Thanks!

Upvotes: 0

Views: 1732

Answers (1)

David Rönnqvist
David Rönnqvist

Reputation: 56625

The position is actually in the center and not in the origin (unless you have changed the anchor point but I'm assuming that you haven't).

If you want the layers to move 50 points to the right then you should change your positions to the center + 50. Like this:

[layer setPosition:CGPointMake(layer.position.x + 50, layer.position.y)];

However...

You are never using the value of the pan gesture recognizer so for every pan event you will move every layer 50 points to the right. If this is what you mean by "the CALayers are moving all towards the side of the screen" then this is what you are doing wrong. The layers are moving just like you told them.

Upvotes: 1

Related Questions