"zPosition" CABasicAnimation not working - ios

I used an animation along the z axis a long time ago in Objective C :
CABasicAnimation *positionAnimation;
positionAnimation=[CABasicAnimation animationWithKeyPath:#"zPosition"];
positionAnimation.fromValue=[NSNumber numberWithFloat:frandom(800, 400)];
positionAnimation.toValue=[NSNumber numberWithFloat:frandom(-300, -100)];
positionAnimation.duration=duration;
positionAnimation.repeatCount = HUGE_VALF;
positionAnimation.removedOnCompletion = NO;
But it is not working anymore on swift 3, the layer does not move at all.
let positionAnimation = CABasicAnimation(keyPath: "zPosition")
positionAnimation.fromValue = NSNumber(value: Float.random(from: 800, 400))
positionAnimation.toValue = NSNumber(value: Float.random(from: -300, -100))
positionAnimation.duration = duration
positionAnimation.repeatCount = 400
positionAnimation.isRemovedOnCompletion = false
Did i miss something ?
thank you.

Related

how to change the starting and ending point for the UIBezierPath rect to CAShapeLayer

I want to create one rectangle layer of progress bar around my object it maybe anything it should be an uibutton, uiview or any object so i create a below code:
let layer_ca = CAShapeLayer.init()
layer_ca.strokeColor = UIColor.green.cgColor
layer_ca.lineWidth = 10.0
layer_ca.fillColor = UIColor.clear.cgColor
let bezier_path = UIBezierPath.init(roundedRect: CGRect.init(x: captureButton.frame.origin.x, y: captureButton.frame.origin.y, width: captureButton.frame.width, height: captureButton.frame.height), cornerRadius: 15)
bezier_path.move(to: CGPoint(x: 12, y: 15))
layer_ca.path = bezier_path.cgPath
let animation = CABasicAnimation.init(keyPath: "strokeEnd")
animation.fromValue = NSNumber.init(value: 0.0)
animation.toValue = NSNumber.init(value: 1.0)
animation.duration = 2.0
layer_ca.add(animation, forKey: "myStroke")
self.view.layer.addSublayer(layer_ca)
here "captureButton" was an my object so its created successful above the object with the cabasicanimation and now what my question was how to change the ending point of the path and how should i stop the path to half of the distance path and then i continue with the same path which i stop previous path whenever i call to start.
i need exactly like as a rectangular progress bar around an object.
please help me anyone who should known the answer..!
I just have the same question, and spend a few hours to figure it out. if you want control the start point or end point, you should create your own BezierPath, and custom your path. bezierPath.move("start position"), and there it is, the last point will be the end position.

Load Animated gif in UIImageView IOS

I have this gif into my assets file
The name of the assets is loading_apple but when I add the following code I get a nullpointer exception error:
let img = UIImage (named: "loading_apple")
So how can I show this gif ? :(
Hope someone can help me.
I would recommend using FLAnimatedImage https://github.com/Flipboard/FLAnimatedImage
I would recommend breaking out the frames of that gif and use animatedImageNamed:duration: - you can name them all the similar name with a number change at the end. For instance:
loading-1.png
loading-2.png
loading-3.png etc.
Xcode will recognize you want multiple images and will play those through in order.
Look at THIS
instead of storing the gif file, why don't u make use of CAReplicatorLayer refer this link and this link in objective c, i took same code in second like and with some modification,
func spinTheCustomSpinner() -> Void {
let aBar:CALayer = CALayer.init()
aBar.bounds = CGRectMake(0, 0, 8, 25);
aBar.cornerRadius = 4; //(8/2)
aBar.backgroundColor = UIColor.blackColor().CGColor
aBar.position = CGPointMake(150.0, 150.0 + 35)
let replicatorLayer:CAReplicatorLayer = CAReplicatorLayer.init()
replicatorLayer.bounds = CGRectMake(0, 0,300,300)
replicatorLayer.cornerRadius = 10.0
replicatorLayer.backgroundColor = UIColor.whiteColor().CGColor
replicatorLayer.position = CGPointMake(CGRectGetMidX(self.view!.bounds), CGRectGetMidY(self.view!.bounds))
let angle:CGFloat = CGFloat (2.0 * M_PI) / 12.0
let transform:CATransform3D = CATransform3DMakeRotation(angle, 0, 0, 1.0)
replicatorLayer.instanceCount = 12
replicatorLayer.instanceTransform = transform
replicatorLayer .addSublayer(aBar)
self.view!.layer .addSublayer(replicatorLayer)
aBar.opacity = 0.0
let animationFade:CABasicAnimation = CABasicAnimation(keyPath: "opacity")
animationFade.fromValue = NSNumber(float: 1.0)
animationFade.toValue = NSNumber(float: 0.0)
animationFade.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animationFade.repeatCount = HUGE
animationFade.duration = 1.0
let aBarAnimationDuration:Double = 1.0/12.0
replicatorLayer.instanceDelay = aBarAnimationDuration
aBar .addAnimation(animationFade, forKey: "fadeAnimation")
}
if u use above with a view, show this view while loading and hide after loading, this is really cool and handy and there is no headache of storing the gif file and using a image view to load.
and by changing the properties of aBar layer u get different effects.
For this specific gif you may want to use UIActivityIndicatorView and make it animating with startAnimating()

UIDynamicAnimator Shake with spring effect

I'm trying to replace my core animation shake effect in this code
let shake = CAKeyframeAnimation(keyPath: "position.x")
shake.values = [0, 20, -20, 20, -15, 15, -5, 5, 0]
shake.keyTimes = [0, 1/10.0, 3/10.0, 5/10.0, 6/10.0, 7/10.0, 8/10.0, 9/10.0, 1]
shake.duration = 0.4
shake.additive = true
circlesContainer.layer.addAnimation(shake, forKey: "shakeYourBooty")
with the spring effect by combining UIPushBehavior and UIAttachmentBehavior like this
if origCirclesContainerCenter == nil {
origCirclesContainerCenter = circlesContainer.center
}
shake = UIDynamicAnimator(referenceView: self.view)
let push = UIPushBehavior(items: [circlesContainer], mode: UIPushBehaviorMode.Continuous)
push.pushDirection = CGVectorMake(100.0, 0.0)
print(origCirclesContainerCenter)
let attachement = UIAttachmentBehavior(item: circlesContainer, attachedToAnchor:origCirclesContainerCenter!)
attachement.frequency = 3.0
attachement.damping = 0.5
shake.addBehavior(attachement)
shake.addBehavior(push)
The problem is the attachment behavior doesn't seem to work because the view does not spring back to the original position after being pushed. The animation actually pushes the view position to the right 100 points. origCirclesContainerCenter is the same on every call. Any idea?
I changed the push behavior mode to UIPushBehaviorMode.Instantaneous to fix the problem.

Programming an animation to loop forever in Swift

I'm using a code snippit from the Stack Overflow post: How to make "shaking" animation
I pasted this code into my project and it works perfectly:
func shakeView(){
var shake:CABasicAnimation = CABasicAnimation(keyPath: "position")
shake.duration = 0.1
shake.repeatCount = 2
shake.autoreverses = true
var from_point:CGPoint = CGPointMake(LoginField.center.x - 5, LoginField.center.y)
var from_value:NSValue = NSValue(CGPoint: from_point)
var to_point:CGPoint = CGPointMake(LoginField.center.x + 5, LoginField.center.y)
var to_value:NSValue = NSValue(CGPoint: to_point)
shake.fromValue = from_value
shake.toValue = to_value
LoginField.layer.addAnimation(shake, forKey: "position")
}
I however want this animation to loop until another function is called. How do I make it loop indefinite and how can I make it stop when another function is called?
First, set the repeatCount to HUGE_VALF / greatestFiniteMagnitude:
Setting this property to HUGE_VALF will cause the animation to repeat forever.
Second, call LoginField.layer.removeAllAnimations() or LoginField.layer.removeAnimationForKey("position")when you want to stop the animation.
Edit: Apparently Swift does not know HUGE_VALF, setting the repeatCount to Float.infinity should work.
Use shake.repeatCount = Float.infinity

Keeping a background image in the center on animation

I have animation setup to resize the image to about 1.3 times it's original size. The animations and everything are working without a problem but the image is moving towards the top left. Which means that the position of the image is not centering upon resize. How do i solve this problem
These are the animations I setup
var borderWidth:CABasicAnimation = CABasicAnimation(keyPath: "borderWidth")
borderWidth.fromValue = 0
borderWidth.toValue = 5
borderWidth.repeatCount = Float.infinity
sender.layer.borderWidth = 0
var increaseButtonHeight:CABasicAnimation = CABasicAnimation(keyPath: "bounds.size.height")
increaseButtonHeight.fromValue = sender.frame.size.height
increaseButtonHeight.toValue = sender.frame.size.height * 1.3
var increaseButtonWidth: CABasicAnimation = CABasicAnimation(keyPath: "bounds.size.width")
increaseButtonWidth.fromValue = sender.frame.size.width
increaseButtonWidth.toValue = sender.frame.size.width * 1.3
var boom:CAAnimationGroup = CAAnimationGroup()
boom.animations = [borderWidth,increaseButtonWidth, increaseButtonHeight]
boom.repeatCount = Float.infinity
boom.duration = 0.5
boom.autoreverses = true
sender.layer.addAnimation(boom, forKey: "boom")
Do I need to setup a new animation for centering the button continuously as the animation happens?
Please help
Nikhil
Set the property contentsGravity of the layer to kCAGravityCenter

Resources