简体   繁体   English

如何删除UIView transitionWithView的影响

[英]How to remove the effects of UIView transitionWithView

Using the code below I have successfully made my UIImageView fade in its image. 使用下面的代码,我成功地使UIImageView的图像褪色。

[UIView transitionWithView:myCell.myImageView duration:2.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    myCell.myImageView.image = myImage;
} completion:NULL];

However, when the cell is reused I need to remove this image. 但是,当单元被重用时,我需要删除该图像。 When reused it shows the last image(shown before it was reused) and then transitions into the image it is supposed to be, instead of transitioning from no image into the image it is supposed to be. 当重用时,它显示最后一个图像(在重用之前显示),然后过渡到应该的图像,而不是从没有图像过渡到应该的图像。

What I have tried: 我试过的

-(void)prepareForReuse {
    [super prepareForReuse];

// More is obviously needed than just setting image to nil when using a transition
self.myImageView.image = nil;

[self.myImageView setNeedsDisplay];

// but I also tried removing all animations
[self.myImageView.layer removeAllAnimations];

// and I even tried removing the sublayers, but still no success.
[self.myImageView.layer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
}

Any help is appreciated, but please don't answer if you don't understand what the issue is. 非常感谢您的帮助,如果您不了解问题所在,请不要回答。 Comment instead so I can explain better if needed. 请发表评论,以便在需要时可以更好地解释。

Thanks! 谢谢!

You could make use of the callback 您可以利用回调

tableView:didEndDisplayingCell:forRowAtIndexPath:

This is called is just as the cell goes outside the visible area. 就像细胞进入可见区域之外一样,这被称为。
Try setting the image to nil or no image over here. 尝试将图像设置为nil或此处没有图像。 Should work. 应该管用。

If you really need to use transitionWithView , then here's how to do it. 如果您确实需要使用transitionWithView ,那么这里是操作方法。

First, add the following function to your UITableViewCell class: 首先,将以下函数添加到您的UITableViewCell类中:

-(void)fadeInImage
{
    self.ivPadlock.alpha = 0.0;

    //  Wait a fraction of a second...
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{

        //  ..then perform the "transitionWithView" on a seperate thread
        [UIView transitionWithView:self.ivPadlock duration:2.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{

            //  Replace our UIImageView's image with an alternative image
            int i = rand()%5;
            switch (i)
            {
                case 0: self.ivPadlock.image = [UIImage imageNamed:@"icnPaperclip.png"]; break;
                case 1: self.ivPadlock.image = [UIImage imageNamed:@"icnSubscribed.png"]; break;
                case 2: self.ivPadlock.image = [UIImage imageNamed:@"icnGroups.png"]; break;
                case 3: self.ivPadlock.image = [UIImage imageNamed:@"icnCross.png"]; break;
                case 4: self.ivPadlock.image = [UIImage imageNamed:@"icnCamera.png"]; break;
            }
            self.ivPadlock.alpha = 1.0;

        } completion:NULL];
    });
}

And then, make sure this function gets called whenever a cell is created or reused: 然后,确保在创建或重用单元格时调用此函数:

- (void)awakeFromNib {
    // Initialization code
    [self fadeInImage];
}

-(void)prepareForReuse
{
    [super prepareForReuse];

    [self fadeInImage];
}

Btw, as you're wanting to always transition from "nothing" into your new image, wouldn't it make life easier to just use animateWithDuration to fade an image onto your cell ? 顺便说一句,由于您希望始终从“无”过渡到新图像,仅使用animateWithDuration将图像淡入您的单元格是否会使生活更轻松?

-(void)prepareForReuse
{
    [super prepareForReuse];

    self.ivPadlock.alpha = 0.0;
    self.ivPadlock.hidden = false;

    int i = rand()%5;
    switch (i)
    {
        case 0: self.ivPadlock.image = [UIImage imageNamed:@"icnPaperclip.png"]; break;
        case 1: self.ivPadlock.image = [UIImage imageNamed:@"icnSubscribed.png"]; break;
        case 2: self.ivPadlock.image = [UIImage imageNamed:@"icnGroups.png"]; break;
        case 3: self.ivPadlock.image = [UIImage imageNamed:@"icnCross.png"]; break;
        case 4: self.ivPadlock.image = [UIImage imageNamed:@"icnCamera.png"]; break;
    }

    [UIView animateWithDuration:2.0 animations:^{
        self.ivPadlock.alpha = 1.0;
    }];
}

Hope this helps. 希望这可以帮助。

[UIView transitionWithView:myCell.myImageView duration:2.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    myCell.myImageView.image = myImage;
} completion:^(BOOL finished) {
                        [self.myImageView.layer removeAllAnimations];   
                         }];

In my case, I tried to solve my problem similar with above code. 就我而言,我试图解决与上述代码相似的问题。 This may solve your problem. 这样可以解决您的问题。 A block object(completion) to be executed when the animation sequence ends. 动画序列结束时要执行的块对象(完成)。

Try this: 尝试这个:

-(void)prepareForReuse {
   [super prepareForReuse];

   [CATransaction begin];
   [self.myImageView.layer removeAllAnimations];
   self.myImageView.image = nil;
   [CATransaction commit];

}

Thank you for trying, but none of the answers fixes the problem. 感谢您的尝试,但是所有答案都不能解决问题。 I have also tried combinations of the different answers. 我还尝试了不同答案的组合。 Also, none of them satisfy "Looking for an answer drawing from credible and/or official sources.". 此外,他们都不满意“正在寻找来自可靠和/或官方来源的答案”。 I have contacted Apple Developer Technical Support and will edit this answer when I get a response from them so that it will help others as well. 我已与Apple开发人员技术支持联系,并将在收到他们的答复后编辑此答案,以便对他人也有所帮助。

  1. Try using [self.myImageView.image setNeedsDisplay]; 尝试使用[self.myImageView.image setNeedsDisplay]; after you have set the image to nil 将图像设置为nil后
  2. You need not remove the sublayers 您无需删除子层
  3. Before you show the animation, as a worst case, you could force the image view to nil with an selector call from 'Cell for row at indexpath' 在显示动画之前,在最坏的情况下,您可以通过“索引路径行的单元格”中的选择器调用将图像视图强制为零

I want to comment, but lost my old login and answering instead :) 我想发表评论,但丢失了旧的登录名,而是无法回答:)

edit ----------------- latest update below 编辑-----------------下面的最新更新

So I retried emulating the bug in my code, below are steps followed 1. Use default tableview and try to fade in its own imageview (available at cell.imageView) 2. Any no. 因此,我尝试在代码中模拟错误,请按照以下步骤操作:1.使用默认的tableview并尝试淡入其自己的imageview(可从cell.imageView获取)。2.否。 of combinations with [UIView transitionWithView..] didn't help my case. [UIView transitionWithView ..]的组合对我的情况没有帮助。 Instead I set the image (with no transitions) in [cellForRowatIndexPath] and used below fade in code only in [willDisplayCell forRowAtIndexPath] 相反,我在[cellForRowatIndexPath]中设置了图像(无过渡),并在下面的淡入淡出中仅在[willDisplayCell forRowAtIndexPath]中使用了代码

cell.imageView.alpha = 0;
[UIView beginAnimations:@"fade in" context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDuration:2.0];
cell.imageView.alpha = 1.0;
[UIView commitAnimations];

and it worked fine. 而且效果很好。 Can you try this once, might work for you. 您可以尝试一次吗,可能对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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