Where should i set a BOOL for a UIView animation? - ios

I want to do a UIView animation like this
CGRect frame = view.frame;
frame.origin.y += 30.0;
[UIView animateWithDuration:0.7 animations:^{
view.frame = frame;
} completion^(BOOL finished){
viewMovedDown = YES;
}];
Should I set the BOOL (viewMovedDown) in the completion block or should I set it in the beginning itself before doing the animation?

Related

how to change position of button with Animation

change position when it is pressed.
UIButton * button = sender;
CGPoint position = button.frame.origin;
[UIButton setAnimationDuration:1.0];
CGSize size = button.frame.size;
button.frame = CGRectMake(position.x + 80,position.y,size.width,size.height);
Try this:
[UIView animateWithDuration:1.0 animations:^{
btn.frame = CGRectOffset(btn.frame, 0, 20);
}];
For animating the position of UIButton with duration 1.0 seconds
CGPoint position = button.frame.origin;
CGSize size = button.frame.size;
-(IBAction)btnClick:(id)sender {
[UIView animateWithDuration:1.0 animations:^{
button.frame = CGRectMake(position.x+80,position.y,size.width,size.height);
}];
}
Try this :
button.frame = CGRectMake(position.x + 80,position.y,size.width,size.height); // initial frame
[UIView animateWithDuration:2 animations:^{
button.frame = CGRectMake(position.x + 80,position.y,size.width,size.height); // new frame frame
} completion:^(BOOL finished) {
}];
you can use Animation block of UIView class ...
whatever you change the frame inside this block , the canvas fill the frame in between
Set the Start frame
button.frame= yourInitialFrame;
[UIView animateWithDuration:1.0 animations:^{
// set the End Frame here
button.frame = CGRectMake(position.x +
80,position.y,size.width,size.height); // new frame frame
} completion:^(BOOL finished) {
}];
also see this see this for more help
[UIView animateWithDuration:1.0 animations:^{
// set the End Frame here
button.frame = CGRectMake(position.x +
80,position.y,size.width,size.height); // new frame frame
} completion:^(BOOL finished) {
}];
Try this:
-(IBAction)ButtonClick:(id)sender
{
UIButton *aBtnRef = sender;
[UIView animateWithDuration:0.5 animations:^{
aBtnRef.frame = CGRectMake(aBtnRef.frame.origin.x + 50,aBtnRef.frame.origin.y, aBtnRef.frame.size.width, aBtnRef.frame.size.height);
} completion:^(BOOL finished) {
}];
}
For More Info :
Animation : How to make UIbutton move from right to left when view is loaded?
Moving UIButton position, animated
[UIView animateWithDuration:0.2 animations:^{
CGRect rect = button.frame;
rect.origin.x = rect.origin.x + 80;
button.frame = rect;
} completion:^(BOOL finished) {
}];

Smoothest Way To Implement Back to Back UIAnimations

I need to have my character jump (increase the origin.y by 50 and reverse) in a game I am creating.
So far I have come across two ways of doing this:
Method 1:
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionAutoreverse
animations:^{
CGRect frame = self.kevinImageView.frame;
frame.origin.y -= 50;
self.kevinImageView.frame = frame;
} completion:^(BOOL finished){
CGRect frame = self.kevinImageView.frame;
frame.origin.y = 135;
self.kevinImageView.frame = frame;
}];
Issues: At the end of every complete jump animation (up and down) the ImageView jumps up and then back down (due probably to the tiny amount it takes for the completion block to be called). This is noticeable and I would much rather do without it.
Method 2:
[UIView animateWithDuration:1.0
animations:^{
CGRect frame = self.kevinImageView.frame;
frame.origin.y -= 50;
self.kevinImageView.frame = frame;
} completion:^(BOOL finished){
[UIView animateWithDuration:1.0
animations:^{
CGRect frame = self.kevinImageView.frame;
frame.origin.y += 50;
self.kevinImageView.frame = frame;
}];
}];
Issues: If I tap the view (I use a UITapGuestureRecognizer) and the ImageView is in the completion animation (going back down), the Image View will snap back to the bottom instead of finishing its animation. Again not a major issue, but something which I should be able to avoid.
Is there any method I have not come across which will resolve both of these issues, or a way to fix one of the methods?
Can you disable your UITapGuestureRecognizer when the animation is still running? And then enable it when the animation finishes.
if (!self.isAnimationRunning) {
self.isAnimationRunning = YES;
[UIView animateWithDuration:1.0
animations:^{
CGRect frame = self.kevinImageView.frame;
frame.origin.y -= 50;
self.kevinImageView.frame = frame;
} completion:^(BOOL finished){
[UIView animateWithDuration:1.0
animations:^{
CGRect frame = self.kevinImageView.frame;
frame.origin.y += 50;
self.kevinImageView.frame = frame;
} completion:^(BOOL finished){
self.isAnimationRunning = NO;
}];
}];
}
I'm very interested in this question so I do a simple demo to do a back to back animation.
I think maybe you can just use UIViewAnimationOptionAutoreverse and UIViewAnimationRepeat option to achieve a autoreverse animation.
[UIView animateWithDuration:1.0
delay:0
options:UIViewAnimationOptionAutoreverse|UIViewAnimationRepeat
animations:^{
[UIView setAnimationRepeatCount:1];
self.kevinImageView.center = CGPointMake(self.kevinImageView.center.x ,self.kevinImageView.center.y - 25);
} completion:^(BOOL finished){
self.kevinImageView.center = CGPointMake(self.kevinImageView.center.x ,self.kevinImageView.center.y + 25);
}];
and you can also set UIViewAnimationOptionAllowUserInteraction if you want to enable use interaction during the animation.

