I am rotating a layer using CATransform3DMakeRotation. I am having two issues:
a) Sometimes going out of the screen
b) Sometimes positioning on top of other layer.
int randrotate=arc4random()%3+1;
CATransform3D trans5=CATransform3DMakeRotation(M_PI*randrotate , 1.0, 0.0, 0.0);
randrotate=arc4random()%4+1;
CATransform3D trans6=CATransform3DRotate(trans5,M_PI*randrotate, 0.0, 0.0, 1.0);
randrotate=arc4random()%3+1;
CATransform3D trans7=CATransform3DRotate(trans5,M_PI_2*randrotate, 0.0, 1.0, 0.0);
CATransform3D trans=CATransform3DRotate(trans7,M_PI*45/180, 0.0, 1.0, 0.0);
CATransform3D trans8=CATransform3DRotate(trans,DEGREES_TO_RADIANS(-45), 1.0, 0.0, 0.0);
CABasicAnimation *ganimation = [CABasicAnimation animationWithKeyPath: #"transform"];
ganimation.beginTime=CACurrentMediaTime();
ganimation.toValue = [NSValue valueWithCATransform3D: trans];
ganimation.duration = 0;
ganimation.cumulative = NO;
ganimation.repeatCount = 0;
ganimation.removedOnCompletion = NO;
ganimation.fillMode = kCAFillModeForwards;
ganimation.delegate=self;
[correctbase addAnimation:ganimation forKey:#"green"];
after animation correct base is changing his position.
Now after animation, I want to put the correctbase in it's original position back, correctbase is a CATransformLayer.
if i try to access presentation layer it's always nil
CATransformLayer *layer=(CATransformLayer*)correctbase.presentationLayer;
Can someone help me?
Thanks in advance.
Related
I've a rectangle shaped CALayer, which I've scaled as below:
CATransform3D currentTransform = layer.transform;
CATransform3D newTransform = CATransform3DScale(currentTransform, xscale, yscale, 1.0);
[layer setTransform:newTransform];
After applying CATransform3DScale, transformed CALayer is also a rectangle.
But when I apply CATransform3DRotate, transformed CALayer becomes a rhombus.
CATransform3D currentTransform = layer.transform;
CATransform3D newTransform = CATransform3DRotate(currentTransform, rotation, 0.0, 0.0, 1.0);
[layer setTransform:newTransform];
How can I get the rectangle shape after applying rotate transform?
I didn't change anchor point of the layer. Sometimes I need to change the position of the layer.
In my app design I want to show tilted map (just like Apple 3D map but in pre-iOS 7 too). I am using transforms to achieve this but the results aren't satisfactory.
My annotations look skewed (I want them straight & facing user - not aligning with my map). Also after applying transform my map stops scrolling.
- (void) animateMap
{
[UIView animateWithDuration:0.8 animations:
^{
CATransform3D transform = CATransform3DIdentity;
transform.m34 = 1.0 / -1200.0;
transform = CATransform3DRotate(transform, M_PI_4, 1.0, 0.0, 0.0);
CATransform3D transform1 = CATransform3DScale(transform, 1.33, 1.66, 0);
map.layer.transform = transform1;
}
completion:^(BOOL finished)
{
for (id ann in map.annotations)
{
[UIView animateWithDuration:0.8 animations:
^{
MKAnnotationView * annView = [map viewForAnnotation:ann];
CATransform3D transform = CATransform3DIdentity;
transform.m34 = 1.0 / -1200.0;
transform = CATransform3DRotate(transform, -M_PI_4, 1.0, 0.0, 0.0);
annView.layer.transform = transform;
}];
}
}];
}
I have a problem with CATransform3D.
When I try to do a Y axis rotation, the UIView breaks into two parts before animation starts.
An image is worth a thousand words...
Here's the code:
CATransform3D _3Dt = CATransform3DMakeRotation(2.0 * M_PI, 0.0, 1.0, 0.0);
[UIView animateWithDuration:2.0
animations:^{
view.layer.transform = _3Dt;
}];
Any idea?
Thanks!
Solved!
The problem was the layer.zPosition
Here's is the final code:
view.layer.zPosition = CGRectGetWidth(view.bounds);
CATransform3D _3Dt = CATransform3DMakeRotation(2.0 * M_PI, 0.0, 1.0, 0.0);
[UIView animateWithDuration:2.0
animations:^{
view.layer.transform = _3Dt;
}];
Thanks!
I have the following code that rotates a CALayer by -45degrees on the Y axis:
#define D2R(x) (x * (M_PI/180.0))
- (void) swipe:(UISwipeGestureRecognizer *)recognizer
{
CATransform3D transform = CATransform3DMakeRotation(D2R(-45), 0, 1.0, 0);
transform.m34 = -1.0 / 850;
CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath: #"transform"];
transformAnimation.fillMode = kCAFillModeForwards;
transformAnimation.removedOnCompletion = NO;
transformAnimation.toValue = [NSValue valueWithCATransform3D:transform];
transformAnimation.duration = 0.5;
[self.layer addAnimation:transformAnimation forKey:#"transform"];
}
The animation works, except it ends with no perspective - ignoring my m34 setting if I'm understanding things correctly.
Halfway through:
At the end:
What am I doing wrong?
Animation only affects the appearance of the view during the animation. It doesn't get applied to the view after the animation ends. You need to do this yourself. I'm guessing something like this right after adding the animation will work:
self.layer.transform = transform;
You can do this right away, as the animation will hide it until the animation completes.
Try this :
- (void) swipe:(UISwipeGestureRecognizer *)recognizer
{
CATransform3D transform = CATransform3DIdentity;
transform.m34 = -10 / 850.0;
transform = CATransform3DRotate(transform, D2R(-45), 0, 1.0, 0);
CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath: #"transform"];
transformAnimation.fillMode = kCAFillModeForwards;
transformAnimation.removedOnCompletion = NO;
transformAnimation.toValue = [NSValue valueWithCATransform3D:transform];
transformAnimation.duration = 0.5;
[self.layer addAnimation:transformAnimation forKey:#"transform"];
}
And end the effect is like this :
Basically, I'm rotating a layer about a point as :
CATransform3D transform = CATransform3DIdentity;
transform.m34 = 1.0 / -500;
transform = CATransform3DTranslate(transform, rotationPoint.x-center.x, rotationPoint.y-center.y, 0.0);
transform = CATransform3DRotate(transform, (rotationAngleFor) * M_PI / 180.0f, 0.0, 1.0, 0);
transform = CATransform3DTranslate(transform, center.x-rotationPoint.x, center.y-rotationPoint.y, 0.0);
I also create a layer, add it to a bigger layer and then apply the transform to it:
[self.layer addSublayer:myLayer];
myLayer.transform = transform;
How to animate this?
Note- Putting this in a UIView animation block doesn't work.
Edit: As #DuncanC pointed out my description did not match the actual code.
You can use a CABasicAnimation that is added to the layer as follows.
CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath: #"transform"];
<here goes your definition of transform>
transformAnimation.toValue = [NSValue valueWithCATransform3D:newTransform];
transformAnimation.duration = 10;
[self.layer addAnimation:transformAnimation forKey:#"transform"];
This animation performs a continuous change of the transform property of the layer from the original value of the transform property of the layer to the newTransform transformation. The change takes 10 seconds.