iPhone: UIImageView Fades in - Howto? - iphone

I have an UIImageView that I want to fade in.
Is there a way to enable that on an underlying UIViewController?
I have translated the simplest answer, though they all work, C# .NET for the MonoTouch users:
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
UIView.BeginAnimations ("fade in");
UIView.SetAnimationDuration (1);
imageView.Alpha = 1;
UIView.CommitAnimations ();
}

Initially set the alpha of your imageview as 0 as imageView.alpha = 0;
- (void)fadeInImage
{
[UIView beginAnimations:#"fade in" context:nil];
[UIView setAnimationDuration:1.0];
imageView.alpha = 1.0;
[UIView commitAnimations];
}

Change the animation duration to what ever length you want.
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myImage.png"]];
myImageView.center = CGPointMake(100, 100);
myImageView.alpha = 0.0;
[self.view addSubview:myImageView];
[UIView animateWithDuration:5.0 animations:^{
myImageView.alpha = 1.0;
}];

this simple code:
[UIView animateWithDuration:5.0 animations:^{
theImage.alpha = 1.0;
}];
the UIImage is in the view and is connected via an IBOutlet
you can also, set the alpha from the utility panel.

Use below in your UIViewController
// add the image view
[self.view addSubview:myImageView];
// set up a transition animation
CATransition *animate = [CATransition animation];
[animate setDuration:self.animationDelay];
[animate setType:kCATransitionPush];
[animate setSubtype:kCATransitionFade];
[animate setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[self layer] addAnimation:animate forKey:#"fade in"];

I would use UIView's +transitionWithView:duration:options:animations:completion:
It is very efficient and powerful.
[UIView transitionWithView:imgView duration:1 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
imgView.image = [UIImage imageNamed:#"MyImage"];
} completion:nil];

You can use this code:
Fade In Animation:
self.yourComponent.alpha = 0.0f;
[UIView beginAnimations:#"fadeIn" context:nil];
[UIView setAnimationDuration:1.0]; // Time in seconds
self.yourComponent.alpha = 1.0f;
Fade Out Animation:
self.yourComponent.alpha = 1.0f;
[UIView beginAnimations:#"fadeOut" context:nil];
[UIView setAnimationDuration:1.0]; // Time in seconds
self.yourComponent.alpha = 0.0f;
self.yourComponent can be a UIView, UIImageView, UIButton or any other component.

Swift version
func fadeIn(){
UIView.beginAnimations("fade in", context: nil);
UIView.setAnimationDuration(1.0);
imageView.alpha = 1.0;
UIView.commitAnimations();
}

Related

Fade-In Fade-Out Animation for UIImageViews?

I have two UIImageView - leftSide and rightSide, which I would like to fade in and then fade out, one after the other, in a loop.
How do I do this in Xcode? I tried CABasicAnimation but this did not work (I'm not sure why, no errors at all, the images do not fade).
Thanks!
EDIT: This is the code I was using:
-(void)leftSideFade{
CABasicAnimation *blink;
blink = [CABasicAnimation animationWithKeyPath:#"opacity"];
blink.duration = 1.0;
blink.repeatCount = 5;
blink.autoreverses = YES;
blink.fromValue = [NSNumber numberWithFloat:1.0];
blink.toValue = [NSNumber numberWithFloat:0.0];
[leftSideStart.layer addAnimation:blink forKey:#"animateOpacity"];
[self rightSideFade];
}
-(void)rightSideFade{
CABasicAnimation *blink;
blink = [CABasicAnimation animationWithKeyPath:#"opacity"];
blink.duration = 1.0;
blink.repeatCount = 5;
blink.autoreverses = YES;
blink.fromValue = [NSNumber numberWithFloat:1.0];
blink.toValue = [NSNumber numberWithFloat:0.0];
[rightSideStart.layer addAnimation:blink forKey:#"animateOpacity"];
[self leftSideFade];
}
- (void)fadeIn
{
[UIView beginAnimations:#"fadeIn Animation" context:nil];
[UIView setAnimationDuration:1.0];
imageView.alpha = 1.0;
[UIView commitAnimations];
}
Try this to.
Try using the following :
- (void) fadeinRightFadwOutLeft {
[UIView beginAnimations:#"start" context:nil];
[UIView setAnimationDuration:1.0];
rightSide.alpha = 0.0;
leftSide.alpha = 1.0;
[UIView commitAnimations];
}
- (void) fadeinLeftFadwOutRight {
[UIView beginAnimations:#"start" context:nil];
[UIView setAnimationDuration:1.0];
leftSide.alpha = 0.0;
rightSide.alpha = 1.0;
[UIView commitAnimations];
}
How about a simpler way with UIView animateWithDuration:animations:completion: ?
There you just change alpha of your views and they will fade in/out
Here is a sample code:
[UIView animateWithDuration:duration
animations:^
{
//Here you set values you want to see in the end of animation
leftView.alpha = 0.0f;
rightView.alpha = 1.0f;
}
completion:^(BOOL finished)
{
//something to do after animation finished
}

Transitions between UIView in the same UIViewController

I want to transit from UIView to another in the UIViewController when I press a button
I write this code:
- (IBAction)changeView:(id)sender {
CATransition* transition = [CATransition animation];
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
transition.duration = 1.0f;
transition.type = #"cube";
transition.subtype = #"fromTop";
[self.view1.layer removeAllAnimations];
[self.view1.layer addAnimation:transition forKey:kCATransition];
}
this code will transit the view1(UIview) to itself. how can I make it to transit to another UIView (e.g. view2). also where can I put the another UIview (view2) in the storyboard
Have you try like this?
self.view1.alpha = 0.0;
[UIView beginAnimations:#"flip" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[UIView setAnimationDuration:1.0f];
self.view2.alpha = 1.0;
[UIView commitAnimations];
Have a look at this code snippet it's much more elegant. Also checkout Apple's documentation for more fancy UIViewAnimationOptions
- (IBAction)changeView:(id)sender
{
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
//Remove View-1 from the main view
[view1 removeFromSuperView];
//Add the new view to your current view
[self.view addSubView:view2];
} completion:^(BOOL finished) {
//Once animation is complete
}];
}
Ensure that the two views are a strong property of your view controller.
If you have two UIView for example take firstView and secondView and add it to UIViewController on same rect. and set hiddden property to secondView.
-(IBAction)flipFirstViewToSecondView{
secondView.hidden= NO;
[UIView transitionWithView:firstView duration:1.0f options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
[UIView transitionWithView:secondView duration:1.0f options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
} completion:^(BOOL finished) {
secondView.hidden = NO;
firstView.hidden = YES;
}];
} completion:^(BOOL finished) {
}];
}
Thanks. just let me know if you have any problem to integrate it.

Trying to standardise UIView animations in my app, and it's not working

I have the same animation code applied to various views in my object, so i thought I would standardise it with it's own class. Here is the code i have:
.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface AHAnimations : NSObject
+(void)popInView:(UIView *)view;
#end
.m
#import "AHAnimations.h"
#implementation AHAnimations
+(void)popInView:(UIView *)view {
view.alpha = 0;
view.transform = CGAffineTransformMakeScale(0.9, 0.9);
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
view.alpha = 1;
view.transform = CGAffineTransformMakeScale(1, 1);
}completion:nil];
}
#end
But instead of animating, the view just appears.
Any ideas why this isn't working?
EDIT: The original code is exactly the same. I can paste it, but other than being a different method name, i'd just be copying and pasting it from above. And now, it is simply [AHAnimations popInView:view];
UIView *myview=[[UIView alloc] init];
[myview setFrame:CGRectMake( 50.0f, 50.0f, 280.0f, 400.0f)];
[UIView commitAnimations];
[UIView beginAnimations:#"animateTableView" context:nil];
[UIView setAnimationDuration:0.7];
[myview setFrame:CGRectMake( 5.0f, 70.0f, 310.0f, 350.0f)];
// CALL This line to stop animation
[myview.layer removeAllAnimations];
[UIView commitAnimations];
+[UIView animateWithDuration ... not animating all properties, tho alpha should work...
These code won't work, you can use CAAnimationGroup or CABasicAnimation's property: fromValue, byValue, toValue.
Also, you could implement like this which is easier to understand,
//go
- (void)stepOne{
[UIView beginAnimations:#"" context:nil];
[UIView setAnimationDuration:0.2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(stepTwo)];
view.alpha = 0;
view.transform = CGAffineTransformMakeScale(0.9, 0.9);
[UIView commitAnimations];
}
//back
- (void)stepTwo{
[UIView beginAnimations:#"" context:nil];
[UIView setAnimationDuration:0.1];
view.alpha = 1;
view.transform = CGAffineTransformMakeScale(1, 1);
[UIView commitAnimations];
}
There are also a lot of original transition animation, you can check them out.

Core Animation for UIView.frame

I'm trying to do a simple animation of moving the frame of two views. Basically hiding the Ad until it is loaded, and then move the frame up from the bottom, along with the view that starts at the bottom, and then will move up also when the Ad pushes it up. The start and end positions are correct, but I don't see it being animated. Is this correct? Thanks.
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:#"frame"];
animation.duration = 1.0;
CGRect adFrame = CGRectMake(self.adBanner.frame.origin.x, self.adBanner.frame.origin.y - self.adBanner.frame.size.height, self.adBanner.frame.size.width, self.adBanner.frame.size.height);
self.adBanner.frame = adFrame;
[self.adBanner.layer addAnimation:animation forKey:#"frame"];
CGRect buttonViewFrame = CGRectMake(self.ButtonView.frame.origin.x, self.adBanner.frame.origin.y - self.adBanner.frame.size.height, self.ButtonView.frame.size.width, self.ButtonView.frame.size.height);
self.ButtonView.frame = buttonViewFrame;
[self.ButtonView.layer addAnimation:animation forKey:#"frame"];
For something as simple as this, you don’t really need to use Core Animation directly—UIView’s built-in animation system should suffice.
[UIView animateWithDuration:1.0 animations:^{
self.adBanner.frame = adFrame;
self.ButtonView.frame = buttonViewFrame;
}];
or, if you’re targeting pre-4.0 iOS,
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
self.adBanner.frame = adFrame;
self.ButtonView.frame = buttonViewFrame;
[UIView commitAnimations];
[UIView beginAnimations : #"Display notif" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationBeginsFromCurrentState:FALSE];
CGRect frame = mainView.frame;
frame.size.height -= 40;
frame.origin.y += 40;
mainView.frame = frame;
[UIView commitAnimations];

addSubview animation

I have main UIView where I display different data. Then I put a button, which displays subview like this:
- (IBAction) buttonClicked:(id)sender
{
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(25,25,50,20)];
UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(25,25,50,25)];
newLabel.text = #"some text";
[newView addSubview:newLabel];
[self.view addSubview:newView];
[newLabel release];
[newView release];
}
newView appears fine, but it doesn't animate itself any way, it just appears immediately. How can I add some kind of animation when newView appears? Like zoom or slide down or something like that. Is there some simple code?
[UIView transitionWithView:containerView duration:0.5
options:UIViewAnimationOptionTransitionCurlUp //change to whatever animation you like
animations:^ { [containerView addSubview:subview]; }
completion:nil];
Hi You could use the UIView Animations:
[newView setFrame:CGRectMake( 0.0f, 480.0f, 320.0f, 480.0f)]; //notice this is OFF screen!
[UIView beginAnimations:#"animateTableView" context:nil];
[UIView setAnimationDuration:0.4];
[newView setFrame:CGRectMake( 0.0f, 0.0f, 320.0f, 480.0f)]; //notice this is ON screen!
[UIView commitAnimations];
The SDK will now figure this out for you and animate the view to the positions you gave it.
This works for most properties of UIViews: alpha, scale, bounds, frames, etc.
There are also build in animations as flip and curl:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:newView
cache:YES];
[self.navigationController.view addSubview:settingsView.view];
[UIView commitAnimations];
Hope this helps out getting you started:)
Let's do this in swift.
var redView = UIView(frame:CGRectMake(20,20,100,100))
redView.backgroundColor = UIColor.redColor
redView.alpha = 0.0
view.addSubview(redView)
UIView.animateWithDuration(0.25) { () -> Void in
redView.alpha = 1.0
}
Adding a subview cannot be animated by simply putting it in an animateWithDuration block. And it can't be animated using hidden.
link against QuarzCore framework
#import <QuartzCore/QuartzCore.h>
CATransition *transition = [CATransition animation];
transition.duration = 1.0;
transition.type = kCATransitionPush; //choose your animation
[newView.layer addAnimation:transition forKey:nil];
[self.view addSubview:newView];
I found that even with the animation blocks and options, trying to animate addSubview alone would not do anything for me. I found a workaround which is to set the subview's alpha to 0.0, add the subview, and then animate the setting of the alpha from 0.0 to 1.0. This gives me the fade in effect I was looking for.
Hope this helps someone else.
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
redView.backgroundColor = [UIColor redColor];
redView.alpha = 0.0;
[self.view addSubview:redView];
[UIView animateWithDuration:0.5 animations:^{
redView.alpha = 1.0;
}];
Swift 5
UIView.transition(with: renderView, duration: 0.25, options: .transitionCrossDissolve, animations: {
self.renderView.addSubview(emojiView)
self.renderView.bringSubviewToFront(emojiView)
}, completion: nil)
in code example renderView is view to what you will add your subView