I wrote this post. This is redundant for UIView, plus the options are OSX-oriented. Do it instead.
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; [animation setDuration:0.05]; [animation setRepeatCount:8]; [animation setAutoreverses:YES]; [animation setFromValue:[NSValue valueWithCGPoint: CGPointMake([lockView center].x - 20.0f, [lockView center].y)]]; [animation setToValue:[NSValue valueWithCGPoint: CGPointMake([lockView center].x + 20.0f, [lockView center].y)]]; [[lockView layer] addAnimation:animation forKey:@"position"];
You will have to play with the duration and repeatCount parameters, as well as the distance x from the center in the from and to values, but it should give you what you need. Hope this helps. Let me know if you have any questions.
---
Swift 3.0
let midX = lockView.center.x let midY = lockView.center.y let animation = CABasicAnimation(keyPath: "position") animation.duration = 0.06 animation.repeatCount = 4 animation.autoreverses = true animation.fromValue = CGPoint(x: midX - 10, y: midY) animation.toValue = CGPoint(x: midX + 10, y: midY) layer.add(animation, forKey: "position")
Matt Long 04 Oct 2018-10-18 18:51
source share