Springy animation on scaling up UIImageView - ios

I have a UIImageView with a circular png inside. I am trying to initialise it with 0 x 0 and scale it up with an ease, so it looks like a bubble effect.
In viewDidLaod(), I initialsied it with CGSize however I couldn't figure out how to apply animation so it doesn't just scales up with a same speed, but it looks like springy (I mean, if it will come to 100 x 100, it works like 'boing' effect and it passes 100 x 100 too, but when animation settles, it finishes at 100x100) .
logoCircularBg.frame.size = CGSize(width: 0, height: 0)
logoCircularBg.hidden = false
Something like this but starting from 0x0 and stopping at 100x100, and with UIImageView.

Here is a code example that I used for animating a UIImageView with a springy/bounce animation like that one that you have posted. Before starting the animation I scale down the imageView to 1% of its size. The animation then scales the imageView back to 100%.
self.myImageView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
UIView.animate(
withDuration: 1.2,
delay: 0.0,
usingSpringWithDamping: 0.2,
initialSpringVelocity: 0.2,
options: .curveEaseOut,
animations: {
self.myImageView.transform = CGAffineTransform(scaleX: 1, y: 1)
},
completion: nil)

For a "springy" animation, call UIView's animate(withDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:). A lower damping value will give increased oscillation at the end of the animation, just as in your screencast.
(When you form this animation, I would suggest animating the view's scale transform, not its frame.)

Related

Transform UIView from center

I have a UIView in my app that I'd like to give a small scale animation. The animation should be bouncy, as in that it should scale a bit smaller and then back to it's original size.
I found the following Objective-C code:
headerView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
Now, I added this to my Swift code:
profileView.transform = CGAffineTransform.identity.scaledBy(x: 0.9, y: 0.9)
However, what happens now is that the view transforms with the left, top corner seemingly as the anchor. I'd like the center to be anchor, though. Any ideas as to what I'm doing wrong?
Do not use identity matrix,
try just like this
view.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)

How can I animate the cornerRadius property of a UIView and the size using a UIView.animate (with spring) method and have the spring effects match?

I am using a
UIView.animate(..., usingSpringWithDamping: 0.4, initialSpringVelocity: 5, ...)
method to animate a UIView (with the layer.cornerRadius set to 1/2 its frame.size.height) to scale in place (or while moving).
Since
UIView.animate(..., usingSpringWithDamping: 0.4, initialSpringVelocity: 5, ...)
does not support the animation of a layer property. I tried creating a
CASpringAnimation(keyPath: "cornerRadius")
with the same values for dampening and velocity. However, the animations were out of sync.
Is there a way to animate the size of a circle UIView and animate the layer.cornerRadius property USING THE SPRING EFFECT (so that the bounce effect occurs on both the cornerRadius and size of the circle)?

Animating height constraints using easing curves

Is there any way to animate a height constraint using an easing curve? I'm thinking of maybe some way to set up a CAKeyFrameAnimation with the values and timings I'd like, and somehow have it affect the height constraint.
I'm not sure if there's a separate animation calss for constraints that I can utilize for constraint keyFrames, or if there's something I'm missing with CAKeyFrameAnimation that would allow me to use that class, or if it's not possible.
Edit: Here's what I'm trying to make work for me, if anyone knows if I'm on the right path or not I'd appreciate some guidance:
self.heightConstraint.constant = newHeight
let animation = CAKeyframeAnimation(keyPath: "frame.size.height")
animation.values = [self.frame.size.height, newHeight]
animation.keyTimes = [0, 1]
animation.duration = self.animationDuration
animation.delegate = self
self.layer.add(animation, forKey: "heightChange")
newHeight would be whatever I want the height to be. But this just pops the view to be taller without any animation. Am I using this correctly? Is this possible to do with constraints?
Edit 2: I should add, I want to use more complex easing functions than the defaults Apple provides as part of UIView.animate(withDuraiton:....
There is no need to use CAKeyframeAnimation. A UIView animation will do easing on animations to a constraint, just like any other animation:
myConstraintOutlet.constant = someNewValue
UIView.animate(
duration: 0.5,
delay: 0.0,
options: .curveEaseInOut, //Use ease-in, ease-out timing.
animations: {
self.view.layoutIfNeeded()
},
completion: nil)
Edit:
If you want custom timing, you could also use the UIView-based keyframe animation with the method animateKeyframes(withDuration:delay:options:animations:completion:) (UIView animation is a whole lot easier to use than CAAnimations.)
Edit #2:
Or, if all you need is a different cubic easing curve, you can do that too. See the very last bit at this link: https://medium.com/#RobertGummesson/a-look-at-uiview-animation-curves-part-3-edde651b6a7a
The key bit is this code snippet from that link:
circleView.transform = CGAffineTransformMakeScale(0, 0)
let timingFunction = CAMediaTimingFunction(controlPoints: 5/6, 0.2, 2/6, 0.9)
CATransaction.begin()
CATransaction.setAnimationTimingFunction(timingFunction)
UIView.animateWithDuration(1) {
self.circleView.transform = CGAffineTransformIdentity
}
CATransaction.commit()
(That's not my code, it's Robert Gummesson's, from the link above. All credit for writing it goes to him.)

How do I animate a horizontal card pivoting (yaw) from its base to vertical?

How do I programmatically via Quartz, animate a rectangle from lying face up (appear as line in 2D) to full height?
The following (pardon the crude drawing) is what I'm trying to get: a deck of cards (lines) with a card pivoting to full height. I don't have any means of adjusting for perspective.
Possible modus operandi: 1) start off with a UIImageView having zero height. 2) Upper (xl,yl)(xr,yr) coordinates widening apart (adjusting perspective) as the height increases.
Any reference, API suggestions welcomed.
This will be relatively close to your desired animation with examples for both UIView animations and CABasicAnimation.
To begin, let's set up the from/to 3D transformations:
let perspective: CGFloat = 1.0 / 1000.0
var fromTransform = CATransform3DMakeRotation(-CGFloat(M_PI_2), 1, 0, 0)
fromTransform.m34 = perspective
var toTransform = CATransform3DMakeRotation(0, 1, 0, 0)
toTransform.m34 = perspective
To animate with UIView animations:
view.layer.transform = fromTransform
UIView.animateWithDuration(1.0, animations: {
view.layer.transform = toTransform
})
If you want to use CABasicAnimation:
let flipAnimation = CABasicAnimation(keyPath: "transform")
flipAnimation.fromValue = NSValue(CATransform3D: fromTransform)
flipAnimation.toValue = NSValue(CATransform3D: toTransform)
flipAnimation.duration = 1.0
flipAnimation.fillMode = kCAFillModeForwards
view.layer.addAnimation(flipAnimation, forKey: "flip")
Edit:
OP desires the anchor point of the animation to be bottom-center, this can be achieved by:
view.layer.anchorPoint = CGPointMake(0.5, 1.0)

how to enable user interaction on animated objects?

For some reason animated cell disable interaction over them, which makes my collection view scrolling useless. If I put my hand in between two cells, the scrolling works, so the animation somehow enters disables touch interaction over them. The animation goes on forever:
UIView.animateWithDuration(0.2,
delay: 0.0,
options: UIViewAnimationOptions.Autoreverse | UIViewAnimationOptions.Repeat,
animations: {
cell.transform = CGAffineTransformMake(1.0, 0.0, skew, 1.0, 0.0, 0.0);
},
completion: nil);
How can I work around this problem?
Try adding UIViewAnimationOptions.AllowUserInteraction flag ?

Resources