如何实现UITableViewCell的摇动动画

时间:2014-01-09 15:22:04

标签: ios animation uitableview caanimation shake

如何让UITableViewCell从左侧向右侧摇晃?

我希望用户知道“此操作无法执行”!

4 个答案:

答案 0 :(得分:5)

这是我的解决方案:

    CGPoint position = cell.center;

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(position.x, position.y)];
    [path addLineToPoint:CGPointMake(position.x-20, position.y)];
    [path addLineToPoint:CGPointMake(position.x+20, position.y)];
    [path addLineToPoint:CGPointMake(position.x-20, position.y)];
    [path addLineToPoint:CGPointMake(position.x+20, position.y)];
    [path addLineToPoint:CGPointMake(position.x, position.y)];

    CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    positionAnimation.path = path.CGPath;
    positionAnimation.duration = .5f;
    positionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    [CATransaction begin];
    [cell.layer addAnimation:positionAnimation forKey:nil];
    [CATransaction commit];

玩得开心!

答案 1 :(得分:3)

谢谢Hans One!这里是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()
    }
}

答案 2 :(得分:0)

我基于Swift 4中@oyalhi的回答创建了一个协议:

protocol Shackable { }

extension Shackable where Self: UIView {

    func shake(duration: CFTimeInterval = 0.3, pathLength: CGFloat = 15) {
        let position = self.center
        let path = UIBezierPath()

        path.move(to: CGPoint(x: position.x, y: position.y))
        path.addLine(to: CGPoint(x: position.x - pathLength, y: position.y))
        path.addLine(to: CGPoint(x: position.x + pathLength, y: position.y))
        path.addLine(to: CGPoint(x: position.x - pathLength, y: position.y))
        path.addLine(to: CGPoint(x: position.x + pathLength, y: position.y))
        path.addLine(to: CGPoint(x: position.x, y: position.y))

        let positionAnimation = CAKeyframeAnimation(keyPath: "position")
        positionAnimation.path = path.cgPath
        positionAnimation.duration = duration
        positionAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        CATransaction.begin()
        self.layer.add(positionAnimation, forKey: nil)
        CATransaction.commit()
    }
}

答案 3 :(得分:0)

这是oyalhi协议的Swift 4版本:

import UIKit

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

        let path: UIBezierPath = UIBezierPath()
        path.move(to: CGPoint(x: position.x, y: position.y))
        path.addLine(to: CGPoint(x: position.x-pathLength, y: position.y))
        path.addLine(to: CGPoint(x: position.x+pathLength, y: position.y))
        path.addLine(to: CGPoint(x: position.x-pathLength, y: position.y))
        path.addLine(to: CGPoint(x: position.x+pathLength, y: position.y))
        path.addLine(to: CGPoint(x: position.x, y: position.y))

        let positionAnimation = CAKeyframeAnimation(keyPath: "position")

        positionAnimation.path = path.cgPath
        positionAnimation.duration = duration
        positionAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)

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