iOS how to animate view scaling from the center of the view? - ios

I'm using an affine transform within a repeating/reversing animation to create a "pulsing" effect. However, I noticed that the button gets shifted right when the animation begins. The anchor point for the button is at (0.5,0.5):
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionRepeat|
UIViewAnimationOptionAutoreverse|
UIViewAnimationOptionAllowUserInteraction|
UIViewAnimationCurveEaseInOut
animations:^{
self.recordButton.transform = CGAffineTransformMakeScale(1.2, 1.2);
} completion:^(BOOL finished) {
}];
It appears that when the transform is applied, the origin of the button's frame stays at the same spot.
How can I animate scale transform so the center of the button remains in the same spot once the transform is applied?

I usually play with the frame and CGRect utility functions
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionRepeat|
UIViewAnimationOptionAutoreverse|
UIViewAnimationOptionAllowUserInteraction|
UIViewAnimationCurveEaseInOut
animations:^{
self.recordButton.frame = CGRectInset(self.recordButton.frame, -1.2, -1.2);
} completion:^(BOOL finished) {
}];

Related

Bouncing animation while changing view frame

Add bouncing animation while changing UIVIew frame. I only know how to change frame but I don't know how to set bouncing effect while changing view frame. Below is my code....
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
CGRect frame = viewContent.frame;
frame.size.height = CGRectGetHeight(viewContentFullDetail);
viewContent.frame = frame;
} completion:^(BOOL finished) { }];
You can check animated gif image on below link. I want to add animation not same like this but near to this. Main animation are unfolding and bouncing effect.
https://drive.google.com/open?id=0B9k_Shyb5v62eFdxWXhYeXV3a0E
Please help. I don't know how to do this.
You need to try this
int duration, damping, velocity;
[UIView animateWithDuration:duration delay:0
usingSpringWithDamping:damping initialSpringVelocity:velocity
options:0 animations:^{
// your animation code here
} completion:nil];
Play with the damping and velocity values to know more about this feature in the animation code.
here is the link for a tutorial

Showing UIView with bouncy animation

I want to create a bouncy animation like in the Skitch application - when i press a button, a subview (simple view with several buttons) will appear from the side and will bounce a bit until it stops.
I can't find any standard animation that does that, so how i can implement this animation?
You can try this :
// Hide your view by setting its position to outside of the bounds of the screen
// Whenever you want, call this :
[UIView animateWithDuration:0.5
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0
options:UIViewAnimationOptionCurveLinear
animations:^{
// Set your final view position here
}
completion:^(BOOL finished) {
}];
This method is the same as the usual animateWithDuration: but it introduces a spring animation.
Use UIView method:
+ (void)animateWithDuration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
usingSpringWithDamping:(CGFloat)dampingRatio
initialSpringVelocity:(CGFloat)velocity
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion
This will do what you want
Use the UIView method, If you want bouncing effect. Then you need to nested the animation process and give the frame as needed for that view. Here's the example:
CGRect frameOnStart = CGRectMake(200,200 ,60 ,60); //On start show small view
CGRect frameOnAnimate = CGRectMake(80,80 ,150 , 150); //while animate show large view
CGRect frameAfterAnimate = CGRectMake(100,100,120 ,120); //after animate large take it back to original frame. SO it loos like bouncing animation
// Whenever you want, call this :
[UIView animateWithDuration:0.5
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0
options:UIViewAnimationOptionCurveLinear
animations:^{
// Set your on Animate position here
view.frame = frameOnAnimate;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.5
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0
options:UIViewAnimationOptionCurveLinear
animations:^{
// Set your final view position here
view.frame = frameAfterAnimate;
}
completion:^(BOOL finished) {
}]
}];
Thanks.

Correctly Using CGAffineTransformMakeScale

I have a UIButton laid out using storyboard. The button just contains an image. When the button is clicked, I want to animate the size of the button - decrease in size and then bring it back to the original size again.
I used the following code -
[UIView animateWithDuration:2.0 animations:^{
_favButton.transform = CGAffineTransformMakeScale(0.5, 0.5);
}completion:^(BOOL finished) {
[UIView animateWithDuration:2.0 animations:^{
_favButton.transform = CGAffineTransformMakeScale(1, 1);
}];
}];
This code moves my button on the screen which I do not want. I want the center of the button to be fixed and the size be animated.
I have not used any Top Constraint in the storyboard for the button. How can I rectify this behaviour?
If you have auto layout turned on, you would need to turn it off.
But it doesn't seem to your problem here as per your description.
I would do the following to re-adjust to the center as it scales:
CGPoint cP = _favButton.center;
[UIView animateWithDuration:2.0 animations:^
{
_favButton.transform = CGAffineTransformMakeScale(0.5, 0.5);
_favButton.layer.position = cp;
}
completion:^(BOOL finished)
{
[UIView animateWithDuration:2.0 animations:^
{
_favButton.transform = CGAffineTransformMakeScale(1, 1);
_favButton.layer.position = cp;
}];
}];
Hope this helps.
SWIFT 3
button.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)

How to animate UISearchbar frame change keeping the rounded caps?

I would like to animate the frame change of the UISearchbar. I would like it to expand and collapse when tapped on a search button.
Am using UIViewAnimationBlock to animate
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionLayoutSubviews
animations:^{
//Set the frame you want to the search bar
}
completion:^(BOOL finished) {
}];
I have to expand the frame from zero and collapse it back. But while animating, the search bar loses its rounded edges and becomes a rectangle. Here is a screenshot of it while still animating
Well it doesn't look good.. I want it to keep its rounded edges while expanding or collapsing. Is it possible ? If yes how ?
Any help would be much appreciated.
You can not change height of a UISearchBar try setting frames with height of 44px.
And if you want to change height then use CGAffineTransformMakeScale like..
//FOR EXPAND USE THIS:
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionLayoutSubviews
animations:^{
self.searchBar.transform = CGAffineTransformMakeScale(1.0, 1.0);
}
completion:^(BOOL finished) {
}];
and
//FOR EXPAND USE THIS:
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionLayoutSubviews
animations:^{
self.searchBar.transform = CGAffineTransformMakeScale(0.001, 0.001);
}
completion:^(BOOL finished) {
}];

ImageView Animation Repeatition

I am working on an application which needs animations with imageView.
At starting the position of image is at CGPoint(735,112),on clicking the animation button it has to move to the left of the screen ie to point CGPoint(20,112) and hast to repeat the animation from left to right i.e to point CGPoint(735,112),has to stop after this. i am using the following Code.
CGRect newRect=cleanStrip.frame;
[UIView beginAnimations:#"animation1" context:nil];
[UIView setAnimationDuration:3.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationRepeatAutoreverses:YES];
newRect.origin.x=20;
cleanStrip.frame=newRect;
[UIView commitAnimations];
After repeating the animation the image view has to stop at it original position CGPoint(735,112).But the above code stops the image at CGPoint(20,112).
How can i stop the image at its original position after animation.
Thanks,
you can use block to start your animation
__block CGRect newRect=cleanStrip.frame;
__block int originx = newRect.origin.x;
[UIView animateWithDuration:2.0
animations:^{
newRect.origin.x=20;
cleanStrip.frame=newRect;
}
completion:^(BOOL finished){
[UIView animateWithDuration:2.0
animations:^{
newRect.origin.x=originx ;
cleanStrip.frame=newRect;
}];
}];

Resources