UIImage is not transitioning using fade with animation block transition

不想你离开。 提交于 2019-12-12 01:49:34

问题


I am doing an image transition using an animation block like this

[UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    self.songTitleLabel.text = currentSong.songTitle;
    self.artistNameLabel.text = currentSong.songArtist;
    self.songImageView.image = currentSong.songArtwork;
}completion:^(BOOL finished){
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:currentSong.songLocation error:nil];
    [self.player setVolume:1.0];
    [self.player play];
}];

I don't understand why I don't have a fade transition, I have tried several different UIAnimationOptionTransition... and every time I just get an abrupt jump to the next image (this happens to the text as well)

Can someone help me out?


回答1:


You can't animate the changing of an image or text that way. I've done it like this,

-(IBAction)doStuff:(id)sender {

    [UIView transitionWithView:self.imageView
                      duration:1.0
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        [self.imageView setImage:[UIImage imageNamed:@"IMG_0081.JPG"]];
                    } completion:nil];

    [UIView transitionWithView:self.label2
                      duration:1.0
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        [self.label2 setText:@"New Text"];
                    } completion:nil];
}

If you're changing multiple labels or image views, it might be better to create a subclass, and override setText: and setImage:. For example, like this for setImage:,

-(void)setImage:(UIImage *)image {
    [UIView transitionWithView:self
                      duration:1.0
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                       [super setImage:image];
                    } completion:nil];
}

You can then just use,

self.imageView.image = ...

for any image view that's your subclass, and it will cross fade the images when you change them.



来源:https://stackoverflow.com/questions/24370695/uiimage-is-not-transitioning-using-fade-with-animation-block-transition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!