如何删除UIView transitionWithView的效果

时间:2015-02-16 06:09:45

标签: ios objective-c uiview calayer

使用下面的代码我已经成功地使我的UIImageView在其图像中淡入淡出。

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

但是,当重复使用单元格时,我需要删除此图像。 重复使用时,它显示最后一个图像(在重复使用之前显示),然后转换到它应该是的图像,而不是从没有图像转换到它应该是的图像。

我尝试过:

-(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)];
}

感谢任何帮助,但如果您不明白问题是什么,请不要回答。反而注释,以便我可以在需要时更好地解释。

谢谢!

6 个答案:

答案 0 :(得分:4)

您可以使用回调

tableView:didEndDisplayingCell:forRowAtIndexPath:

这被称为就像细胞走出可见区域一样。
尝试在此处将图像设置为零或无图像。应该工作。

答案 1 :(得分:1)

如果你真的需要使用transitionWithView,那么这里是如何做到的。

首先,将以下函数添加到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];
    });
}

然后,确保在创建或重用单元格时调用此函数:

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

-(void)prepareForReuse
{
    [super prepareForReuse];

    [self fadeInImage];
}

顺便说一句,因为您希望始终从“无”转换到新图像中,是否只会使用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;
    }];
}

希望这有帮助。

答案 2 :(得分:0)

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

在我的情况下,我尝试用上面的代码解决我的问题。 这可以解决您的问题。动画序列结束时要执行的块对象(完成)。

答案 3 :(得分:0)

试试这个:

-(void)prepareForReuse {
   [super prepareForReuse];

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

}

答案 4 :(得分:0)

感谢您的尝试,但没有一个答案可以解决问题。我也尝试过不同答案的组合。此外,它们都不满足“寻找可信和/或官方来源的答案”。 我已经联系了Apple开发者技术支持部门,当我得到他们的回复时,我会编辑这个答案,以便它也能帮助其他人。

答案 5 :(得分:-1)

  1. 尝试使用[self.myImageView.image setNeedsDisplay];将图像设置为nil后
  2. 您无需删除子图层
  3. 在您展示动画之前,作为最坏的情况,您可以使用来自' Cell的选择器调用将图像视图强制为nil
  4. 我想发表评论,但遗失了旧的登录信息并回答:)

    编辑-----------------下面的最新更新

    所以我重新尝试在我的代码中模拟错误,下面是遵循的步骤 1.使用默认的tableview并尝试淡入其自己的imageview(在cell.imageView上可用) 任何不。与[UIView transitionWithView ..]的组合对我的情况没有帮助。相反,我在[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];
    

    它工作正常。你可以尝试一次,可能适合你。