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.
Related
Background: I am using xcode 3.1.4 and iphone simulator 3.1 (I know it's outdated and please dont comment about that).
Goal: I am trying to get a UIImageView that is created once the view loads to continuously move down. Creating the UIImageView works fine, but the move function does not work. Nothing happens. I use an NSTimer. Here is my code in the view controller.m file:
-(void)viewDidLoad{
UIImageView *six = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"Meteor.png"]];
CGRect rectSix = CGRectMake(arc4random() % (250), arc4random() % (1), 35, 35);
[six setFrame:rectSix];
[self.view addSubview:six];
[self moveMeteor:six];
[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:#selector(moveMeteor:) userInfo:six repeats:YES];
}
-(void)moveMeteor:(UIImageView *)t{
t.center=CGPointMake(t.center.x, t.center.y + 1);
}
And of course, I declared my moveMeteor function in the view controller.h file. By typing in:
-(void)moveMeteor:(UIImageView *)t;
Any ideas of what the problem is and a solution to it?
Or you could do something like this, which takes advantage of UIView's animateWithDuration and should look much better. No timer required.
// in your interface declaration...
#property (nonatomic, assign) BOOL meteorShouldAnimate;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
// in your implementation...
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.meteorShouldAnimate = YES;
[self moveMeteorWithAnimationOptions:UIViewAnimationOptionCurveEaseIn]; // ease in to the animation initially
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.meteorShouldAnimate = NO;
}
- (void)moveMeteorWithAnimationOptions: (UIViewAnimationOptions) options {
__weak MyViewController * weakSelf = self;
if (self.meteorShouldAnimate) {
[UIView animateWithDuration:0.2 delay:0.0 options:options animations:^{
self.imageView.transform = CGAffineTransformMakeTranslation(20.0, 20.0); // or whatever x and y values you choose
} completion:^(BOOL finished) {
MyViewController * strongSelf = weakSelf; // in case our view controller is destroyed before our completion handler is called
if (strongSelf) {
UIViewAnimationOptions nextOptions = strongSelf.meteorShouldAnimate ? UIViewAnimationOptionCurveLinear : UIViewAnimationOptionCurveEaseOut; // linear if we're continuing to animate, ease out if we're stopping
[strongSelf moveMeteorWithAnimationOptions:nextOptions];
}
}];
}
}
Your method is actually not getting the right parameter, you get NSTimer as a parameter. so instead
-(void)moveMeteor:(UIImageView *)t
the method should be like this
-(void)moveMeteor:(NSTimer *)timer
and you can't treat NSTimer as an UIImageView. :)
*Edit
I would suggest you to do something like
-(void)viewDidLoad{
UIImageView *six = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"Meteor.png"]];
CGRect rectSix = CGRectMake(arc4random() % (250), arc4random() % (250), 35, 35);
[six setFrame:rectSix];
[self.view addSubview:six];
[self moveMeteor:six];
[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:#selector(moveMeteor:) userInfo:six repeats:YES];
}
and update your method to this
- (void)moveMeteor:(NSTimer *)timer {
UIImageView *six = timer.userInfo;
six.center=CGPointMake(six.center.x, six.center.y + 1);
}
just for information, I am passing the UIImageView in the dictionary as userInfo.
I need to implement animated splash screen to the iPhone application. I have seen skype application where same thing is already implemented.
Can anyone has idea how can i implement same thing in my applicatio
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.
OR
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];
}
We can show a .gif image in webView and it looks perfect!
Take a new UIViewController class named SplashView with XIB and then add an UIWebView with (320.0, 480.0) frame, hidden statusbar also.
In SplashView.h
#import <UIKit/UIKit.h>
#interface SplashView : UIViewController
#property(nonatomic, retain)IBOutlet UIWebView *webView;
#end
In SplashView.m
- (void)viewDidLoad
{
[super viewDidLoad];
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];
}
This is about SplashView class. Now come to your appdelegate's class.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
splashView = [[SplashView alloc]initWithNibName:#"SplashView" bundle:nil];
[self.window addSubview:splashView.view];
[self performSelector:#selector(changeView) withObject:nil afterDelay:3.0];
[self.window makeKeyAndVisible];
return YES;
}
-(void)changeView
{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[splashView.view removeFromSuperview];
[self.window setRootViewController:self.viewController];
}
Take a UIView & an Imageview into it. Give all your images to ImageView to animate.
-(void)viewDidLoad
{
NSArray *arrImage=[NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
nil];
imgVw.backgroundColor=[UIColor purpleColor];
imgVw.animationImages=arrImage;
imgVw.animationDuration=2.5;
imgVw.animationRepeatCount=1;
[imgVw startAnimating];
[NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:#selector(animateNext) userInfo:nil repeats:NO];
}
This will show up you application icon.
After that you will show the controls those would be hidden by default and animate them from bottom to up.
-(void)animateNext
{
lbl.hidden = NO;
btn.hidden = NO;
txt1.hidden = NO;
txt2.hidden = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.9];
lbl.frame=CGRectMake(lbl.frame.origin.x,lbl.frame.origin.y - 150,lbl.frame.size.width,lbl.frame.size.height);
imgVw.frame = CGRectMake(imgVw.frame.origin.x, imgVw.frame.origin.y - 150, imgVw.frame.size.width, imgVw.frame.size.height);
txt1.frame = CGRectMake(txt1.frame.origin.x, txt1.frame.origin.y - 150, txt1.frame.size.width, txt1.frame.size.height);
txt2.frame = CGRectMake(txt2.frame.origin.x, txt2.frame.origin.y - 150, txt2.frame.size.width, txt2.frame.size.height);
btn.frame = CGRectMake(btn.frame.origin.x, btn.frame.origin.y - 150, btn.frame.size.width, btn.frame.size.height);
[UIView commitAnimations];
}
Hope this help...
Try this
Appdelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UIImageView *splashView;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
- (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
// Make this interesting.
splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
splashView.image = [UIImage imageNamed:#"Default.png"];
[self.window addSubview:splashView];
[self.window bringSubviewToFront:splashView];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.window cache:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(startupAnimationDone:finished:context:)];
splashView.alpha = 0.0;
splashView.frame = CGRectMake(-60, -85, 440, 635);
[UIView commitAnimations];
return YES;
}
- (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[splashView removeFromSuperview];
}
you need to start your app with a viewcontroller,with an uiimageview in it..Create a series of .png images to be subjected to the UIImageView check how to animate array of images in uiimageview. Further to dismiss it once animation over you would need to implement a protocol that will inform your starting first viewcontroller of your app to dismiss the animation
- (void) welcomeScreen
{
//Welcome Screen
UIImageView* welcome = [[[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)]autorelease];
welcome.image = [UIImage imageNamed:#"img.png"];
[window addSubview:welcome];
[window bringSubviewToFront:welcome];
//Animation Effects (zoom and fade)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:window cache:YES];
[UIView setAnimationDelegate:welcome];
[UIView setAnimationDidStopSelector:#selector(removeFromSuperview)];
//set transparency to 0.0
welcome.alpha = 0.0;
//zoom effect
welcome.frame = CGRectMake(-60, -60, 440, 600);
[UIView commitAnimations];
}
yes simple in AppDelegate class first defile imageview like bellow..
#interface AppDelegate : UIResponder
{
UIImageView *splashView;
}
and in .m file...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
splashView.image = [UIImage imageNamed:#"Default"];
[self.window addSubview:splashView];
[self performSelector:#selector(loadViewIphone) withObject:nil afterDelay:2.0];
}
[self.window makeKeyAndVisible];
return YES;
}
-(void)loadViewIphone
{
[splashView removeFromSuperview];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:kCATransitionFade];
[animation setDuration:0.5];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:
kCAMediaTimingFunctionEaseInEaseOut]];
[[self.window layer] addAnimation:animation forKey:#"transitionViewAnimation"];
}
i hope this help you..
:)
I found this code to change the image when it is clicked.
in .h
#interface MyappViewController : UIViewController
{
NSDictionary *ddata;
UIImageView *firstImage;
}
#property(retain,nonatomic) IBOutlet UIImageView *firstImage;
in .m
- (void)viewDidLoad
{
[super viewDidLoad];
firstImage.userInteractionEnabled = YES;
UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:#selector(clickHandler:)];
pgr.delegate = self;
[firstImage addGestureRecognizer:pgr];
[pgr release];
// [self clickHandler:self];
}
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
/* destroy the transition view, set the image first */
UIImageView *transitionImageView = (UIImageView *)context;
self.firstImage.image = transitionImageView.image;
[transitionImageView removeFromSuperview];
transitionImageView = nil;
}
- (void)clickHandler:(id)sender {
/* temporary view for the animation */
NSLog(#"Click Handled ");
UIImageView *transitionImageView = [[UIImageView alloc] initWithFrame:self.firstImage.frame];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[ddata objectForKey:#"pic"]]]];
transitionImageView.image = image;
transitionImageView.alpha = 0.0f;
[self.view addSubview:transitionImageView];
[UIView beginAnimations:#"UpdateImages" context:transitionImageView];
[UIView setAnimationDuration:2.0f];
transitionImageView.alpha = 1.0f;
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[UIView commitAnimations];
}
When I click the image nothing happens, but if I call [self clickHandler:self]; in ViewDidLoad, the image changes. My problem is that the click is not handled when I click the image.
Instead of a UIPinchGestureRecognizer you need to use a UITapGestureRecognizer. Don't forget to set things like the number of taps required and number of fingers either. The docs are very good for gesture recognizers.
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];
}
I am programmatically adding a UIImageView and a UILabel to multiple screens in a program I'm working on. As such, I've written a UIViewController that contains the code to load up the UIImageView and UILabel in viewDidLoad, along with a timer to update the contents. I use this view controller as a superclass for any views that need the UIImageView and UILabel.
For the most part it works fine, but when transitioning from specific screens the pair seem to start with a frame of (0,0,0,0) and slowly animate to the frame I've defined. I have no idea what the issue is or even where to look. Any pointers here would be great.
Here is the code I'm using as my superclass.
FoundationViewController.h
#import
#interface FoundationViewController : UIViewController {
IBOutlet UIImageView *activeInventoryImage;
IBOutlet UILabel *activeInventoryLabel;
NSTimer *inventoryStatusTimer;
}
#property (nonatomic, retain) IBOutlet UIImageView *activeInventoryImage;
#property (nonatomic, retain) IBOutlet UILabel *activeInventoryLabel;
- (void) advanceToView:(id)nextView;
- (void) inventoryStatus;
- (BOOL) inventoryActive:(NSDate*)inventoryExpire withImage:(NSString*)imageName;
#end
FoundationViewController.m
#import "FoundationViewController.h"
#import "GameData.h"
#import "Globals.h"
#implementation FoundationViewController
#synthesize activeInventoryImage;
#synthesize activeInventoryLabel;
- (void) viewDidLoad{
// initialize the active inventory icon and label
activeInventoryImage = [[UIImageView alloc] init];
activeInventoryLabel = [[UILabel alloc] init];
// clear
activeInventoryImage.image = [UIImage imageNamed:#""];
activeInventoryLabel.text = #"";
// set the center points
activeInventoryImage.frame = CGRectMake(258, 7, 20, 20);
activeInventoryLabel.frame = CGRectMake(280, 6, 30, 20);
// set label parameters
activeInventoryLabel.backgroundColor = [UIColor clearColor];
activeInventoryLabel.font = [UIFont fontWithName:#"Helvetica" size:11.0f];
activeInventoryLabel.textColor = [UIColor colorWithRed:0.2 green:0.4 blue:0.6 alpha:1];
// add to view
[self.view addSubview:activeInventoryImage];
[self.view addSubview:activeInventoryLabel];
// start the timer if there is any active inventory
if(
[self inventoryActive:[GameData sharedGameData].player.inventory.aExpire withImage:#"a.png"] ||
[self inventoryActive:[GameData sharedGameData].player.inventory.bExpire withImage:#"b.png"] ||
[self inventoryActive:[GameData sharedGameData].player.inventory.cExpire withImage:#"c.png"]
)
inventoryStatusTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(inventoryStatus) userInfo:nil repeats:YES];
[super viewDidLoad];
}
- (BOOL) inventoryActive:(NSDate*)inventoryExpire withImage:(NSString*)imageName{
NSTimeInterval expireSeconds;
NSDateFormatter *formatter;
BOOL runTimer = FALSE;
// expire
expireSeconds = [inventoryExpire timeIntervalSinceNow];
if(expireSeconds > 0){
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"mm:ss"];
NSDate *expireTime = [NSDate dateWithTimeIntervalSince1970:expireSeconds];
activeInventoryImage.image = [UIImage imageNamed:imageName];
activeInventoryLabel.text = [formatter stringFromDate:expireTime];
[formatter release];
runTimer = TRUE;
}
return runTimer;
}
- (void) inventoryStatus{
// if there is no active inventory kill the timer
if(
![self inventoryActive:[GameData sharedGameData].player.inventory.aExpire withImage:#"a.png"] &&
![self inventoryActive:[GameData sharedGameData].player.inventory.bExpire withImage:#"b.png"] &&
![self inventoryActive:[GameData sharedGameData].player.inventory.cExpire withImage:#"c.png"]
){
activeInventoryImage.image = [UIImage imageNamed:#""];
activeInventoryLabel.text = #"";
if(inventoryStatusTimer != nil)
[inventoryStatusTimer invalidate];
}
}
- (void) advanceToView:(id)nextView{
if([nextView isKindOfClass:[UIView class]]){
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.7];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
[self.view addSubview:nextView];
[UIView commitAnimations];
}
else
NSLog(#" Error Loading View: %#",nextView);
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[activeInventoryImage removeFromSuperview];
[activeInventoryImage release];
[activeInventoryLabel release];
[super dealloc];
}
#end
Put a break point in advanceToView as this seems to be your only animation code. Everytime it gets called, look at the call stack in the debugger. From here, you can see what is calling this code when they are not supposed to be.
After some experimenting I've discovered using initWithFrame corrects the drifting problem.
activeInventoryImage = [[UIImageView alloc] initWithFrame:CGRectMake(258, 7, 20, 20)];
activeInventoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(280, 6, 30, 20)];
I still have no idea what the root cause of the issue is. If anyone could tell me what's going on here I would love to know for future reference.