for days now I am trying to solve this, apparently I just can't get affine transforms.
Basically, I would like to rotate a movie multiple times by 90 degrees. There is a good example here:
Rotating Video w/ AVMutableVideoCompositionLayerInstruction
but this works only for the first rotation, while I would like to rotate it from portrait to landscape, back to portrait, back to landscape.
Whatever I tried, I get absolutely weird results. Unfortunately, even after much reading, my understanding of affine transforms obviously did not improve either.
So I am basically asking for a layout of an algorithm which will
rotate the layer of an AVMutableVideoComposition by 90 degrees,
regardless of its current orientation.
Apply a translation to move the layer into the center.
This should be called each time I click on a button.
But whatever I try, the layer is rotating correctly, but the translation is moving the layer out of the center. What is the right way to do this?
But whatever I try, the layer is rotating correctly, but the translation is moving the layer out of the center. What is the right way to do this?
The right way to do this is with 2 transforms: one to rotate, and one to translate. At the beginning of the processing, make sure that your current position is set - by assigning the current transform at the start of processing. Then, create two independent transforms - one for rotate, and the other for translation. You can 'concat' these two transforms to create a new one which does both operations - but if you expect the translation to function 'in place', be sure to re-set the original transform before you apply the new one.
Related
I have a UIImage on my storyboard, and I have just rotated it( i can rotate it any number of degrees). The code for the rotation is below:
let DegreesFloat = Double(-Degrees) * M_PI/180
self.imageView.transform = CGAffineTransformMakeRotation(CGFloat(DegreesFloat))
By the way, degrees in the first line of code, is an integer I enter into a text field. When the view loads, I have this...
self.imageView.transform = CGAffineTransformMakeScale(1, -1);
to keep the image upside down, cause that's just how I need it to be. The issue is that when I rotate the image, it cancels out the flip, and flips it back to its original flip (without the 2nd paragraph of code). I need it to keep it's upside down flip, while performing it's rotation.
When I add the flip code after the rotate code, it just cancels out the rotate code. I have no idea how to do this, but I just need it to stay upside down, while correctly performing a rotation, and NOT flipping itself back to its original right-side up orientation.
I think what you are asking is how to make multiple transform commands together. The transformations are actually a matrix telling the image later what to do. Use a CGAffineTransformConcat to apply multiple transformations. Refer to the Apple documentation
I have been searching from last two days on internet, I have checked many source codes on net but none of them has provided the result I want.
The image rotation would have perspective but still there would be no changes in the heights of both left and right sides of an image.
I want to set image inside the laptop screen
Please help me out, Thanks.
So you want to 2D pespective drawing of a laptop screen (on an iOS device?) and put a 2D image on that screen, but with the image transformed so its perspective looks correct on the laptop screen, right?
What you need to do is to add an image view on top of your laptop image view. Lets call it laptopScreenImageView.
Then apply a CATransform3D to that the laptopScreenImageView's layer.
The trick to get 3D perspective out of a CALayer is to modify the .m34 value of the transform. Typically you set the .m34 value to a very small negative number, somewhere around -1/200 to -1/500 (the denominator in the fraction is the z coordinate of the "eye position" for viewing the perspective image, in pixels, or how many pixels "above" the image the viewer's eye should seem to be. I don't fully understand it, to be honest. I fiddle with the .m34 value until I get something that looks right.)
Alternately you could try adding a CATransformLayer to your laptop image view's layer, and then adding a CALayer containing your image as a sublayer of the CATransformLayer. I haven't used CATransformLayers before, but the docs say they are supposed to support layers with 3D perspective, giving you the same effect as modifying the .m34 component of a layer's transform.
Here is the result that I get:
If I set the doubleSided property of layer 4 as No, the whole layer disappears. I know that already. But then, how can I show yet with correct content orientation?
Do the animation in two parts. At the halfway point, when the layer is side-on to the camera so you can’t see it, flip the layer the other way around and move the anchor point to the other side, and swap out the contents of the layer if you need to.
Unfortunately, I don’t have any sample code, because the last time I did this was on iOS 4, and the pile of hacks view-based animations that I was using don’t appear to work on layers any more. You could post your own code and accept your own answer if you want.
I want to build an animated transition between two view controllers in iOS, resembling the "Box" transition in PowerPoint or the "Reflection" transition in Keynote.
You can see it here, at 2:10:
http://youtu.be/1fLQg5hFQQg?t=2m10s
What's the best way to do this?
Thanks!
That would be a complex animation to recreate. You'd need to use a CAAnimationGroup that grouped several different animations running at once. You'd want to animate a rotation around the y axis with the center of rotation lifted off the screen, on both the view controller that is animating away and the view that your are animating into place.
You would have to tweak the transform to make it draw with perspective (you add a small value to the .m34 record in the transform). That's because CA animations are orthographic by default (they don't show perspective.)
The reflections could be created using a special subclass of CALayer that lets you create duplicates of a layer. I'm blanking on the name of that layer subclass at the moment. You'd set up 1 duplicate with a scale of -1 on the y axis to flip it upside down, and a darkening effect. I've never done it myself, but I've seen several examples in books and online.
I am having one issue about CATransform3D.
I have a view A, which contains a label B. B's layer position is the center of A. B is used to display notification messages. I want to rotate A by 180 degree, of course the text in B will be upside down. So I have to rotate B by 180 degree too. Everything seems straight forward and actually in the simulator it works fine.
But when I loaded on the device, A and B did rotate, but B's position was changed. Now I can only see half of B, the other half is out of A.
My guess is that, when rotating A, because B is related to A's coordinate system, which was flipped 180 degree, B's location is changed.
But I want to know if anybody has ideas how to fix this problem or if you guys have better ways to approach.
Thank you very much.
UPDATE
I still can't figure out why B's position gets shifted, but I came up with another method to implement the same animation. A little bit tricky.
The key is animation.autoreverse, we know that when you rotate the label's super view by Pi, the text in the label will be rotated upside down. So what I did was rotate the super view by Pi/2, keep the same duration and set autoreverse = YES, what it will do is that it will rotate the super view by Pi/2, then rotate it back to the initial state. The result turns out that the view isn't get rotated at all, but for the user's vision, it is rotated.
I've found that subLayers and subviews of views don't always respond well to doubled animations (esp. alpha and center changes). If you must (as it is a little more expensive than normal), use UIViewAnimationOptionAllowsAnimatedContent, which will force a redraw instead of a 'snapshot' that is animated.