Animation to and from value - iphone

I am trying to animate a label across the screen however I cannot seem to get it work properly. Currently it animates off the view into the other views.
Also I cannot "stop" the animation when needed. I am using it in a music player to scroll the track name across the UIView and when I tap Next or Prev it must stop mid-animation and start again.
- (void)startAnimation {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:10];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:)];
CGRect frame = trackName.frame;
frame.origin.x = -50;
trackName.frame = frame;
[UIView commitAnimations];
}
- (void)animationDidStop:(id)sender {
NSLog(#"animationDidStop");
CGRect frame = trackName.frame;
frame.origin.x = 194;
trackName.frame = frame;
[self startAnimation];
}

I assume what you are trying to do is animate the label so that it moves 50 pixels to the left. If so, you need to change the line:
frame.origin.x = -50
to
frame.origin.x -= 50;
The problem is that you are setting the new origin of the frame to -50, which is 50 pixels off the left side of the view. Instead you need to just subtract 50 pixels from it's current position so that it moves 50 pixels relative to where it was.

Related

UIView animate frame of ImageView

I'm trying to add an animation to a UIImageView when a button is tapped. When the button is tapped I want the imageView to move to the right at the same time as it is increasing in size. I have tried this code, but the imageView aren't animation correct.
- (IBAction)bA:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:0.5];
CGRect frame = imageView.frame;
frame.origin.x = frame.origin.x + 30;
frame.size.hight = frame.size.hight + 20;
frame.size.width = frame.size.width + 20;
imageView.frame = frame;
[UIView commitAnimations];
}
This code makes the imageView move little to the right, then back again. Wierd behavior, what should I do to animate both size and origin at the same time?
The problem is that you've got Autolayout turned on. With Autolayout, you cannot change the frame of something, because layout comes along and changes it back again.
Either turn Autolayout off, or else animate by changing the constraints of this view instead of its frame.

UITableView resizing when moving the container UIView

I have an application where I'm moving my entire UIView up when using the keyboard, in order to not hide some important UITextFields.
My problem is that the UITableView that I have there does not behave the same way as all the other views. Instead of moving the entire UITableView, it moves only its top edge, and that resizes the UITableView which is not the desired effect.
My problem is better described here: http://www.youtube.com/watch?v=AVJVMiBULEQ
This is the code that I'm using for the animation:
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5]; // if you want to slide up the view
[UIView setAnimationBeginsFromCurrentState:YES];
CGRect rect = self.view.frame;
if (movedUp) {
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= 140;
rect.size.height += 140;
} else {
// revert back to the normal state.
rect.origin.y += 140;
rect.size.height -= 140;
}
self.view.frame = rect;
[UIView commitAnimations];
}
As solved in comments above, code above works when view is not autoresizing. The simpler approach would be to use CGAffineTransform transformations that calculate view frame instead of you.
-(void)setViewMovedUp:(BOOL)movedUp {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5]; // if you want to slide up the view
[UIView setAnimationBeginsFromCurrentState:YES];
if (movedUp) {
self.transform = CGAffineTransformMakeTranslation(0, -150);//move up for 150 px
} else {
self.transform = CGAffineTransformIdentity;//reset to initial frame
}
[UIView commitAnimations];
}

zooming from a particular point

I am using this code to zoom from a particular point
CGPoint getCenterPointForRect(CGRect inRect)
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
return CGPointMake((screenRect.size.height-inRect.origin.x)/2,(screenRect.size.width-inRect.origin.y)/2);
}
-(void) startAnimation
{
CGPoint centerPoint = getCenterPointForRect(self.view.frame);
self.view.transform = CGAffineTransformMakeTranslation(centerPoint.x, centerPoint.y);
self.view.transform = CGAffineTransformScale( self.view.transform , 0.001, 0.001);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration];
self.view.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
Its not working. What is the correct way to do zooming from a particular point.
I think that, if I've diagnosed your problem correctly, you're getting a scaling animation where the view starts tiny and at a point, then scales and moves to the centre of the screen exactly like you want, but the point it starts at is incorrect?
First of all, views scale around their centre. So if you took out the translation and hence reduced the code you have to:
self.view.transform = CGAffineTransformMakeScale( 0.001, 0.001);
And your view ends up taking up the whole screen then it'll remain centred on the middle of the screen, sort of a little as though it's a long way away and you're heading directly towards it.
Supposing you instead want it to grow and move to the centre of the screen from (x, y) then you need something more like:
CGPoint locationToZoomFrom = ... populated by you somehow ...;
CGPoint vectorFromCentreToPoint = CGPointMake(
locationToZoomFrom.x - self.view.center.x,
locationToZoomFrom.y - self.view.center.y);
self.view.transform = CGAffineTransformMakeTranslation(vectorFromCentreToPoint.x, vectorFromCentreToPoint.y);
self.view.transform = CGAffineTransformScale( self.view.transform , 0.001, 0.001);
Where locationToZoomFrom will be the initial centre of the view and its normal centre as per its frame will be the destination.

scaling a UIImageView at the center of a UIView

I'm trying to make a scaling ring, which scales in the center of a UIView.
Setup is the following:
In IB, I have a view and my ImageView attached to it (just a transparent PNG with a ring).
Setup an IBOutlet to the ImageView (theRing) and a button to trigger the action.
ImageView is set to «scaleToFill» in IB.
The code for the action is:
-(IBAction)buttonPressed:(id)sender{
CGRect theFrame = theRing.frame;
theFrame.size.width = 16;
theFrame.size.height = 16;
[theRing setFrame:theFrame]; //Scale down the ring first
theRing.center = [self.view center]; // Center the ring in the center of the view
theFrame.size.width = 300;
theFrame.size.height = 300;
[theRing setAlpha:1.0];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[theRing setFrame: theFrame];
[theRing setAlpha:0.0];
theRing.center = [self.view center];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector: #selector(animEnd:finished:context:)];
[UIView commitAnimations];
}
Now - when I hit the button the first time, the image is scaled with the reference point being the upper left corner of the ImageView (means, it scales, but the ring expands to the lower right of the screen).
BUT: When I hit the button again, the whole thing works as expected (means, the ring stays in the middle of the screen and is scaled).
Any ideas?
try to use ImageView mode Aspect Fill

iPhone change height with animation, not downward sweep

I am using the code:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[info setFrame:CGRectMake( 0, 96, 320, 384)];
[UIView commitAnimations];
to have an animation of the object "info" to change its heght to 384... Nothing else is changed, but the only way to do that is to change the whole frame, and in the animation, instead of the height growing, the object just snaps to the top of the screen with the proper width and height, and just moves downward into place, instead of staying in place and just simply growing. How can I fix it so it does just grow downward?
EDIT: The info is a UITextView, and the downward sweep only happens when you are not scrolled to the top.
Try this:
//... animation setup
CGRect frame = info.frame;
frame.size.height = 384;
info.frame = frame;
[UIView commitAnimations];
Instead of [info setFrame:CGRectMake( 0, 96, 320, 384)]; try info.frame.size.height = 384