I have a view which I would like to transform in two ways. First I'd like to move it on its y-axis. Then, I'd like to zoom in on it.
However, when I use the following code, the object is first moved and then moved back to its original position while being zoomed.
Is there a way to apply the two transformation at once without cancelling the first?
Sorry if this is basic, but any help would be very much appreciated!
[UIView animateWithDuration:animationDuration
delay:0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
currentCover.transform = CGAffineTransformMakeTranslation(0, 0-keyboardTop+35);
}
completion:^(BOOL finished) {
[UIView animateWithDuration:animationDuration
delay:0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
[currentCover setTransform:CGAffineTransformMakeScale (1.3, 1.3)];
}
completion:^(BOOL finished) { }
];
}
];
You should multiply one transform by the other. Each transform (scale and translate) are transform matrices. To combine them, simply multiple one by the other before using it. The order of the multiplication determines the order that the tranforms are applied
I'd similar issue where my view moves back to original position.
Documentation of CGAffineTransformMakeTranslation reads "it constructs a new translation matrix from x and y values that specify how much to move the origin." I don't think it applies actual transformation.
I think you'd rather apply translation by calling CGAffineTransformTranslate(). At least it works for me!!
Here we are creating the two changes (CGAffineTransform's) you want to do together, and apply the transformation to the view.
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, 0-keyboardTop+35);
CGAffineTransform scale = CGAffineTransformMakeScale(1.3, 1.3);
CGAffineTransform transform = CGAffineTransformConcat(translate, scale);
[UIView animateWithDuration: animationDuration
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
currentCover.transform = transform;
}completion:^(BOOL finished){
}];
Related
I'm trying to animate the size of a UIView with a random factor repeatedly as follows, but I need to make sure it doesn't shrink/grow beyond limits.
This is my attempt:
-(void)animateView{
float percentage =50;
float rnd = (100 +((float)rand())/RAND_MAX * percentage - percentage/2)/100;
[UIView animateWithDuration:5.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState
animations:^{
myView.transform = CGAffineTransformScale(myView.transform, rnd, rnd);
}
completion:^(BOOL finished){
[self animateView];
}
];
}
So, somewhere in this loop, I need to get the original size of myView, and get a relative growth to that size, rather than to the current size of myView.
I am aware of CGAffineTransformIdentity, but can't figure out how to put that into this loop.
Any suggestions?
try to create the instance of CGAffineTransform initialTransform; and preserve initialTransform = myView.transform; initial value in did load later use it below way : -
myView.transform = initialTransform;
in top of your animateView method.
I am facing this weird issue, where CGAffineTransformMakeScale is causing rotation. The name suggests that it should only cause scaling, but that's not the case.
[UIView animateWithDuration:1.0 animations:^{
self.logoView.transform = CGAffineTransformMakeScale(6.0, 6.0);
} completion:^(BOOL finished) {
if (finished) {
[UIView animateWithDuration:3.0 animations:^{
self.logoView.transform = CGAffineTransformMakeScale(-6.0, -6.0);
} completion:nil];
}
}];
I would assume that the view should scale 6x and scale back 6x. However, the second animation causes a 90-degree anti-clockwise rotation of the image! Can anyone explain what's going on?
Use the relative scaling transform rather than making an absolute one. So:
self.logoView.transform = CGAffineTransformScale(self.logoView.transform,
6, 6)
you shouldn't be scaling to CGAffineTransformMakeScale(-6.0,-6.0) in order to reverse what you already did (unless thats what you want, but i doubt it) but instead animate back to CGAffineTransformMakeScale(1.0,1.0). a shortcut is CGAffineTransformIdentity constant, which is an empty transform. So change that last line in the completion block to
self.logoView.transform = CGAffineTransformIdentity;
to explain whats currently going on, by scaling to a negative value in both x and y axes you are turning the view 'inside out' in both dimensions at the same time
So I want to do a UI animation where a view gradually fades out. Right now I have something like this
[UIView animateWithDuration:2.0f
animations:^{
myView.alpha = 0.0f;
}
];
So right now over the course of those 2 seconds, the view will linearly gradually fade out. However I want to make it so that the fade out is non-linear. For instance, I want it to start fading out slowly and then fade more quickly over time.
How would I do this?
Use one of the longer forms of animateWithDuration methods that take an options parameter, like animateWithDuration:delay:options:animations:completion:.
One of the options you can supply is a timing curve like UIViewAnimationOptionCurveEaseInOut.
Your choices are
UIViewAnimationOptionCurveEaseInOut = 0 << 16,
UIViewAnimationOptionCurveEaseIn = 1 << 16,
UIViewAnimationOptionCurveEaseOut = 2 << 16,
UIViewAnimationOptionCurveLinear = 3 << 16,
If you use CAAnimation instead of UIView animation, you can also create your own custom timing function using the control points of a bezier curve that describes the desired timing function. However, CAAnimation is more complex to use and less well documented.
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
myView.alpha = 0.0f;
} completion:nil];
Or pick your favorite time curve instead of UIViewAnimationOptionCurveEaseInOut
You must be needing about "ease in" timing curve in which the animation starts slowly and ends as linear. Try the below code,
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
myView.alpha = 0.0f;
} completion:nil];
Please do let me know, if it solves your problem.
I had an IPhone application in which i want to transform my image view to its double size without changing the position of the image view.(i.e. the centre need not be changed)I am doing like this
CGAffineTransform firstTransform = bg.transform;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.5];
bg.transform = CGAffineTransformScale(bg.transform,2,2);
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.5];
bg.transform = firstTransform;
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
But when transforming its position gets changed.so
Now i am doing like this to do not change the position of the image view.i want the image view to be transformed to the double size on click with an animation like this
animation on the red button when clicking next
can anybody guide me on this?
Make sure the anchor point is set to the centre, bearing in mind when setting the anchor point the value has to be between 0 and 1: 0 being the left/top side and 1 being the right/bottom side. So to set the anchor point to the middle, set it to 0.5 on both the X and Y axis:
[itemName.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
Then when you scale the object, it should scale up from around the centre point.
I also suggest using animateWithDuration:completion. The code will become much simpler:
[UIView animateWithDuration:1.0f animations:^{
[bg setTransform:CGAffineTransformScale(bg.transform, 2.0, 2.0)];
} completion ^{
[UIView animateWithDuration:1.0f animations:^{
[bg setTransform:CGAffineTransformScale(bg.transform, 0.5, 0.5)];
];
}];
Hope this helps you achieve the desired effect!
Make sure that you are not using auto layout. To disable that open your xib, click on first tab in the right panel, uncheck the Use Auto Layout under Interface Builder Document. Screen shot is attached for help.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to use CGAffineTransformMakeRotation?
I have an imageview that acts like a button, I am looking for a way to show the user that he clicked on it, I came up with this code that will rotate it a bit :
float angle = 60.0;
[UIView animateWithDuration:0.5f animations:^{
customimageView.transform = CGAffineTransformMakeRotation(angle);
}];
but its not doing the behavior I am looking fpr.
Anyone know how can I make the imagekinda zoom in ( or enlarge/ blowup ) to maybe 1.5 its size on its click? what kind of animation is relevant here?
feel free to share behaviors you guys thought was cool in this situation.
Thanks.
As a side note, how can I make it just rotate full circle? I tried passing in 360 but its not doing it??
You can always CGAffineTransformConcat your rotation with a scaling transformation, made with CGAffineTransformMakeScale.
If you're interested in another button pressed effect, I needed something cool, too, and settled on a "throb" animation as follows:
BOOL keepThrobbing = NO;
-(void)buttonThrob:(CGFloat)inset {
UIViewAnimationOptions opts = UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionAllowUserInteraction;
[UIView animateWithDuration:0.2 delay:0.0 options:opts animations:^{
self.button.frame = CGRectInset(self.button.frame, inset, inset);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 delay:0.0 options:opts animations:^{
self.button.frame = CGRectInset(self.button.frame, -inset, -inset);
} completion:^(BOOL finished) {
if (finished && keepThrobbing) {
[self buttonThrob:inset];
}
}];
}];
}
Call it like this:
keepThrobbing = YES;
[self buttonThrob:10.0]; // expand/shrink by 10px in x and y
Use CGAffineTransformMakeScale to change the scale.
CustomimageView.transform = CGAffineTransformMakeScale(1.5,1.5);
You can combine two affine transforms with CGAffineTransformConcat:
[customimageView setTransform:CGAffineTransformConcat(CGAffineTransformMakeScale(1.5, 1.5), CGAffineTransformMakeRotation(angle))];