2017-04-14 48 views
-5

我想創建一個函數,當用戶無法在登錄視圖中插入通行證或電子郵件時,屏幕會抖動,有人可以幫助我嗎?iOS:如何創建失敗的登錄動畫?

enter image description here

+0

添加代碼.. –

+1

請將您的代碼,而不是一個截圖,但通過粘貼到你的問題。 – rckoenes

回答

1

可以撼動使用此功能

public func shakeView() { 

    let shake: CAKeyframeAnimation = CAKeyframeAnimation(keyPath: "transform") 
    shake.values = [NSValue(caTransform3D: CATransform3DMakeTranslation(-5.0, 0.0, 0.0)), NSValue(caTransform3D: CATransform3DMakeTranslation(5.0, 0.0, 0.0))] 
    shake.autoreverses = true 
    shake.repeatCount = 2.0 
    shake.duration = 0.07 

    self.layer.add(shake, forKey:"shake") 
} 

動搖視圖,您可以創建擴展或做小做下面的功能變化不大撼動任何視圖的任何視圖。

0

您可以通過CABasicAnimation申請搖動動畫。

實施例:

class ViewController: UIViewController { 

    override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(animated) 
     shakeAnimation() 
    } 

    func shakeAnimation() { 
     let animation = CABasicAnimation(keyPath: "position") 
     animation.duration = 0.3 
     animation.repeatCount = 10 
     animation.autoreverses = true 
     animation.fromValue = NSValue(cgPoint: CGPoint(x: self.view.center.x - 10, y: self.view.center.y)) 
     animation.toValue = NSValue(cgPoint: CGPoint(x: self.view.center.x + 10, y: self.view.center.y)) 
     self.view.layer.add(animation, forKey: "position") 
    } 
} 

結果:

enter image description here

0

寫的視圖爲它前一段時間。一切就緒裏面的界面生成器,鏈接視圖IBOutlet中,並呼籲:

shakeItView.shake(completion: { _ in }) 

代碼爲我的組件:

public final class ShakeItView: UIView { 

    public func shake(completion: @escaping (Bool) ->()) { 
     UIView.animate(withDuration: 0.07 - TimeInterval(count) * 0.01, delay: 0.01, options: .curveEaseInOut, animations: { [unowned self] in 
      self.transform = CGAffineTransform.init(translationX: CGFloat(10 * self.direction), y: 0) 
     }) { [unowned self] _ in 
      if self.count >= 4 { 
       self.transform = CGAffineTransform.identity 
       self.resetValues() 
       completion(true) 
      } else { 
       self.count += 1 
       self.direction *= -1 
       self.shake(completion: completion) 
      } 
     } 
    } 

    private var direction: Int = 1 
    private var count: Int = 0 

    private func resetValues() { 
     direction = 1 
     count = 0 
    } 

}