how to animate stuff in swift (xcode 6 beta) - ios

i was wondering how do i animate things in apple's new language swift.
in objective c i would use the following code to move an image from the top of the screen to the end :
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
UIImageView.center = CGPointMake(UIImageView.center.x , UIImageView.center.y + 200);
} completion:^(BOOL finished) {
[self move];
}];
so the questoin is:
how do i animate stuff in swift with the same effect that the code above(obj c)has,
i would appreciate some explaination about the way you can do that aswell.

UIView.animateWithDuration(1, delay: 0, options: .CurveLinear, animations: {
UIImageView.center = CGPointMake(UIImageView.center.x, UIImageView.center.y + 200);
}, completion: {
(finished: Bool) in
move();
});
This is how.

Related

animate UIImage in objective-c

I am trying to have my uiimageview move in a straight line horizontally out of bounds and loop again starting from the other side... this is the code I have in swift..
and was wondering if anyone knows how I can implement it in objective c?
let carSpeed = 20.0 / Double(view.frame.size.width)
let duration: NSTimeInterval = Double(view.frame.size.width - car.frame.origin.x) * carSpeed
UIView.animateWithDuration(duration, delay: 0.0, options: .CurveLinear, animations:
{
car.frame.origin.x = self.view.bounds.size.width
}, completion: {_ in
//reset cloud
car.frame.origin.x = -self.carImageView.frame.size.width
self.animateCars(car)
also if anyone knows any tutorials on objective C animations! thanks!
your can set the frame of image view first
UIImageView * imageView=[[UIImageView alloc]initWithFrame(x,y,width,height)];
Then set the frames in the following method
int moveValue=10 // add your desired value
[UIView animateWithDuration:0.25 animations:^{
imageView.frame=CGRectMake(x+moveValue,y,width,height);
} completion:nil];
This is your exact code converted to Objective-C:
double carSpeed = 20.0 / self.view.frame.size.width;
NSTimeInterval duration = (self.view.frame.size.width - car.frame.origin.x) * carSpeed;
[UIView animateWithDuration:duration
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
car.frame = CGRectMake(self.view.bounds.size.width,
car.frame.origin.y,
car.frame.size.width,
car.frame.size.height);
}
completion:^(BOOL finished) {
if (finished) {
car.frame = CGRectMake(-self.carImageView.frame.size.width,
car.frame.origin.y,
car.frame.size.width,
car.frame.size.height);
[self animateCars:car];
}
}];

How to animate an UILabel from small to its original size?

Actually i am using RESlider in my app. In the menu table view there is a profile image and aside to it there is a notification label. Now i want is that when the user presses the hamburger menu the notification label(orange label with 999 number) should animate from a tiny dot to its original size. How to achieve this??
Put this in viewDidAppear
-(void)viewDidAppear:(BOOL)animated{
self.label.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.5 animations:^{
self.label.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
}];
}
myTextLabel.transform = CGAffineTransformMakeScale(0.3, 0.3);
[UIView animateWithDuration:2.0
delay: 0.1
options: UIViewAnimationOptionBeginFromCurrentState
animations:^{
myTextLabel.transform = CGAffineTransformMakeScale(1.5, 1.5); //grow
}
completion:^(BOOL finished){
myTextLabel.transform = CGAffineTransformMakeScale(1, 1);
}];
Change the transform scale of your label, like this :
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut
animations:^{
timerLabel.transform = CGAffineTransformScale(timerLabel.transform, 0.7, 0.7);
}
completion:nil];

how to add right to left animation and zoom in animation using CGAffineTransformScale

I am new to core graphics, I want to show a view from right to left animation using CGAffineTransformScale after completion of this animation I have apply zooming animation to same view.
I can show the right to left animation. But I am unable to show the zooming animation.
Can some one help me to solve the above problem.
Please find the below code what i tried.
toView.layer.anchorPoint = CGPointMake(1, 0.5);
toView.center = CGPointMake(toViewController.view.bounds.size.width,toViewController.view.bounds.size.height/2.0);
toView.transform = CGAffineTransformScale(CGAffineTransformIdentity, -1.0, 1.0);
[UIView animateWithDuration:ANIMATION_DURATION delay: 0.0 options: UIViewAnimationOptionTransitionFlipFromLeft animations:^{
[toView setTransform: CGAffineTransformMakeScale(1.0, 1.0)];
} completion:^(BOOL finished) {
}];
Your scale values are 1.0 that default value, change it to any other value other than 1.0, may be 1.2 to zoom-in, or 0.8 to zoom out.
And I think there should be few additions to your animation block.
[UIView animateWithDuration:ANIMATION_DURATION delay: 0.0 options: UIViewAnimationOptionTransitionFlipFromLeft animations:^{
[toView setTransform: CGAffineTransformMakeScale(1.2, 1.2)]; //Zoom in
} completion:^(BOOL finished) {
if(finished){
[UIView animateWithDuration:ANIMATION_DURATION animations:^{
toView.transform=CGAffineTransformIdentity; //Make things normal.
}];
}
}];
I hope it works.
Cheers.

