tableViewCell的动画效果
看到很多APP的tableViewCel在从屏幕外进入屏幕的时候会有很炫的动画,就找资料写了一个demo,非常炫酷,也非常实用
- 先看下实现的效果吧,有两种动画效果
- 下面开始正式内容 1.创建工程,添加2个ViewController,实现NavigationController跳转 2.红色cell的动画效果,需要重写viewWillAppear方法,在viewWillAppear方法中将所有的cell移动到屏幕外边,并且设置动画
override func viewWillAppear(animated: Bool) {
// tableView重新加载视图
self.tableView.reloadData()
// 获取tableView所有的cell的数组
let cells = self.tableView.visibleCells
for cell in cells {
// 将每个cell移动到屏幕外
cell.transform = CGAffineTransformMakeTranslation(0, self.view.frame.size.height)
}
// 将每个cell移动到自己的位置,并且根据cell的index设置延时
for (index, cell) in cells.enumerate() {
UIView.animateWithDuration(1, delay: 0.05 * Double(index), options: .CurveLinear, animations: { () -> Void in
cell.transform = CGAffineTransformMakeTranslation(0, 0)
}, completion: { (finished) -> Void in
})
}
}
3.绿色cell 的动画效果,这个动画效果我们需要在willDisplayCell的代理方法中实现,并且需要用到UIview的简单动画
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
// 设置大小缩放
let trans = CATransform3DMakeScale(0.1, 0.1, 0.1)
// 在大小缩放的基础上设置旋转
cell.layer.transform = CATransform3DRotate(trans, CGFloat(M_PI_2), 0, 0, 1)
UIView.animateWithDuration(1) { () -> Void in
// 动画实现将cell的layer复原
cell.layer.transform = CATransform3DIdentity
}
// 最后复原cell的frame
cell.frame = CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);
}
- 这样带动画的cell就完成了,附上github地址:cell动画,喜欢的可以给星支持一下啦~~另外更多iOS动画效果可以参考我的另一篇博客:iOS核心动画详解swift版