潜望镜式评论的快速动画

时间:2016-12-25 20:29:51

标签: ios swift swift2 uiviewanimation cgaffinetransform

我在iOS应用中使用此代码进行潜望镜式评论(评论气泡从底部向上滑动):https://github.com/yoavlt/PeriscommentView

这是实际动画评论的代码:

public func addCell(cell: PeriscommentCell) {
        cell.frame = CGRect(origin: CGPoint(x: 0, y: self.frame.height), size: cell.frame.size)
        visibleCells.append(cell)
        self.addSubview(cell)

        UIView.animateWithDuration(self.config.appearDuration, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in
            let dy = cell.frame.height + self.config.layout.cellSpace
            for c in self.visibleCells {
                let origin = c.transform
                let transform = CGAffineTransformMakeTranslation(0, -dy)
                c.transform = CGAffineTransformConcat(origin, transform)
            }
        }, completion: nil)

        UIView.animateWithDuration(self.config.disappearDuration, delay: self.config.stayDuration, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
            cell.alpha = 0.0
        }) { (Bool) -> Void in
            self.visibleCells.removeLast()
            cell.removeFromSuperview()

        }
    }

上述代码的问题在于,有时在添加新评论时,它会显示与先前评论重叠。预期的行为是前一个评论向上滑动,新评论取而代之。我注意到这主要发生在上一条评论开始淡出后添加新评论时,但仍然没有消失。

我尝试在self.visibleCells.removeLast()中放置一个断点,看起来只有当最后一条注释完成后才会调用它,所以我希望这能正常工作(因为for循环向上移动)可见的细胞,即使评论淡出,它仍然是可见的。

对此有任何帮助将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

我刚刚获得了该存储库的克隆,在我的设备上运行,您的问题是尝试重写该功能。不要那样做。相反,只需添加以下行:

@IBAction func addNewCell(sender: UIButton) {
    self.periscommentView.addCell(UIImage(named: "twitterProfile")!, name: "Your_Name_Here", comment: "Your_Comment_Here")
}

这就是全部!我刚检查过它,效果很好!

不要试图改变alpha或向上移动。图书馆完成了所有这些工作!祝好运! ;)