how to use array value in UIView animation

In UIViewAnimation, so far i used a single image to animate from one place to another like this:
CGRect frame = button.frame;
CGRect frame1 = button1.frame;
frame.origin.x = frame1.origin.x;
frame.origin.y = frame1.origin.y;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 3.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView animateWithDuration:3.0 animations:^{
[button setTransform:CGAffineTransformIdentity];
} completion:^(BOOL finished) {
}];
button.frame = frame;
[UIView commitAnimations];
Here I have nine images in an array:
redAppleArray = #[redApple1,redApple2,redApple3,redApple4,redApple5,redApple6,redApple7,redApple8,redApple9,redApple10];
I have text display random numbers.
My question is how to get the above text value and animate the image using UIViewAnimation? i.e., if text =3, 3 redApple to animate .
To get the value from a label for example do:
int i = [[myLabel text] intValue];
And then use it to get the element in your array:
id *redApple = [redAppleArray objectAtIndex:i];
Try do something lilt this:
-(void)yourMethodToAnimateWithText:(NSString*)numberAsAText {
int = [numberAsAText intValue];
CGRect frame = button.frame;
CGRect frame1 = button1.frame;
frame.origin.x = frame1.origin.x;
frame.origin.y = frame1.origin.y;
// If you want to add some option use [UIView animateWithDuration:delay:option:animation:completion:]
[UIView animateWithDuration:3.0 animations:^{
//[button setTransform:CGAffineTransformIdentity];
button.frame = frame;
//Animate your images, text, etc.
} completion:^(BOOL finished) {
}];
}
Hope this is what you after.

UIView animation: simulating velocity

I am trying to use a UIView animation to simply move a view onto the screen:
[UIView animateWithDuration:.10 delay:0 options: nil animations:^
{
self.menusView.frame = endingMenuViewFrame;;
}
completion:^(BOOL finished)
{
}];
I'm wanting to add an animation so that UIView floats a little when it reaches the top before it comes down, i.e. akin to if someone jumps in the air - when they first jump, they shoot up quickly, but then gravity gradually slows them down as they reach the top of their jump, and eventually it pushes them back to earth. Does anyone know how to accomplish this?
Try with this code. This will make your view bounce three times with each time your height reduced by half. You may add more bounces.
CGFloat offset = 200.0;
CGRect originalFrame = self.menusView.frame;
[UIView animateWithDuration:1.0 animations:^{
CGRect frame = self.runnerAlertView.frame;
frame.origin.y = frame.origin.y - offset;
self.menusView.frame = frame;
} completion:^(BOOL finished){
[UIView animateWithDuration:1.0 animations:^{
self.menusView.frame = originalFrame;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.0 animations:^{
CGRect frame = self.menusView.frame;
frame.origin.y = frame.origin.y - 0.5 *offset;
self.menusView.frame = frame;
} completion:^(BOOL finished){
[UIView animateWithDuration:1.0 animations:^{
self.menusView.frame = originalFrame;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.0 animations:^{
CGRect frame = self.runnerAlertView.frame;
frame.origin.y = frame.origin.y - 0.25 * offset;
self.menusView.frame = frame;
} completion:^(BOOL finished){
[UIView animateWithDuration:1.0 animations:^{
self.menusView.frame = originalFrame;
}];
}];
}];
}];
}];
}];

Animating UIView size jumps

Tyring to animate a UIView by changing its height and position. The first animation jumps? I have the size of the UIView (theView) set in Storyboards to be height of 216. So the app loads and the theView is shown. I click theView and it quickly jumps in height to 200+ pixels then animates as it should?
- (void)singleTapAction:(UIGestureRecognizer *)singleTap {
UIView *view = singleTap.view;
[UIView animateWithDuration:1.2 delay:0 options:UIViewAnimationCurveLinear | UIViewAnimationOptionAllowUserInteraction animations:^{
CGRect theFrame = self.theView.frame;
theFrame.size.height += 100.f; <!-- when it hits here the view jumps then animates
self.theView.frame = theFrame;
}
completion:^(BOOL finished) {
if(finished){
[UIView animateWithDuration:1.2 delay:0 options:UIViewAnimationCurveLinear | UIViewAnimationOptionAllowUserInteraction animations:^{
CGRect theFrame = self.theView.frame;
theFrame.origin.y -= 100.f;
self.theView.frame = theFrame;
}
completion:^(BOOL finished) {
NSLog(#"animations complete");
}];
}
}];
}
Try moving lines
CGRect theFrame = self.theView.frame;
theFrame.size.height += 100.f; <!-- when it hits here the view jumps then animates
outside the block. Put it before the animation method is called.

Resources