xcode iphone ball animation - iphone

What I want to do is that every ten second a ball is created outside of the screen, and when it is created it move to the center of the screen. How can I do this?

In your view controller set up a UIImageView property for your ball, contruct the ball and make sure its starting location is off the viewable area of the creen and then use an NSTimer which fires every ten seconds which callas a method that animates your ball across the screen to the centre.
-(void)viewDidLoad{
NSTimer *ballTimer = [NSTimer timerWithTimeInterval:10.0
target:self
selector:#selector(moveBall)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:ballTimer forMode:NSRunLoopCommonModes];
myBall = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"MyBallImageFile.png"]];
myBall.center = CGPointMake(320/2, [self randNumBetween:-50:-100]);
[self.view addSubview:myBall];
}
-(void)moveBall{
myBall.center = CGPointMake(320/2, [self randNumBetween:-50:-100]);
[UIView animateWithDuration:5.0 animations:^{
myBall.center = CGPointMake(320/2, 480/2);
}];
}
----------EDIT------------
- (CGFloat)randNumBetween:(CGFloat) min :(CGFloat) max{
CGFloat difference = max - min;
return (((CGFloat) rand()/(CGFloat)RAND_MAX) * difference) + min;
}

- (void)viewDidLoad {
[super viewDidLoad];
myBallImage = [[UIImage imageNamed:#"ball.png"] retain];
myTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(addBallToScreen) userInfo:nil repeats:YES];
}
- (void)viewDidUnload {
[myBallImage release];
[myTimer invalidate];
}
- (void)addBallToScreen {
//Create the imageView
UIImageView *imageView = [[UIImageView alloc] initWithImage:myBallImage];
imageView.transform = CGAffineTransformMakeTranslation(1000, 1000);
[self.view addSubview:imageView];
[imageView release];
//Animate the image view
[UIView beginAnimations:nil context:nil];
imageView.transform = CGAffineTransformMakeTranslation(50, 50);
[UIView commitAnimations];
}

Related

Objective-C Auto scroll images in a UI scroll view repeatedly, like a horizontal slide show

I have a list of images getting from database in to an iMagesArray.
My requirement is to display images as a slide show in a fixed size window horizontally.
Same like in http://www.nda4u.com/ *Thank you to these 2013 NDA Convention Sponsors:* line
Try
//...
int i;
}
-(void) viewDidLoad{
//... whatever wour code in the view is...
[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:#selector(animation:) userInfo:Nil repeats:Yes];
}
-(void)animation{
i++;
//i'd name the images just with numbers
int newView = mod(i,numberOfImagesInSlideShow);
NSString *name = [NSString stringWithFormat:#"%d",mod(i,numberOfTotalImages);]
UIImageView newImageView = [[UIImageView alloc] initWithImage[UIImage imageNamed:name]];
self.imageView.center = CGPointMake(-self.bounds.size.width/numberOfImagesInSlideShow,self.bounds.size.height/2);
[self addSubView:newImageView];
[UIView animateWithDuration:2.0f animations:^{
for(int n=0;n<numberOfImagesInSlideShow;n++){
if([self viewWithTag:(mod(i+n,numberOfImagesInSlideShow))])
[self viewWithTag:(mod(i+n,numberOfImagesInSlideShow))].center = CGPointMake(n*self.bounds.size.width/numberOfImagesInSlideShow,self.bounds.size.height/2);
}
} completion:^(BOOL finished){
if([self viewWithTag:(mod(mod(i+numberOfImagesInSlideShow-1,numberOfImagesInSlideShow)))])
[[self viewWithTag:(mod(i+numberOfImagesInSlideShow-1,numberOfImagesInSlideShow))] removeFromSuperView];
}];

Displaying next view after splashscreen

I want to load a view of my choice after the default splash view of iphone launches. Does someone know how to achieve this ? i am new to xcode and ios development.
Add [self setUpSplash]; method in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method. And add following Three method in AppDelegate.m
-(void) setUpSplash {
self.splashImgView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[self.splashImgView setImage: [UIImage imageNamed:#"splash.png"]];
[self.window addSubview: self.splashImgView];
[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:#selector(closeSplashWithTimer:) userInfo:nil repeats:NO];
}
- (void)closeSplashWithTimer:(NSTimer *)theTimer
{
[UIView beginAnimations:#"ToggleViews" context:nil];
[UIView setAnimationDuration:1.0];
// Make the animatable changes.
splashImgView.alpha = 0.0;
self.mvNavigationController.view.alpha = 1.0;
// Commit the changes and perform the animation.
[UIView commitAnimations];
[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:#selector(closeSplashWithTimerAgain:) userInfo:nil repeats:NO];
}
- (void)closeSplashWithTimerAgain:(NSTimer *)theTimer
{
[self.splashImgView removeFromSuperview];
}

How to resume updating views when resumed AVAudioplayer

I m struggling with this issue for so long but not able to find any solution for this
The issue i have is when i pause the audioplayer i want views to be paused from updating so i used timer invalidate to pause the views from loading but when i resume play only Audioplayer is resumed not the views cause i invalidated the timer.
Now i how i can resume updating views from that point where view was paused. By starting the timer again i can start updating views again but how program will know that at what view it was paused and it should resume updating views from that point. There are multiple views involved displayed code for only two.
-(void)playpauseAction:(id)sender
{
if
([audioPlayer isPlaying]){
[sender setImage:[UIImage imageNamed:#"play.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[timer invalidate];
} else {
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
self.timer = [NSTimer scheduledTimerWithTimeInterval:11 target:self selector:#selector(displayviewsAction:) userInfo:nil repeats:NO];
}
}
- (void)displayviewsAction:(id)sender
FirstViewController *viewController = [[FirstViewController alloc] init];
viewController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
[viewController release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:23 target:self selector:#selector(secondViewController) userInfo:nil repeats:NO];
-(void)secondViewController {
SecondViewController *secondController = [[SecondViewController alloc] init];
secondController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:secondController.view];
[self.view addSubview:toolbar];
[secondController release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:27 target:self selector:#selector(ThirdviewController) userInfo:nil repeats:NO];
}
Thanks and appreciate for your answers and helping me out with this issue.
Use a background thread to basically bind the view to the player, something like this:
UIImageView* imageView; //the view that gets updated
int current_selected_image_index = 0;
float time_for_next_image = 0.0;
AVAudioPlayer* audioPlayer;
NSArray* image_times; //an array of float values representing each
//time when the view should update
NSArray* image_names; //an array of the image file names
-(void)update_view
{
UIImage* next_image_to_play = [image_names objectAtIndex:current_selected_image_index];
imageView.image = next_image_to_play;
}
-(void)bind_view_to_audioplayer
{
while(audioPlayer.isPlaying)
{
float currentPlayingTime = (float)audioPlayer.currentTime;
if(currentPlayingTime >= time_for_next_image)
{
current_selected_image_index++;
[self performSelectorOnMainThread:#selector(update_view) withObject:nil waitUntilDone:NO];
time_for_next_image = [image_times objectAtIndex:[current_selected_image_index+1)];
}
[NSThread sleep:0.2];
}
}
-(void)init_audio
{
current_selected_image_index = 0;
time_for_next_image = [image_times objectAtIndex:1];
}
-(void)play_audio
{
[audioPlayer play];
[self performSelectorInBackground:#selector(bind_view_to_audioplayer) withObject:nil];
}
-(void)pause_audio
{
[audioPlayer pause];
//that's all, the background thread exits because audioPlayer.isPlaying == NO
//the value of current_selected_image_index stays where it is, so [self play_audio] picks up
//where it left off.
}
also, add self as observer for the audioPlayerDidFinishPlaying:successfully: notification to reset when the audioplayer finishes playing.

problem with multiple animation at the same time

here is my code :
-(void) createNewImage {
UIImage * image = [UIImage imageNamed:#"abouffer_03.png"];
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setCenter:[self randomPointSquare]];
[imageViewArray addObject:imageView];
[[self view] addSubview:imageView];
[imageView release];
}
-(void)moveTheImage{
for(int i=0; i< [imageViewArray count];i++){
UIImageView *imageView = [imageViewArray objectAtIndex:i];
imageView.center = CGPointMake(imageView.center.x + X, imageView.center.y + Y);
}
}
-(void)viewDidLoad {
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
displayLink = [CADisplayLink displayLinkWithTarget:self selector:#selector(onTimer2)];
[displayLink setFrameInterval:1];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
imageViewArray = [[NSMutableArray alloc]init];
}
So what I want to do is http://www.youtube.com/watch?v=rD3MTTPaK98.
But my problem is that after imageView is created(createNewImage) it stops after 4 seconds (maybe due to the timer).I want that imageView continue moving while new imageView are created. How can I do this please ? sorry for my english I'm french :/
Instead, keep a reference to the point you want all your images to move too. Using an NSTimer and moving all the images yourself in the timer will eventually slow down your app a lot (I know from experience). Use UIView animation blocks, and just tell it to move to the point when you create it.
-(void) createNewImage {
UIImage * image = [UIImage imageNamed:#"abouffer_03.png"];
imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
[imageView setCenter:[self randomPointSquare]];
//Move to the centerPoint
[self moveTheImage:imageView];
[imageViewArray addObject:imageView];
[[self view] addSubview:imageView];
}
-(void)moveTheImage:(UIImageView *)imageView {
[UIView animateWithDuration:1.0
animations:^{
[imageView setCenter:centerPoint];
}];
}
-(void)viewDidLoad {
[super viewDidLoad];
imageViewArray = [[NSMutableArray alloc]init];
//IDK what all that other code was
centerPoint = self.view.center;
}
EDIT: Finding the UIImage during animation
You need to reference the presentation layer of the UIImageView to find its position during an animation
UIImageView *image = [imageViewArray objectAtIndex:0];
CGRect currentFrame = [[[image layer] presentationLayer] frame];
for(UIImageView *otherImage in imageViewArray) {
CGRect objectFrame = [[[otherImage layer] presentationLayer] frame];
if(CGRectIntersectsRect(currentFrame, objectFrame)) {
NSLog(#"OMG, image: %# intersects object: %#", image, otherImage);
}
}

Splash screen related in iphone

Hi I am trying to splash screen with the help of timer. but it can't. IS any suggestion regarding this code.........
SplashViewController.h:-
#import <UIKit/UIKit.h>
#import "MainMenu.h"
#interface SplashViewController : UIViewController {
UIImage *imgSplash;
NSTimer *timer;
MainMenu *objMainMenuView;
}
#property (nonatomic,retain) NSTimer *timer;
#property (nonatomic,retain) UIImage *imgSplash;
#property (nonatomic,retain) MainMenu *objMainMenuView;
#end
SplashViewController.m:-
#import "SplashViewController.h"
#implementation SplashViewController
#synthesize timer,imgSplash,objMainMenuView;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
timer = [[NSTimer alloc] init];
UIImageView *splashImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,460)];
imgSplash = [[UIImage alloc] init];
imgSplash = [UIImage imageNamed:#"chicocredit_splash.png"];
[splashImageView setImage:imgSplash];
[self.view addSubview:splashImageView];
timer = [NSTimer timerWithTimeInterval:2.0 target:self.timer selector:#selector(fadeScreen) userInfo:nil repeats:NO];
if([timer isValid]==1)
{
[timer fire];
//self.view.alpha = 0.0;
objMainMenuView = [[MainMenu alloc] initWithNibName:#"MainMenu" bundle:nil];
[self.navigationController pushViewController:objMainMenuView animated:YES];
}
}
-(void) onTimer{
NSLog(#"LOAD");
}
- (void)fadeScreen
{
[UIImageView beginAnimations:nil context:nil];
[UIImageView setAnimationDuration:2.0];
[UIImageView setAnimationDelegate:self];
[UIImageView commitAnimations];
//[UIView setAnimationDidStopSelector:#selector(finishedFading)];
//self.view.alpha = 0.0;
//[UIView commitAnimations];
}
/*
- (void) finishedFading
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5.0];
//self.view.alpha = 1.0;
//viewController.view.alpha = 1.0;
//self.objMainMenuView.view.alpha = 1.0;
[UIView commitAnimations];
//[splashImageView removeFromSuperview];
}*/
- (void)dealloc {
[super dealloc];
}
#end
Use this It may be solution for that.
- (void)viewDidLoad {
timer = [[NSTimer alloc] init];
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 460.0f);
splashImageView = [[UIImageView alloc] initWithFrame:myImageRect];
[splashImageView setImage:[UIImage imageNamed:#"image3.jpg"]];
splashImageView.opaque = YES;
[self.view addSubview:splashImageView];
[splashImageView release];
[self performSelector:#selector(doTHis) withObject:nil afterDelay:2.0];
[super viewDidLoad];
}
-(void)doTHis{
objMainMenuView = [[MainMenu alloc] initWithNibName:#"MainMenu" bundle:nil];
[self.navigationController pushViewController:objMainMenuView animated:YES];
}
Good Luck
Looks like your MianMenu is getting pushed before you can see the fade effect. Its because you are firing the timer and pushing the Main Menu immediately.
// Schedule the timer here.
[NSTimer timerWithTimeInterval:2.0f target:self selector:#selector(timerFireMethod:) userInfo:nil repeats:NO];
// Timer fire method.
-(void) timerFireMethod:(NSTimer *) theTimer {
[UIView beginAnimations:nil context: nil];
[UIView setAnimationDuration:2.0f];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector: #selector(animationDidStop: finished: context:)];
// Put animation code.
[UIView commitAnimations];
}
// Called when animation finishes.
-(void) animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
// Called when animation finishes.
// Push the Main menu here.
}
Keep break point in the timer fire method. It should get called after 2 seconds as specified.
Then keep break point in the animationDidStopSelector method. It will get called after your fade animation of 2 seconds.
Your code is leaked:
1:
timer = [[NSTimer alloc] init]; //Leak here, remove this line
.
.
.
timer = [NSTimer timerWithTimeInterval:2.0 target:self.timer selector:#selector(fadeScreen) userInfo:nil repeats:NO];
2:
imgSplash = [[UIImage alloc] init];//Leak here, remove this line
imgSplash = [UIImage imageNamed:#"chicocredit_splash.png"];
3:
UIImageView *splashImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,460)];
...
[splashImageView setImage:imgSplash]; //Leak here, should release splashImageView
Why not, do everything else after timer expired and fading is done.
- (void) finishedFading
{
objMainMenuView = [[MainMenu alloc] initWithNibName:#"MainMenu" bundle:nil];
[self.navigationController pushViewController:objMainMenuView animated:YES];
}
short cut: use another viewController with imageView present it modally and dismiss with animation dissolve.
You should present view in viewWillAppear.Start timer on viewLoad of second viewController.When timer fires dismiss it.