简体   繁体   中英

How to animate uiimageview when changing device orientation

I have a single UIImageView which is used to display a background image for my app so takes up the whole screen, the image is changed between 1 of 4 images depending on the screen size/device (480points or 568points) & the orientation.

I've been searching to find if there is a way to animate the switching of the image between orientations as when the device is rotated currently the switch over is quite 'harsh' & briefly shows the white space behind.

Most of what I have found from google/here is people asking how to animate changing between uiimageviews which is not what I want, i just change the image that is displayed in the single uiimageview in code as in this snippet:

- (void)doLayoutForOrientation:(UIInterfaceOrientation)orientation {
if (IS_WIDESCREEN && UIInterfaceOrientationIsPortrait(orientation)) {
    self.backgroundImage.image = [UIImage imageNamed:@"iPhone5backgroundPortrait.png"];
    _backgroundImage.frame = CGRectMake(0, 0, 320, 568);
} if (IS_NOT_WIDESCREEN && UIInterfaceOrientationIsPortrait(orientation)) {
    self.backgroundImage.image = [UIImage imageNamed:@"iPhone4backgroundPortrait.png"];
    _backgroundImage.frame = CGRectMake(0, 0, 320, 480);
} if (IS_WIDESCREEN && UIInterfaceOrientationIsLandscape(orientation)) {
    self.backgroundImage.image = [UIImage imageNamed:@"iPhone5backgroundLandscape.png"];
    _backgroundImage.frame = CGRectMake(0, 0, 568, 320);
} else if (IS_NOT_WIDESCREEN && UIInterfaceOrientationIsLandscape(orientation)) {
    self.backgroundImage.image = [UIImage imageNamed:@"iPhone4backgroundLandscape.png"];
    _backgroundImage.frame = CGRectMake(0, 0, 480, 320);

I have tried the [animateImageNamed: duration:] method but when the device is rotated the background disappears!

thanks

Did you try changing the image in:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration

? Also, at some point prefetch the images so they are already in the image cache when the rotation happens - otherwise the first one may have a bit of delay.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM