UIImageView Zoom/Fade animation? - ios

I want to make a little zoom/fade animation for when the UIImageView shows up in my view, I already have the code for the fade effect:
banner.alpha = 0.0;
[UIView animateWithDuration:2.0 animations:^{
banner.alpha = 1.0;
}];
Now I want to know if there is another easy way to combine the fade effect with a little zoom effect, like the image becoming larger and filling up the frame as it fades in? Any ideas?
Thanks!

you can use CG_EXTERN CGAffineTransform CGAffineTransformMakeScale(CGFloat sx, CGFloat sy) to do the zoom effect
such as
banner.alpha = 0.0;
[UIView animateWithDuration:2.0 animations:^{
banner.alpha = 1.0;
banner.transform =CGAffineTransformMakeScale(1.3,1.3);
}];

You can also use the frame
banner.alpha = 0;
[UIView animateWithDuration:2.0f animations:^(void){
banner.alpha = 1.0f;
[banner setFrame:CGRectMake(0, 0, 1024, 748)];
}];
// With Completion
[UIView animateWithDuration:0.1f animations:^(void){
banner.alpha = 1.0f;
[banner setFrame:CGRectMake(0, 0, 1024, 748)];
}completion:^(BOOL finished) {
}];

Related

Moving UIView OFF the screen from left to right and bring it IN again from left

How can I Move an UIView OFF the screen from left to right and then bring it IN the screen again from left side ?!!
Thanks in advance,
All you have to do is animate the x origin position of the view and use the devices screen with to determine how much. So for off screen to the right:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
_yourView.frame = CGRectMake(screenRect.size.width, _yourView.frame.origin.y, _yourView.frame.size.width, _yourView.frame.size.height);
}
completion:^(BOOL finished) {
//position screen left after animation
_yourView.frame = CGRectMake(-screenRect.size.width, _yourView.frame.origin.y, _yourView.frame.size.width, _yourView.frame.size.height);
}];
And for on screen go back to 0.0 x-position or whatever your starting point was:
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
_yourView.frame = CGRectMake(0.0, _yourView.frame.origin.y, _yourView.frame.size.width, _yourView.frame.size.height);
}
completion:^(BOOL finished) {
//do something after animation
}];
To move OFF the screen:
CGRect frame = viewToMove.frame;
frame.origin.x = self.view.frame.size.width;
viewToMove.frame = frame;
To move the view back into the screen:
CGRect frame = viewToMove.frame;
frame.origin.x = 0;
viewToMove.frame = frame;
//Store your view's original rect
UIRect originRect = yourView.frame;
[UIView animateWithDuration:0.25 animations:^{
//create new rect and set its X coord to phone's width
CGrect rect = originRect;
rect.origin.x = self.view.frame.size.width;
yourView.frame = rect;
} completion:^(BOOL finished){
// when animation finished animating start new animation
//to bring your view to its original position
[UIView animateWithDuration:0.25 animation:^{
yourView.frame = originRect;
}];
}];
This code will move it to right in 0.3 seconds and then back from left to it's current position again in the next 0.3 seconds.
CGRect backToOriginal = Yourview.frame;
CGRect frameOnRight = CGRectMake ( screenWidth,backToOriginal.origin.y,backToOriginal.frame.size.width,backToOriginal.frame.size.height);
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ Yourview.frame = frameOnRight; } completion:^(BOOL finished)[UIView setFrame: screenLeft;] [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ Yourview.frame = backToOriginal; }{ }];
There must be errors in this code in terms of syntax since I am not sitting on a MAC right now.
//To move view to left
[UIView animateWithDuration:0.5
delay:1.0
options: UIViewAnimationCurveEaseOut
animations:^{
self.yourView.frame = CGRectMake(distanceToLeft, distanceFromTop, yourView.width, yourView.height);
}
completion:^(BOOL finished){
NSLog(#"Moved to left!");
}];
// To move back to original position
[UIView animateWithDuration:0.5
delay:1.0
options: UIViewAnimationCurveEaseOut
animations:^{
self.yourView.frame = CGRectMake(originalX, OriginalY, yourView.Width, yourView.height);
}
completion:^(BOOL finished){
NSLog(#"Back to normal!");
}];

animateWithDuration:delay:options:animations:completion: not working (strangely)

I'm trying to translate a UIView for a distance using the following code:
CGAffineTransform transform = CGAffineTransformMakeTranslation(-100, -100);
[self.exposureHintView setTransform:transform];
[UIView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
NSLog(#"Begin!");
CGAffineTransform newTrans = CGAffineTransformMakeTranslation(0, 0);
[self.exposureHintView setTransform:newTrans];
}
completion:^(BOOL finished) {
NSLog(#"End %d", finished);
}];
So basically, I'm justing trying to move the view from some point say (-100, -100) to where it should be (0, 0) relative to itself.
Since I wanted to animate it once the view appeared, so I put the code in viewDidAppear:. But when I run the code, nothing happened.
The self.exposureHintView here is a custom subclass of UIView, does it matter?
Why?
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[_AboutView setFrame:CGRectMake(0, 0, 320, 510)];
[UIView commitAnimations];
Use this code to change the position of your view
I think the problem here is you are create exposureHintView in storyboard or XIB file without code programmatically.
Try to do as the follows:
- (void)viewDidLoad
{
//Init exposureHintView
UIView* exposureHintView = [[UIView alloc]initWithFrame(sampleFrame)];
[self addSubView: exposureHintView];
//Set transform
CGAffineTransform transform = CGAffineTransformMakeTranslation(-100, -100);
[self.exposureHintView setTransform:transform];
//Commit animation
[UIView animateWithDuration:2.0 delay:0.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
NSLog(#"Begin!");
CGAffineTransform newTrans = CGAffineTransformMakeTranslation(0, 0);
[self.exposureHintView setTransform:newTrans];
}
completion:^(BOOL finished) {
NSLog(#"End %d", finished);
}];
The reason here is your could not set Frame or Transform or some another attribute of this UIView in >>ViewDidLoad when you using storyboard or XIB

When creating an animation to cause a UIImageView to go up and down (almost float) infinitely, how do I stop a pause at the end of the animation?

Here's the code I'm using to take a UIImageView and make it float up and down.
[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionRepeat animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.25 animations:^{
self.slider.transform = CGAffineTransformMakeTranslation(0, -5.0);
}];
[UIView addKeyframeWithRelativeStartTime:0.25 relativeDuration:0.5 animations:^{
self.slider.transform = CGAffineTransformMakeTranslation(0, 5.0);
}];
[UIView addKeyframeWithRelativeStartTime:0.75 relativeDuration:0.25 animations:^{
self.slider.transform = CGAffineTransformMakeTranslation(0, 0.0);
}];
} completion:^(BOOL finished) {
}];
However, it comes out looking like this with this delay after the animation ends and before it restarts.
How do I make it fluid?
I'm not sure what effect you're going for exactly, but I think this gives you something like your code does without the delay. I usually do this by animating a constraint, rather than using transforms. In this example, I've made an IBOutlet (topCon) to the constraint to the top of the view:
-(IBAction)floatView:(id)sender {
static int i= 1;
static float duration = .25;
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
self.topCon.constant = self.topCon.constant - (5 * i);
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
i = (i == 1 || i == 2)? -2 : 2;
duration = 0.5;
[self floatView:self];
}];
}

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;
}];
}];
}];
}];
}];
}];

Sequence of affine transformation with Quartz 2D

I'm attempting to simulate the incorrect password entry animation in OS X with an UIAlertView in iOS. Essentially I want it to translate the alert left then translate right. Here is what I am doing:
[UIView animateWithDuration:0.5
animations:^{
alertView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 200, 0);
alertView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, -200, 0);
}];
This doesn't have the desired effect however. The alert shoots of to the right instantaneously and then smoothly translates to the left. How can I get both of the translations to occur smoothly after each other?
Try this:
[UIView animateWithDuration:0.5 animations:^{
alertView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 200, 0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 animations:^{
alertView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, -200, 0);
}];
}];
But I would do the animation like this:
[UIView animateWithDuration:0.1 animations:^{
alertView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 20, 0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 animations:^{
alertView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, -20, 0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 animations:^{
alertView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, 0);
}];
}];
}];
Up to you! ;-)

Resources