Last active
December 19, 2015 11:09
-
-
Save danmatthews/5945456 to your computer and use it in GitHub Desktop.
Basic loading image fading in and out using NSTimer and UIImageView / UIImage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Create a container UIView - actually, this part might not be needed at all!. | |
// Establish this view with a 150x150pt frame, to fit our custom image. | |
// Note that it's in the top-left for the moment, don't worry about that for now. | |
UIView *loader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)]; | |
// Load your image data using the +imageNamed method. | |
UIImage *loaderImage = [UIImage imageNamed:@"Loading.png"]; | |
// Use the imageview to populate a UIImageView | |
UIImageView *loaderImageView = [[UIImageView alloc] initWithImage:loaderImage]; | |
// Add the UIImageView as a subview of the containing UIView. | |
[loader addSubview:loaderImageView]; | |
// Add the loader container view to our main UIViewControllers view | |
[self.view addSubview:loader]; | |
// Start the initial animation - the name here doesn't matter for now. | |
[UIImageView beginAnimations:@"FadeInOut" context:nil]; | |
// Make it last one second. | |
[UIImageView setAnimationDuration:1.0f]; | |
// Add a quick ease-in-out curve. | |
[UIImageView setAnimationCurve:UIViewAnimationCurveEaseInOut]; | |
// Now set the property we wanna animate, this being the alpha. | |
loaderImageView.alpha = 0.0f; | |
// Commit animations, this runs them. | |
[UIImageView commitAnimations]; | |
// Now, we wanna keep animating this thing until the sun goes down, so set up a timer | |
[NSTimer scheduledTimerWithTimeInterval:1.0 // How often to send a message asking what to do. | |
target:self // Sends a message to this class at the interval above. | |
selector:@selector(timerHitInterval:) // The method we define below to receive the message | |
userInfo:loaderImageView // pass our loaderImageView as userInfo so we can adjust it | |
repeats:YES]; // Tell it to repeat after the first one | |
} | |
// Now define this method that will receive the message from the NSTimer instance every second. | |
- (void)timerHitInterval:(NSTimer*)timer | |
{ | |
// Assign our loaderView reference to a variable for compiler sanity. | |
UIImageView *loaderView = timer.userInfo; | |
// Declare this here - we're gonna change it dynamically. | |
CGFloat newLoaderAlpha; | |
if (loaderView.alpha == 0) { | |
newLoaderAlpha = 1.0; // If the opacity is at 0, send it to 1. | |
} else { | |
newLoaderAlpha = 0.0; // Vice versa. | |
} | |
// Perform the animations as in ViewDidLoad. | |
[UIImageView beginAnimations:@"FadeInOut" context:nil]; | |
[UIImageView setAnimationDuration:1.0f]; | |
[UIImageView setAnimationCurve:UIViewAnimationCurveEaseInOut]; | |
loaderView.alpha = newLoaderAlpha; | |
[UIImageView commitAnimations]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment