iOS:如何创建失败登录动画?
我正在尝试创建一个功能,当用户无法在登录视图中插入通行证或电子邮件时,屏幕将抖动,有人可以帮助我吗iOS:如何创建失败登录动画?,ios,swift,xcode,Ios,Swift,Xcode,我正在尝试创建一个功能,当用户无法在登录视图中插入通行证或电子邮件时,屏幕将抖动,有人可以帮助我吗 您可以使用此功能摇动任何视图 public func shakeView() { let shake: CAKeyframeAnimation = CAKeyframeAnimation(keyPath: "transform") shake.values = [NSValue(caTransform3D: CATransform3DMakeTranslation(-5.0,
您可以使用此功能摇动任何视图
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")
}
要抖动视图,您可以创建扩展或在下面的函数中稍作更改来抖动任何视图。您可以通过
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")
}
}
结果:
不久前为它写了一个视图。在interface builder中放置其中的所有内容,将视图链接为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
}
}
添加代码。请添加您的代码,而不是屏幕截图,而是通过粘贴到您的问题中。