Как я могу поставить встряхивание анимации на UIImageView - PullRequest
0 голосов
/ 22 мая 2019

Я пытаюсь добавить постоянную анимацию встряхивания в UIImageView. Как я могу контролировать время анимации.

 var coffeeImageView = UIImageView(image: UIImage(named: "coffee.png"))
        shakeImg.frame = CGRectMake(100, self.view.frame.size.height - 100, 50, 50)
        self.view.addSubview(coffeeImageView)

        let coffeeShakeAnimation = CABasicAnimation(keyPath: "position")
        coffeeShakeAnimation.duration = 0.07
        coffeeShakeAnimation.repeatCount = 20
        coffeeShakeAnimation.autoreverses = true
        coffeeShakeAnimation.fromValue = NSValue(cgPoint: CGPointMake(shakeImg.center.x - 10, shakeImg.center.y))
        coffeeShakeAnimation.toValue = NSValue(cgPoint: CGPointMake(shakeImg.center.x + 10, shakeImg.center.y))
        shakeImg.layer.add(coffeeShakeAnimation, forKey: "position")

Ответы [ 3 ]

2 голосов
/ 22 мая 2019

Я бы предпочел использовать UIView Extension для достижения этой анимации с возможностью повторного использования.

Swift 4

extension UIView {
    func shake(_ duration: Double? = 0.4) {
        self.transform = CGAffineTransform(translationX: 20, y: 0)
        UIView.animate(withDuration: duration ?? 0.4, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
            self.transform = CGAffineTransform.identity
        }, completion: nil)
    }
}

Использование

coffeeImageView.shake()
coffeeImageView.shake(0.2)
1 голос
/ 22 мая 2019

Вам нужно

var coffeeImageView = UIImageView(image: UIImage(named: "coffee.png"))
coffeeImageView.frame = CGRect(x: 100, y: self.view.frame.size.height - 100, width: 50, height: 50)
self.view.addSubview(coffeeImageView) 
let coffeeShakeAnimation = CABasicAnimation(keyPath: "position")
coffeeShakeAnimation.duration = 0.07
coffeeShakeAnimation.repeatCount = 20
coffeeShakeAnimation.autoreverses = true
coffeeShakeAnimation.fromValue = NSValue(cgPoint: CGPoint(x: coffeeImageView.center.x - 10, y: coffeeImageView.center.y))
coffeeShakeAnimation.toValue = NSValue(cgPoint: CGPoint(x: coffeeImageView.center.x + 10, y: coffeeImageView.center.y))
coffeeImageView.layer.add(coffeeShakeAnimation, forKey: "position")

extension UIView {
    func shake(_ dur:Double) {
        let anim = CABasicAnimation(keyPath: "position")
        anim.duration = dur
        anim.repeatCount = 20
        anim.autoreverses = true
        anim.fromValue = NSValue(cgPoint: CGPoint(x: self.center.x - 10, y: self.center.y))
        anim.toValue = NSValue(cgPoint: CGPoint(x: self.center.x + 10, y: self.center.y))
        self.layer.add(anim, forKey: "position")
    }
}

enter image description here

1 голос
/ 22 мая 2019

Попробуйте!

class func shakeAnimation(_ view: UIView) {
    let animation = CABasicAnimation(keyPath: "position")
    animation.duration = 0.06
    animation.repeatCount = 2
    animation.autoreverses = true
    animation.isRemovedOnCompletion = true
    animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeOut)
    animation.fromValue = NSValue(cgPoint: CGPoint(x: view.center.x - 7, y: view.center.y))
    animation.toValue = NSValue(cgPoint: CGPoint(x: view.center.x + 7, y: view.center.y))
    view.layer.add(animation, forKey: nil)
}
...