據我所知,爲了位置/佈局定製層,你應該繼承您的ImageView和實施此方法:
- (void) layoutSublayersOfLayer:(CALayer*)layer
{
[super layoutSublayersOfLayer:layer];
self.customlayer.position = CGPointMake(
self.bounds.size.width/2, self.bounds.size.height/2);
}
如果你不想繼承ImageView的,你可以試試這條路。 此代碼應該位於您的imageview控制器中:
- (void) viewDidLoad
{
[super viewDidLoad];
self.container = [CALayer layer];
self.container.bounds = self.imageview.bounds;
self.container.delegate = self;
[self.container addSublayer:self.customlayer];
[self.imageview.layer addSublayer:self.container];
}
- (void) layoutSublayersOfLayer:(CALayer*)layer // this is layers delegate method
{
if (layer == self.container)
{
self.customlayer.position = CGPointMake(self.container.bounds.size.width/2,
self.container.bounds.size.height/2);
}
}