CGAffineTransform rotation and resize on slider value - ios

I am using Slider to Resize and Rotate-
For the Rotate -
CGAffineTransform transform = editingView.transform;
transform = CGAffineTransformMakeRotation(sliderVal * 2*M_PI / 30);
editingView.transform = transform;
For the Resize-
CGAffineTransform t = CGAffineTransformMakeScale(sliderVal/30, sliderVal/30);
CGPoint center = editingView.center;
[UIView animateWithDuration:0.5
animations:^{
editingView.transform = t;
editingView.center = center;
}
completion:^(BOOL finished) {
}];
Using the above code,Both working fine separately.
But I have to resize the rotated view,Or rotate the resized view.
I saw many suggestions coming separate behavior because i am using CGAffineTransformMakeRotation,CGAffineTransformMakeScale,If i use the CGAffineTransformScale,CGAffineTransformRotation then my problem will be solve.
The problem is when I am using CGAffineTransform then scaling is not proper,View disappears from the screen.

You're setting transformation matrix of the view with editingView.transform line.
You should change your code for rotate:
CGAffineTransform transform = editingView.transform;
transform = CGAffineTransformMakeRotation(sliderVal * 2*M_PI / 30);
editingView.transform = CGAffineTransformConcat(editingView.transform, transform);
and for resize:
CGAffineTransform t = CGAffineTransformMakeScale(sliderVal/30, sliderVal/30);
CGPoint center = editingView.center;
[UIView animateWithDuration:0.5
animations:^{
editingView.transform = CGAffineTransformConcat(editingView.transform,t);
editingView.center = center;
}
completion:^(BOOL finished) {
}];
With CGAffineTransformConcat you add 2 transform matrixes together so you won't lose older transforms. You can use CGAffineTransformIdentity to reset the transform.

