Custom ease in animationWithDuration?

When doing UIView.animateWithDuration, I would like to define a custom curve for simplicity, not the default: .CurveEaseInOut, .CurveEaseIn, .CurveEaseOut, .CurveLinear.

This is an example of ease that I want to apply to UIView.animateWithDuration:

let ease = CAMediaTimingFunction(controlPoints: Float(0.8), Float(0.0), Float(0.2), Float(1.0)) 

I tried to make my own UIViewAnimationCurve, but it seems to only accept one Int.

I can apply custom lightness to Core Animation, but I would like to simplify and optimize the code for UIView.animateWithDuration. UIView.animateWithDuration is better for me, since I will not need to define an animation for each animated property and simpler completion handlers and have all the animation code in one function.

Here is my broken code:

 let customOptions = UIViewAnimationOptions(UInt((0 as NSNumber).integerValue << 50)) UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: 5)!) UIView.animateWithDuration(2, delay: 0, options: customOptions, animations: { view.layer.position = toPosition view.layer.bounds.size = toSize }, completion: { finished in println("completion") }) 
+8
ios swift uiviewanimation
source share
3 answers

This is because UIViewAnimationCurve is an enumeration - its mostly human-readable representations of integer values ​​used to determine which curve to use.

If you want to define your own curve, you need to use CA animation.

You can execute completion blocks and animation groups. You can group multiple CA animations in a CAAnimationGroup

 let theAnimations = CAAnimationGroup() theAnimations.animations = [positionAnimation, someOtherAnimation] 

Use CATransaction to complete.

 CATransaction.begin() CATransaction.setCompletionBlock { () -> Void in // something? } // your animations go here CATransaction.commit() 
+4
source share
0
source share

After some research, the following code works for me.

  1. Create an explicit underlying animation transaction, set the desired synchronization function.
  2. Use eigher or UIKit and CAAnimation to make changes.

     let duration = 2.0 CATransaction.begin() CATransaction.setAnimationDuration(duration) CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(controlPoints: 0.8, 0.0, 0.2, 1.0)) CATransaction.setCompletionBlock { print("animation finished") } // View animation UIView.animate(withDuration: duration) { // Eg view.center = toPosition } // Layer animation view.layer.position = toPosition view.layer.bounds.size = toSize CATransaction.commit() 
0
source share

All Articles