UIImageView subclass needs to handle resize

2019-07-07 02:45发布

问题:

I am creating a UIImageView subclass to display an audio waveform. The approach is to load the file, do math, save a PNG file and then self.image = thePNG. The nice part about this is that on a resize or repaint the UIImageView will stretch the PNG and stretch quickly.

Now if the image is expanded too much then I need to recalculate the waveform to avoid visible pixelation. Since we know that UIImageView does not call drawRect, is there a method that is called during resize so that I can decide if redrawing is necessary?

P.S. When recalculating I will be fading in the new image after it is calculated. Hopefully this will be seamless to the user like Google Earth.

回答1:

Here's a solution:

- (void)layoutSubviews
{
    [super layoutSubviews];
    NSLog(@"%@", NSStringFromCGRect(self.frame));
}

The trade off with this is that it is not called for each frame of an animation, as pointed out by David Jeske in Is there a UIView resize event?


Other ideas that didn't work:

  • Override setFrame:(CGRect)frame discussed at Is there a UIView resize event?

  • Listen for key-value observation, discussed at https://groups.google.com/forum/#!topic/ococoa/UojAu8rclwo