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)
Related
I have this code to rotate imageView around its axis (center of image):
transform.m34 = 1.0 / -2000.0
self.imageRevealed1.layer.transform = CATransform3DMakeRotation(CGFloat(Double.pi), 0.0, 1.0, 0.0)
But I need rotate imageView around its left side. How to do it?
You should modify the view anchorPoint before set the layer transform. Example, in your case, the anchorPoint should set CGPoint(x: 0, y: 0.5), then set transform.
I am using this code to scale the view :
view.transform = CGAffineTransform(scaleX: 2, y: 2)
It scales the view perfectly, But problem is that:
If I rotate the view first and then scale it, in this case also scaling works as required but it resets the rotation.
It should not reset the rotation.
You are creating a transform that only has a scale and you are then applying just that scale to the view.
You want to apply the scale transform to the existing transform.
view.transform = view.transform.scaledBy(x: 2, y: 2)
I'm trying to apply perspective transformation to a view (let's call it subview) that only draws a square along its frame, is centered horizontally in its superview and is 3/4 of its superview's width:
I'm using this snippet:
var rotationAndPerspectiveTransform = CATransform3DIdentity
rotationAndPerspectiveTransform.m34 = CGFloat(-1.0/280.0)
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, 30.0 * CGFloat.pi / 180.0, 1.0, 0.0, 0.0)
subview.layer.transform = rotationAndPerspectiveTransform
Which does the transformation I need, but has a weird effect — the right part of the rectangle is "clipped":
What's causing it? How can I avoid it?
Thank you!
Ah, it's a good-old bounds versus frame problem. The frame becomes wider after the transformation and doesn't fit into the bounds anymore.
As I still wanted it to be horizontally centered, this snipped did the trick:
let transformedFrame = segmentView.layer.frame
let transformedBounds = segmentView.layer.bounds
let correctedBoundsX = (transformedFrame.width - transformedBounds.width) / 2
segmentView.layer.bounds = CGRect(x: correctedBoundsX, y: transformedBounds.origin.y, width: transformedBounds.width, height: transformedBounds.height)
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.)
I'm scaling an UIView this way:
myView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
and the view is scaled well, but I want one of its subview to remain unscaled, i.e fullscreen. How to achieve this?
I was able to get Avt's answer to work by using CGAffineTransformInvert:
myView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9)
subView.transform = CGAffineTransformInvert(myView.transform)
The only way is to add additional scaling for specific subview
myView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
specific.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1./0.9, 1./0.9);
probably you will also need to add
myView.clipsToBounds = NO;