Center of second animation not using new position - ios

I am having an issue with running two animations in iOS. First i am trying to move the image from the bottom of the screen to the top by changing frame in the first animation set, then on completion i am trying to extend the length of the object by transforming the scale. But for some reason once the first animation is finished the object goes back to the original place at the bottom of the screen and the growing transformation happens off the screen. Can anyone help me with this? Thanks in advance for your help, here is my code:
CGRect fullview = CGRectMake(59, 102, 650, 150);
CGRect iconView = CGRectMake(59, 102, 290, 150);
CGRect textView = CGRectMake(349, 102, 360, 150);
[_profileBtn setEnabled:FALSE];
[_profileBtn setHidden:TRUE];
[UIView animateWithDuration:1 animations:^{ // animate the following:
_profileIcon.frame = iconView; // move to new location
_profileText.frame = textView; // move to new location
_profileBackground.frame =fullview;
}completion:^(BOOL finished){
[UIView animateWithDuration:1 animations:^{ // animate the following:
_profileBackground.transform = CGAffineTransformMakeScale(1, 5.64);
}];
}];

Check out the documentation for the UIView transform property. Specifically, what happens to the object's frame if you adjust the transform. I think instead of performing the second animation on the UIView's transform, you should apply it to the scale of its layer's transform.

I figure out a solution that was close to what i wanted. This will do the translation and the movement at the same time:
CGRect iconView = CGRectMake(59, 102, 290, 150);
CGRect textView = CGRectMake(349, 102, 360, 150);
_profileIcon.frame = iconView; // move to new location
_profileText.frame = textView; // move to new location
CGAffineTransform scale = CGAffineTransformMakeScale(1, 5.00);
CGRect fullview = CGRectMake(59, 40, 650, 150);
_profileBackground.frame = CGRectApplyAffineTransform(fullview,scale);

Related

Animate UIView with Anchor Point and stretchable image

I'm trying to animate a bar chart where the images rise from the bottom. After a few hours, I'm seeing this is not an easy thing to achieve.
I simply want to do one thing:
Create an animation that scales up from a bottom center anchor point.
- (void)startCharts{
//set the stretchable images
UIImage* stretchGray = [UIImage imageNamed:#"rescueGrayChart"];
stretchGray = [stretchGray resizableImageWithCapInsets:UIEdgeInsetsMake(50, 0, 10, 0) resizingMode:UIImageResizingModeStretch];
self.grayChart.image = stretchGray;
//set the start frame and anchor point
CGRect gframe = self.grayChart.frame;
CGPoint bottomCenterGray = CGPointMake(CGRectGetMidX(gframe), CGRectGetMaxY(gframe));
self.grayChart.layer.anchorPoint = CGPointMake(0.5, 1);
[UIView animateWithDuration:5
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
//this creates the issue where the image scales outwards from center (see image)
CGRect frame = self.grayChart.frame;
frame.size.height = 300;
self.grayChart.frame = frame;
//this animates up from bottom, but stretches the image
/*
[CATransaction begin];
self.grayChart.layer.transform = CATransform3DMakeScale(1, 2, 1);
[CATransaction commit];
*/
}
completion:nil];
//readjust the frame
self.grayChart.layer.position = bottomCenterGray;}
** These images are screen shots taken during the animation
Thank you for your time.

UIImageView frame isn't dynamic

This is my method to move a player (aka ball synchronized with label):
int x2 = self.ball.frame.origin.x;
int y2 = self.ball.frame.origin.y;
double distance = sqrt(pow((x2 - coords.x), 2.0) + pow((y2 - coords.y), 2.0));
// 140 is just an arbitrary multiplier
NSTimeInterval time = distance / 140;
animating = YES;
[UIView animateWithDuration:time delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[self.ball setFrame:CGRectMake(coords.x, coords.y, 48, 48)];
[self.label setFrame:CGRectMake(coords.x - 30, coords.y - 25, 108, 20)];
} completion:^(BOOL finished) {
animating = NO;
}];
This code works on one tap, but when a player redirects his move (touches somewhere else while still animating, hence the UIViewAnimationOptionBeginFromCurrentState), the distance is wrong. This is because the frame is set to the end point of the first touch, instead of where the animation ended off.
You probably want to get the starting frame position from the object layer.presentationLayer property.
The docs say:
The layer object returned by this method provides a close
approximation of the layer that is currently being displayed onscreen.
While an animation is in progress, you can retrieve this object and
use it to get the current values for those animations.

