Swift: rotate imageView around left side - ios

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.

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)

Scaling the UIView resets rotation

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)

View clipping after CATransform3DRotate

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)

How to create an anchorPoint to fill the whole iOS screen

I am trying to design a Swift game but I noticed that I am unable to have the ball in the game go into the top left corner of the screen. I set my anchor point below.
override func didMoveToView(view: SKView) {
anchorPoint = CGPoint(x: 0, y: 0.5)
}
I noticed that if I change the anchor point's y-coordinate to 0, I am only able to access the right half of the screen and there's an invisible wall on the left half.
What is the reason for this?
This code is a bit confusing. is the Sprite called anchorPoint? If you can't get the position right, try setting the Sprite to the centre of the screen first, then change the x and y positions gradually so you can experiment with where the Sprite ends up on the screen. Set it to the center like this:
spriteName.position = CGPoint(x: size.width * 0.5, y: size.height * 0.5)

iOS -- How to scale the UIView with left top point?

I want to fix a point (left top point) and scale (zoom out) the UIView according to the point
like this image
Now I use self.transform = CGAffineTransformMakeScale( 0.7 , 0.7); to scale the UIView , But it only can scale the UIView according to the center point
Edit
I try to use set anchorPoint with (0,0) but the view position be wrong
Use anchorPoint property:
self.layer.anchorPoint = CGPointMake(0.0, 0.0);
Try to set anchorPoint of layer
container!.layer.anchorPoint = CGPoint(x: 0, y: 0)

Resources