Animation Grow/Decrease Size imageView iOS

I'm trying to animate a custom button using CGAffineTransformMakeScale as follows:
if (stateButton == 0) { //The button is gonna appear
self.selected = YES;
self.imageView.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
// animate it to the identity transform (100% scale)
self.imageView.transform = CGAffineTransformIdentity;
} completion:nil];
}
else if (stateButton ==1) { //The button is gonna disappear
self.imageView.transform = CGAffineTransformMakeScale(1, 1);
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
// decrease button
self.imageView.transform = CGAffineTransformMakeScale(.01, .01);
} completion:^(BOOL finished){
self.selected = NO;
}];
}
The button grows perfectly to the original size, however, i don't know the reason but when i click on the button to decrease it, it decreases from a size like 100% bigger than the original size to the original size, instead of beginning the decrease in the original size and achieve an scale of 0.01 as I indicated in the code.
Please help!!
You can animate the grow and decrease size of the image view using the following code
[UIView animateWithDuration:2.0 animations:^{
self.imageView.transform = CGAffineTransformMakeScale(0.5, 0.5);
}
completion:^(BOOL finished){
[UIView animateWithDuration:2.0 animations:^{
self.imageView.transform = CGAffineTransformMakeScale(1, 1);
}];
}];
This will make the imageview decrease in size initially and when animation is over it will get back to its original size with an animation.
SWIFT 3 Version
UIView.animate(withDuration: 2.0, animations: {() -> Void in
self.imageView?.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
}, completion: {(_ finished: Bool) -> Void in
UIView.animate(withDuration: 2.0, animations: {() -> Void in
self.imageView?.transform = CGAffineTransform(scaleX: 1, y: 1)
})
})

UILabel animated fade in out isn't working

I am trying to build an introduction to my app with UILabels fading in and out. I have two labels. I want the first one to fade in, stay on the screen for 4 seconds. Then second label should fade in and stay on the screen for 4 seconds. Then it should fade out both the labels.
I have the following code but it does not do anything since it goes straight to the final state. I have the following method in viewDidAppear(). What am I doing wrong?
-(void) animateLabels
{
[UIView beginAnimations:#"First Label Display" context:nil];
[UIView setAnimationDelay:4.0];
firstLabel.alpha = 1;
[UIView commitAnimations];
[UIView beginAnimations:#"Second Label Display" context:nil];
[UIView setAnimationDelay:6.0];
secondLabel.alpha = 1;
[UILabel commitAnimations];
[UIView beginAnimations:#"Hide Labels" context:nil];
[UIView setAnimationDelay:10.0];
secondLabel.alpha = 0;
firstLabel.alpha=0;
[UILabel commitAnimations];
}
Use block based animation & chain your animations together. So there are 3 steps. label1 fadesIn, Label2 fadesIn, finally Label3 fadesIn. I have written the code below for fading in label1 & label2. Fading out is simple. I think you can fill in the rest. Its straight-forward from here...
Try this -
[UIView animateWithDuration:1.0
delay:4
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^(void)
{
[firstLabel setAlpha:1.0];
}
completion:^(BOOL finished)
{
if(finished)
{
[UIView animateWithDuration:1.0
delay:4.0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^(void)
{
[secondLabel setAlpha:1.0];
}
completion:^(BOOL finished)
{
if(finished)
{
//put another block her to hide both the labels.
}
}];
}
}];
I suggest rewriting this using blocks. First animateWithDuration:animations:completion: and then nested animateWithDuration:delay:options:animations:completion:. It's far more flexible, and there's no need to run on pre-blocks systems these days.
Also, your first animation as written wouldn't trigger for 4 seconds.
Here is a solution in swift 4+
UIView.animate(withDuration: 1.0, delay: 4, options: [.curveLinear, .allowUserInteraction], animations: {
firstLabel.alpha = 1.0
}) { finished in
if finished {
UIView.animate(withDuration: 1.0, delay: 4.0, options: [.curveLinear, .allowUserInteraction], animations: {
secondLabel.alpha = 1.0
}) { finished in
if finished {
//put another block her to hide both the labels.
}
}
}
}

Resources