CGAffineTransform translate = CGAffineTransformMakeTranslation(self.webView.frame.origin.x,self.webView.frame.origin.y - self.webView.frame.size.height * 0.25);
CGAffineTransform scale = CGAffineTransformMakeScale(0.6, 0.6);
CGAffineTransform transform = CGAffineTransformConcat(translate, scale);
transform = CGAffineTransformRotate(transform, degreesToRadians(-10));
[UIView beginAnimations:#"MoveAndRotateAnimation" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:2.0];
editingView.transform = transform;
[UIView commitAnimations];
Try like this...

Related

CGAffineTransform block others animation

i have two images(iphoneImage , ipadImage) and i want to move ipadImage and rotate iphoneImage but when i use CGAffineTransform on iphoneImage it blocks ipadImage Move ??
so any suggestions
CGRect tempRect=CGRectMake(-351, self.ipadImage.frame.origin.y,
self.ipadImage.frame.size.width,
self.ipadImage.frame.size.height);
CGAffineTransform transform=_iphoneImage.transform;
transform = CGAffineTransformRotate(transform, M_PI * 90 / 180.0);
[UIView animateWithDuration:2
animations:^{
_ipadImage.frame=tempRect;
_iphoneImage.transform=transform;
}];

Scale/Animate One Side of UIView to Be Smaller? - CGAffineTransformMakeScale

I would like to animate a UIView so its right side becomes smaller and then back again. This animation will trigger when a UIButton in this UIView is tapped. Here is a quick mockup of what I would like - the UIView will go from state 1 > state 2 > state 1:
It looks like it is being pushed on one side.
Here is the code I have for another action - this makes the UIView smaller and larger again as if it is being pushed on the centre, rather than the side.
self.myView.transform = CGAffineTransformMakeScale(0.95,0.95);
self.myView.alpha = 1.f;
[UIView beginAnimations:#"button" context:nil];
[UIView setAnimationDuration:0.5];
self.myView.transform = CGAffineTransformMakeScale(1,1);
self.myView.alpha = 1.0f;
[UIView commitAnimations];
How do I apply this same effect but only to the right side? Any help would be much appreciated, thanks!
According to this question u need t make Perspective Transform for example, i modified a bit from that question i linked
CATransform3D perspectiveTransform = CATransform3DIdentity;
perspectiveTransform.m34 = 1.0 / -500;
perspectiveTransform = CATransform3DRotate(perspectiveTransform, 40.0f * M_PI / 180.0f, 0.0f, 1.0f, 0.0f);
[UIView animateWithDuration:0.4 animations:^{
self.myView.layer.transform = perspectiveTransform;
}];
EDIT 2 Brings back to original
- (void)startAnimation:(id)sender
{
CATransform3D perspectiveTransform = CATransform3DIdentity;
perspectiveTransform.m34 = 1.0 / -500;
perspectiveTransform = CATransform3DRotate(perspectiveTransform, 40.0f * M_PI / 180.0f, 0.0f, 1.0f, 0.0f);
[UIView animateWithDuration:1.4 animations:^{
self.myView.layer.transform = perspectiveTransform;
}completion:^(BOOL finished) {
CATransform3D originalPerspectiveTransform = CATransform3DIdentity;
[UIView animateWithDuration:0.9 animations:^{
self.myView.layer.transform = originalPerspectiveTransform;
}];
}];
}

How to rotate the image from original and back to it?

I have try to rotate the image by using CGAffineTransformMakeRotation and CGAffineTransformRotate. I want to rotate the image from it's original and clockwise to the original again(like degree 0 to 360). I need to rotate image like a CD on the player. I did something like this:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
CGAffineTransform transform = CGAffineTransformMakeRotation(1.0);
transform = CGAffineTransformRotate(transform, -180);
ImageView.transform = transform;
[UIView commitAnimations];
it just spin to the degree 180, and if I change it to -300 it will rotate counterclockwise (that not what I need).
the angle of rotation is given better by atan2() suppose A and B are two points on the centerline to which your image is to be rotated then the angle of rotation is
0-atan2((b.x - a.x) ,(b.y -a.y))
Hope this helps
Try this:
-(void)startAnimationWithRevolutions:(float)revPerSecond forTime:(float)time
{
spinWheel.userInteractionEnabled = FALSE;
float totalRevolutions = revPerSecond * time;
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:time] forKey:kCATransactionAnimationDuration];
CABasicAnimation* spinAnimation = [CABasicAnimation
animationWithKeyPath:#"transform.rotation"];
CGAffineTransform transform = spinWheel.transform;
float fromAngle = atan2(transform.b, transform.a);
float toAngle = fromAngle + (totalRevolutions*4*M_PI);
spinAnimation.fromValue = [NSNumber numberWithFloat:fromAngle];
spinAnimation.toValue = [NSNumber numberWithFloat:toAngle];
spinAnimation.repeatCount = 0;
spinAnimation.removedOnCompletion = NO;
spinAnimation.delegate = self;
spinAnimation.timingFunction = [CAMediaTimingFunction functionWithName:
kCAMediaTimingFunctionEaseOut];
[spinWheel.layer addAnimation:spinAnimation forKey:#"spinAnimation"];
[CATransaction commit];
}
Use this:
[UIView animateWithDuration:0.1 animations:^{
CGAffineTransform transform = CGAffineTransformMakeRotation(1.0);
transform = CGAffineTransformRotate(transform, -180);
ImageView.transform = transform;
} completion:^(BOOL finished) {
CGAffineTransform transform = CGAffineTransformMakeRotation(0);
transform = CGAffineTransformRotate(transform,0);
ImageView.transform = transform;
}];

CATransform3DRotate effects gone after applying anchor point

EDIT with correct observation.
I used the following two snippets to rotate an UIImageView. After the stage1, I got the desired 3D effect: the view rotate (3D effect) and stretched out (larger size). On stage2, I am trying to swing the rightView with the hinge on the right.
If I applied the line that changes the anchor point, the view swings correctly (hinge on right) with one major issue: The view's size got restored back to original size (smaller) while the (rotation) 3D effect is still intact and then the swinging occurs.
Any suggestions?
rightView.layer.anchorPoint = CGPointMake(1.0, 0.5);
-(void)stage1
{
[UIView animateWithDuration:1.5 animations:^{
rightView.transform = CGAffineTransformMakeTranslation(0,0); //rightView i UIImageView
CATransform3D _3Dt = CATransform3DIdentity;
_3Dt =CATransform3DMakeRotation(3.141f/42.0f,0.0f,-1.0f,0.0f);
_3Dt.m34 = -0.001f;
_3Dt.m14 = -0.0015f;
rightView.layer.transform = _3Dt;
} completion:^(BOOL finished){
if (finished) {
NSLog(#"finished..");
}
}];
}
-(void)Stage2
{
rightView.layer.anchorPoint = CGPointMake(1.0, 0.5); //issue?
rightView.center = CGPointMake(1012, 384);
[UIView animateWithDuration:1.75 animations:^{
CATransform3D rightTransform = rightView.layer.transform;
rightTransform.m34 = 1.0f/500;
rightTransform = CATransform3DRotate(rightTransform, M_PI_2, 0, 1, 0);
rightView.layer.transform = rightTransform;
} completion:^(BOOL finished) {
}];
}
You need to add the "options" to your animate with duration and add options:UIViewAnimationOptionBeginFromCurrentState
Otherwise it starts from the beginning again.
So your stage 2 would look like this:
-(void)Stage2
{
rightView.layer.anchorPoint = CGPointMake(1.0, 0.5); //issue?
rightView.center = CGPointMake(1012, 384);
[UIView animateWithDuration:1.75
delay:0.00
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
CATransform3D rightTransform = rightView.layer.transform;
rightTransform.m34 = 1.0f/500;
rightTransform = CATransform3DRotate(rightTransform, M_PI_2, 0, 1, 0);
rightView.layer.transform = rightTransform;
} completion:^(BOOL finished) {
}];
}

ios CATransform3DMakeScale scaleup animation

I have the following animation:
CATransform3D scaleTransform = CATransform3DMakeScale(0.1,0.1, 1.0);
scaleAnimation.toValue = [NSValue valueWithCATransform3D:scaleTransform];
keyframeAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];;
where I'm scaling down a subview. But I'm trying to do opposite. start from .01% of the size of the subview and go to 100% of the size of the subview. Does anyone knows how can I do this?
Do this:
yourSubView.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
//change time duration according to requirement
// animate it to the identity transform (100% scale)
yourSubView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished){
// if you want to do something once the animation finishes, put it here
}];

Resources