06 Feb 2010, 17:13
Bathyjb1_pragsmall

Ronald Bell (11 posts)

The code in video 1 isn’t quite right. It works fine as-is, but it assumes the anchorPoint = (0.5, 0.5). Change the anchorPoint to (0.0, 0.0) or (1.0, 1.0) and the sublayer no longer positions correctly when moved, or when initially added.

To make it more general, change awakeFromNib as follows (first set the anchorPoint before position, btw):

sublayer.anchorPoint = CGPointMake(0.5f, 0.5f);
sublayer.position = CGPointMake(NSMidX(self.bounds) + (sublayer.anchorPoint.x - 0.5) * sublayer.bounds.size.width, 
						  NSMidY(self.bounds) + (sublayer.anchorPoint.y - 0.5) * sublayer.bounds.size.height);

Change moveLeft as follows:

CGPoint newPosition = 
	CGPointMake(sublayer.anchorPoint.x * CGRectGetWidth(sublayer.bounds), 
				NSMidY(self.bounds) + (sublayer.anchorPoint.y - 0.5) * sublayer.bounds.size.height);

Change moveRight as follows:

CGPoint newPosition = 
	CGPointMake(NSWidth([self bounds]) - ((1-sublayer.anchorPoint.x) * CGRectGetWidth(sublayer.bounds)), 
		NSMidY(self.bounds) + (sublayer.anchorPoint.y - 0.5) * sublayer.bounds.size.height);

Change moveUp as follows:

CGPoint newPosition = 
	CGPointMake(NSMidX(self.bounds) + (sublayer.anchorPoint.x - 0.5) * sublayer.bounds.size.width, 
				NSHeight(self.bounds) - (1-sublayer.anchorPoint.y) * CGRectGetHeight(sublayer.bounds));

Change moveDown as follows:

CGPoint newPosition = 
	CGPointMake(NSMidX(self.bounds) + (sublayer.anchorPoint.x - 0.5) * sublayer.bounds.size.width, 
				sublayer.anchorPoint.y * CGRectGetHeight(sublayer.bounds));

Now change the anchorPoint wherever you want, and it positions properly.

Cheers

  You must be logged in to comment