fade out image Animation in iphone [duplicate] - iphone

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
iphone fading of images
How can implement fading out animation in to a set of images(array of images). Images will appear one by one. when i using the code given below it will show only a simple slide show.
NSArray *imagearray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"imag2.png"],[UIImage imageNamed:#"imag3.png"],[UIImage imageNamed:#"imag4.png"],[UIImage imageNamed:#"imag5.png"],[UIImage imageNamed:#"imag6.png"],[UIImage imageNamed:#"imag7.png"],nil];
imageview.animationImages=imagearray;
imageview.animationDuration = 10.00;
imageview.animationRepeatCount = 0;
[imageview startAnimating];
How can I implement the fading effect?

animate the alpha property of the view:
[UIView animateWithDuration:5 animations:^{
imageview.alpha = 0;
}];

Please visit this stack overflow question its iphone fading of images
- (void)viewDidLoad {
imgView.animationImages=[NSArray arrayWithObjects:
[UIImage imageNamed:#"lori.png"],
[UIImage imageNamed:#"miranda.png"],
[UIImage imageNamed:#"taylor.png"],
[UIImage imageNamed:#"ingrid.png"],
[UIImage imageNamed:#"kasey.png"],
[UIImage imageNamed:#"wreckers.png"], nil];
imgView.animationDuration=20.0;
imgView.animationRepeatCount=0;
[imgView startAnimating];
[self.view addSubview:imgView];
[imgView release];
//The timers time interval is the imageViews animation duration devided by the number of images in the animationImages array. 20/5 = 4
NSTimer *timer = [NSTimer timerWithTimeInterval:4.0
target:self
selector:#selector(onTimer)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[timer fire];
[super viewDidLoad];
}
//It is important that the animation durations within these animation blocks add up to 4
//(the time interval of the timer). If you change the time interval then the time intervals
//in these blocks must also be changed to refelect the amount of time an image is displayed.
//Failing to do this will mean your fading animation will go out of phase with the switching of images.
-(void)onTimer{
[UIView animateWithDuration:3.0 animations:^{
imgView.alpha = 0.0;
}];
[UIView animateWithDuration:1.0 animations:^{
imgView.alpha = 1.0;
}];
}

Related

UIImageView change Background Color and setImage not working during Animation

As the title said, it's very strange, did you agree?
So , if someone found your codes
imageView.backgroundColor = [UIColor greenColor];
or
imageView.image = [UIImage imageName:#"angryBird"];"
are not working. Just mind that if your imageView is animating , or whether the animation has been removed or not.
----------The Answer Below---------------
Just stop animating before you set image and change background of the animating UIImageView.
UIImageView* imageView = [UIImageView alloc] init];
//......do sth. like setFrame ...
imageView.images = [NSArray arrayWithObjects....]; // set the animation images array
imageView.animationDuration = 1.0;
[imageView startAnimating];
//......do sth.
[imageView stopAnimating]; // **** do not forget this!!!!! ****
imageView.backgroundColor = [UIColor greenColor];
imageView.image = [UIImage imageName:#"angryBird"];
When you performSelector: withObject: afterDey:1.0s , in the selector method , also, you need to stopAnimating too, and then setImage or change background color.
Try to change background color and changing the image once animation is complete. it will simply work for you.
Try this:
[UIView animateWithDuration:1.0 animations:^(){} completion:^(BOOL finished){}];
Try this,Animation using block
[UIView animateWithDuration:1.0 animations:^{
}
completion:^(BOOL finished)
{
[imageView setImage:[UIImage imageNamed:#"YOUR_IMAGE_NAME"]];
[imageView setBackgroundColor:[UIColor greenColor]];
}];
Note: It is imageNamed: not imageName: ,I am not sure how this get compiled
Try this code and performSelector:withObject:afterDelay:: in this way
[self performSelector:#selector(animationDidFinish:) withObject:nil
afterDelay:instructions.animationDuration];
or use dispatch_after:
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, instructions.animationDuration * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self animationDidFinish];
});
i hope it helps you

Move image from left to right

In my application after the view load, an image should appear from left to right.
I am writing this code in in viewdidload or viewwillappear:
UIImage *image = [UIImage imageNamed:#"PG05(REV).jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
for (int i=-1024; i<=0; i++) {
NSLog(#"i: %d",i);
sleep(5);
imageView.frame = CGRectMake(i,0, 1024, 768);
NSLog(#"done");
[self.view addSubview:imageView];
}
[imageView release];
But problem is that, before loading view it executes the above code than loads the view, so that it seems that view loaded and a static image is there.
But my requirement is that first view load completely then image should display from left to right.
Please help me out.
First of all, you should not call the -(void)addSubview: inside your for loop. You just need to call it once.
Secondly, if you want to animate your imageView from left to right, UIView class provides great functionalities for this using blocks.
Your code should look like this :
UIImage *image = [UIImage imageNamed:#"PG05(REV).jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(-1024, 0, 1024, 768);
[self.view addSubview:imageView];
[imageView release]; //Your imageView is now retained by self.view
//Animation
[UIView animateWithDuration:2.0
animations:^(void) {
imageView.frame = CGRectMake(0, 0, 1024, 768);
}];
The animateWithDuration: method will handle the animation timer for you, just set the duration argument according to your needs.
You can use a function and call it using timer method.
timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(moveImage) userInfo:nil repeats:NO];
-(void)moveImage
{
//Your condition
}
This is a job for UIView animations.
Put your imageView down at -1024 left, then go:
[imageView animateWithDuration:5 animations:^{
[imageView.frame = CGRectMake(0,0, 1024, 768)];
}];
Ta-da! A five second animation of your image sliding to the right.
Check out the animation methods of UIView (of which UIImageView is a subclass):
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html

How to add animated splash screen in our application

How to add animated splash screen in our application.
You can use sequence of images, here is code:
for(NSInteger i=1;i<=totalImages;i++){
NSString *strImage = [NSString stringWithFormat:#"Activity_%d",i];
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:strImage ofType:#"png"]];
[imageArray addObject:image];
}
splashImageView.animationImages = imageArray;
splashImageView.animationDuration = 0.8;
and just call startAnimation and endAnimation method of UIImageView.
Its very simple...I had used it in to begin my app with splashView.Hope it vil help you....
In AppDelegate.m:
application didFinishLaunchingWithOptions:
UIImage* image=[UIImage imageNamed:#"splash.jpg"];
splashView=[[UIImageView alloc]initWithImage:image];
[window addSubview:splashView];
[window bringSubviewToFront:splashView];
[self performSelector:#selector(removeSplash) withObject:self afterDelay:2];
[window makeKeyAndVisible];
To remove splashView:
-(void)removeSplash{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:window cache:YES];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
[splashView removeFromSuperview];
[UIView commitAnimations];
[window addSubview:viewController.view];
}
Old Answer:
Well this is not possible yet. You can not make any Animation on Splash Screen. But you can make it via UIViewController class which will look like a Splash Screen. Remove the default.png image from your project by which user cant see the Default Splash Screen. Then in your first ViewController class you can make an animation using an array of images as was told above already. And in viewDidLoad: method make a NSTimer then hold the View according to you. After finish the time limit of NSTimer navigate your next ViewController view.
Edit:
I found an alternative solution to make it animated. We can show a .gif image in webView and it looks perfect!
NSString *imagePath = [[NSBundle mainBundle] pathForResource: #"animated" ofType: #"gif"];
NSData *data = [NSData dataWithContentsOfFile:imagePath];
[self.webView setUserInteractionEnabled:NO];
[self.webView loadData:data MIMEType:#"image/gif" textEncodingName:nil baseURL:nil];
Make this view as a rootView of your app and after few delay navigate your next View.
Remember make its userIntractionEnabled: false i.e. user cant scroll it.
Full description see here Animated Splash Screen in iPhone
I do this by creating an array of images because gif is not supported format
Just add image frames of your movieclip for eg: {Splashbackground1,Splashbackground2,Splashbackground3 are sequence of images}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
// create the view that will execute our animation for elephant
CGRect splashscreenmovieclipframe = CGRectMake(0.0f,0.0f,480.0f, 320.0f); //set co-ordinate here i use full screen
splashscreenmovieclip = [[UIImageView alloc] initWithFrame:splashscreenmovieclipframe];
// load all the frames of our animation
splashscreenmovieclip.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"Splashbackground.png"],
[UIImage imageNamed:#"Splashbackground1.png"],
[UIImage imageNamed:#"Splashbackground2.png"],
[UIImage imageNamed:#"Splashbackground3.png"],
nil];
// all frames will execute in 1.75 seconds
splashscreenmovieclip.animationDuration =7;
// repeat the annimation forever
splashscreenmovieclip.animationRepeatCount = 0;
// start animating
[splashscreenmovieclip startAnimating];
// add the animation view to the main window
[self.view addSubview:splashscreenmovieclip];
[NSTimer scheduledTimerWithTimeInterval:7.0f target:self selector:#selector(Gotomainmenuview:) userInfo:nil repeats:NO];
[super viewDidLoad];
}
- (void)Gotomainmenuview:(NSTimer *)theTimer
{
// write your code here for counter update
[splashscreenmovieclip removeFromSuperview];
newclasstojump *mmvc=[[newclasstojump alloc]initWithNibName:#"newclasstojump" bundle:nil];
[self.view addSubview:mmvc.view];
}

iPhone animation delay problem

I'm trying to get this animation to delay for 60 seconds and take 125 seconds to complete it's animation cycle. then repeat infinitely. the problem is that the delay only lasts 20 seconds. Is there a limit on the delay you can specify? or, perhaps a better way to do what I'm attempting?
here's my code:
- (void)firstAnimation {
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"f1.png"],
[UIImage imageNamed:#"f2.png"],
[UIImage imageNamed:#"f3.png"],
[UIImage imageNamed:#"f4.png"],
nil];
UIImageView *myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:CGRectMake(0, 0, 320, 400)];
myAnimatedView.animationImages = myImages;
[UIView setAnimationDelay:60.0];
myAnimatedView.animationDuration = 125.0;
myAnimatedView.animationRepeatCount = 0; // 0 = loops forever
[myAnimatedView startAnimating];
[self.view addSubview:myAnimatedView];
[self.view sendSubviewToBack:myAnimatedView];
[myAnimatedView release];
}
thanks for any help.
You're using the setAnimationDelay method in the wrong way.
setAnimationDelay is intended to be used when animating changes to animatable properties on views within a UIViewAnimations block like so:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:60];
//change an animatable property, such as a frame or alpha property
[UIView commitAnimations];
That code will delay the animation of a property change by 60 seconds.
If you want to delay a UIImageView from animating its images you'll need to use an NSTimer.
[NSTimer scheduledTimerWithTimeInterval:60
target:self selector:#selector(startAnimations:)
userInfo:nil
repeats:NO];
Then define the startAnimations: selector, like so:
- (void)startAnimations:(NSTimer *)timer
{
[myAnimatedView startAnimating];
}
That way, after 60 seconds, the timer will fire the method startAnimations: which will start your image view animating.

Image Intro at iphone application starting

Hi guys I want to create an intro image (fade in and fade out ) for my application.
Here is my code but, when I run the app on the device I have a problem:
My main nib page has been displayed
My image fades in and then fades out and then again fades in and fades out 3 times
Here is my code and my attached file. Please check it out:
.h
#interface myProject : UIViewController {
float alphaValue, imageAlphaValue;
}
UIImageView *IntroImage;
NSTimer *AlphaTimer;
#end
.m
-(void)viewDidLoad
{
imageAlphaValue = 0.0;
alphaValue = 0.035;
IntroImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:IntroImage];
[IntroImage setImage:[UIImage imageNamed:#"yourimagenamehere.png"]]; //use your image here
[IntroImage setBackgroundColor:[UIColor blueColor]];
[IntroImage setAlpha:imageAlphaValue];
[IntroImage release];
AlphaTimer = [NSTimer scheduledTimerWithTimeInterval:(0.05) target:self selector:#selector(setAlpha) userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:(2.0) target:self selector:#selector(changeAlpha) userInfo:nil repeats:NO];
}
-(void)setAlpha
{
imageAlphaValue = imageAlphaValue + alphaValue;
[IntroImage setAlpha:imageAlphaValue];
}
-(void)changeAlpha
{
alphaValue = -0.035;
}
my sample code
wow.... why you cannot just use something like that to change your image's alpha?
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
[UIView setAnimationDelegate:self];
[introImage setAlpha:0.0f];
[UIView commitAnimations];
Seems to be not surprising that the main nib is shown, because it is the viewDidLoad method!
I'd also prefer to make use of the animation stuff like in morion's answer. Even though: The repeated fading is probably caused by viewDidLoad called more than once. Check it out with debugging or logging.