I have an UIImage with the following properties:
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imageView.contentMode = UIViewContentModeScaleAspectFit;
Later on I add a custom CALayer
to the imageview at a given position.
When I rotate my imageview the image is scaled to fit (given the contentMode), but now my CALayer
is positioned wrong. Apparently only the Y-position is wrong. How can I make my CALayer
reposition correctly when view is rotated?
As far as i know, in order to position/layout your custom layer, you should subclass your imageview and implement this method:
- (void) layoutSublayersOfLayer:(CALayer*)layer
{
[super layoutSublayersOfLayer:layer];
self.customlayer.position = CGPointMake(
self.bounds.size.width / 2, self.bounds.size.height / 2);
}
If you don't want to subclass imageview, you can try this way. This code should be in your imageview-controller:
- (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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.