ios - 如何为 UITableViewCell 实现摇动动画

标签 ios animation uitableview caanimation shake

如何让一个UITableViewCell从左边摇到右边?

我想让用户知道“这个 Action 是不可能的”!

最佳答案

谢谢汉斯!这里是 Swift 和 UITableViewCell 的扩展形式,持续时间和路径长度可选。

import UIKit
import QuartzCore

extension UITableViewCell {
    func shake(duration: CFTimeInterval = 0.3, pathLength: CGFloat = 15) {
        let position: CGPoint = self.center

        let path: UIBezierPath = UIBezierPath()
        path.moveToPoint(CGPointMake(position.x, position.y))
        path.addLineToPoint(CGPointMake(position.x-pathLength, position.y))
        path.addLineToPoint(CGPointMake(position.x+pathLength, position.y))
        path.addLineToPoint(CGPointMake(position.x-pathLength, position.y))
        path.addLineToPoint(CGPointMake(position.x+pathLength, position.y))
        path.addLineToPoint(CGPointMake(position.x, position.y))

        let positionAnimation = CAKeyframeAnimation(keyPath: "position")

        positionAnimation.path = path.CGPath
        positionAnimation.duration = duration
        positionAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)

        CATransaction.begin()
        self.layer.addAnimation(positionAnimation, forKey: nil)
        CATransaction.commit()
    }
}

关于ios - 如何为 UITableViewCell 实现摇动动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21024201/

相关文章:

ios - 重用系统和以编程方式设计的 TableView 问题

ios - UITableView选择——多选

objective-c - 数组仅与其他数组重复第一项

ios - 仅支持带有视网膜图像的 iPad 非视网膜

javascript - Three.js 骨骼动画

iphone - NSUserDefaults 返回的共享默认对象的数据类型

python - 如何为给定 X、Y 坐标和时间的 2D 散点图设置动画,并带有出现和消失的点?

android - 在 RecyclerView 中分别对 View 进行动画处理

iphone - AFNetworking —— GET 使用什么?

ios - Swift - 在 whereField 子句中传递多个参数以从 firebase 检索