Making a map bigger on tap - iOS

If I were wanting to make a map on my app bigger on touch, how would I do that? Would I use core animation? Example, I want to change my map from 50px height to 70px height on touch.
Thanks :D
First you need to know when a user is touching the MKMapView and in the method that the user touched the map you do that:
[UIView animateWithDuration:0.5 animations:^{
CGRect rect = myMapView.frame;
rect.size = CGSizeMake(50, 70);//Set to Bigger Size
myMapView.frame = rect;
}];
And when the user leaves the touch from map:
[UIView animateWithDuration:0.5 animations:^{
CGRect rect = myMapView.frame;
rect.size = CGSizeMake(50, 50);//Set to Original Size
myMapView.frame = rect;
}];
If you want the resize animated then yes you can use core animation. However if you just want to resize, you can just redraw the map view by doing the following:
CGRect oldFrame = yourMapView.frame;
oldFrame.size.height = oldFrame.size.height + 20; //aka 50+20 = 70
yourMapView.frame = oldFrame;

Animate size of View keeping its bottom left point fixed

I have a UIView at the bottom left of its superview, i.e.its frame is like
[myView setFrame:CGRectMake(0,superView.bounds.size.height-myViewHeight,myViewWidth,myViewHeight)];
I want to animate its size such that its bottom left point remains fixed at its position. I am not sure how to play with anchor point property of layers.
Currently I am increasing its size using the following lines of code.
[UIView animateWithDuration:0.2 animations:^{
[bottomLbl setFrame:CGRectMake(0, bottomView.bounds.size.height-250, 300, 250)];
This works fine but when I try to bring back to its original size, its bottom point gets lifted at the start of animation and settles down at the end of animation.
First define the point for the bottom left corner...
CGPoint bottomLeft = CGPointMake(0, 480); // this can be anything.
Then you need to define the sizes (big and small).
CGSize bigSize = CGSizeMake(280, 280);
CGSize smallSize = CGSizeMake(50, 50); // again, these can be anything.
Then animate them...
// create the rect for the frame
CGRect bigFrame = CGRectMake(bottomLeft.x, bottomLeft.y - bigSize.height, bigSize.width, bigSize.height);
CGRect smallFrame = CGRectMake(bottomLeft.x, bottomLeft.y - smallSize.height, smallSize.width, smallSize.height);
[UIView animateWithDuration:0.2
animations:^{
// set the rect onto the view
bottomLbl.frame = bigFrame;
// or
bottomLbl.frame = smallFrame;
}];
As long as you set the frame and the calculated end frames are correct and it's all done in one animation then the bottom left corner won't move.
If this doesn't help can you please post a video of what is happening (and all your animation code).
EDIT
[UIView animateWithDuration:0.2
delay:0.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
// set the rect onto the view
bottomLbl.frame = bigFrame;
// or
bottomLbl.frame = smallFrame;
}
completion:nil];

Animating ImageView X position and resetting

I am wanting to animate a ImageView's X Position and reset it if it reaches a certain X Position. For example, Start out a 700 and move to the left.. if it reaches 100 I want to reset it to 700. And do this continually over and over.
Pretty sure a timer would be needed, but not sure how to go about this as this is my first IOS app. Searching google turned up alot of animation stuff, but all was different which led to confusion. :)
Use this code
call [self animate:your_image_view];
- (void)animate:(UIImageView*)your_image
{
if (need_to_animate)
{
CGRect from = CGRectMake(10, 100, 200, 200);
CGRect to = CGRectMake(10, 700, 200, 200);
[UIView animateWithDuration:2 animations:^{
your_image.frame = from;
} completion:^(BOOL finished) {
your_image.frame = to;
[UIView animateWithDuration:2 animations:^{
your_image.frame = from;
} completion:^(BOOL finished) {
[self animate:your_image];
}];
}];
}
}
See here how to use UIView animations iPhone UIView Animation Best Practice Alternatively set up a timer loop and then adjust the frame/center of your view

Resources