iOS fading animation for multiple objects - ios

I have an iOS app in which every time the view is loaded, all of the objects in the view quickly fade in.
In my code, I have a
- (void)fadeInEverything
function, and within this function, I call five different functions, with an NSTimer on each of them, so that the objects fade in sequentially, like this:
[self fadeInLabel];
[NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:#selector(fadeInTextField)
userInfo:nil
repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.4
target:self
selector:#selector(fadeInButton)
userInfo:nil
repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.6
target:self
selector:#selector(fadeInTextView)
userInfo:nil
repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.8
target:self
selector:#selector(fadeInFlipsideViewButton)
userInfo:nil
repeats:NO];
I have a function that looks like this for each object:
- (void)fadeInTextView
{
[resultView setAlpha:0];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[resultView setAlpha:1];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[UIView commitAnimations];
}
My question is, would there we a way to make one function that would take a parameter (the object) and fade it in, so I don't have to have five almost identical functions? It would save me a lot of space and clutter.

You could pass your object by reference to accomplish that:
-(void)fadeInView:(UIView **)aView { // the ** indicates that this method is expecting a reference
// your fading code
// this works, because all view's (UILabels, UITextFields etc. inherit UIView.
}
And the calling method could look like this:
-(void)callFader {
[self fadeInView:&mytextView]; // make sure to put the & before the variable so that it's sent as a reference
// and more fadeInView calls...
}

Related

How to set time interval api call in ios objective c and reload the table view after getting success

I am new to ios development .I have issue . I need to call api after set time interval (ios objective c) . After getting success it should load the table view .
Below is what I would do...
Step 1 : Call webservice [self makeWebserviceCall]
Step 2 : When webservice call is done, use NSTimer in - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(makeWebserviceCall) userInfo:nil repeats:NO];
^^^ --> change this to increase more
That's it...
You can use the following code :
// Delay execution of my block for 10 seconds.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
// Call API
});
You can use NSTimer class for this
[NSTimer scheduledTimerWithTimeInterval:YOUR_TIME_INTERVAL target:self selector:#selector(apiCall:) userInfo:nil repeats:YES];
or use
[self performSelector:#selector(apiCall:) withObject:nil afterDelay:YOUR_TIME_INTERVAL];
and in apiCall method
[self performSelector:_cmd withObject:nil afterDelay:YOUR_TIME_INTERVAL];
But I think this way less clear
#define kAutoInterval 5.00f
[NSTimer scheduledTimerWithTimeInterval:kAutoInterval target:self selector:#selector(startUpdatingValues) userInfo:nil repeats:NO];

Stopping an UIView animation that was started but being delayed [duplicate]

This question already has answers here:
How to cancel UIView block-based animation?
(4 answers)
Closed 8 years ago.
[UIView animateWithDuration:1 delay:4 options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveEaseIn)
animations:^
{
// do something here (irrelevant)
}
completion:nil
];
How do I stop the animation within the time of the delay? For example, I want to stop this animation after I execute it but before the delay timer ends. I've tried [self.view.layer removeAllAnimations]; but to no avail.
EDIT: As I've stated. I've tried [self.view.layer removeAllAnimations]; but to no avail.
I didn't try it but you can stop all animations with [view.layer removeAllAnimations]; it should work.
You could use NSTimer and then just call [timer invalidate] if you no longer want the animation to run.
E.g.:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:#selector(animateView:) userInfo:nil repeats:NO];
// Then stop...
[timer invalidate];

calling method repeatedly after 3 seconds time interval in background

I have gone through many sites but still no answer.
I have a method suppose void xyz(), which will get called automatically from a View Controller after every 3 seconds.
I have no idea what to use, do I have to use NSThread or PerformSelector.
Call this method from ViewDidLoad method.ViewDidLoad will when your view will be appear in iPhone device or Simulator.
[NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:#selector(runMethod) userInfo:nil repeats:YES];
-(void)runMethod
{
}
Something like this
-(void)xyz{
[self performSelectorInBackground:#selector(xyz) withObject:nil];
}
- (void)viewDidLoad {
[self performSelector:#selector(xyz) withObject:nil afterDelay:0.3];
}
Use NSTimer
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:#selector(xyz) userInfo:nil repeats:YES];
You should use NSTimer as mentioned by #mokujin.
Please visit https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html

NSTimer does not stop

[self performSelectorInBackground:#selector(method) withObject:nil];
-(void)method
{
timer1 = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(getLastImageName1) userInfo:nil repeats:YES];
runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer1 forMode:NSRunLoopCommonModes];
[runLoop run];
}
-(void)viewdidunload
{
[timer1 invalidate];
timer1=nil;
}
I start Timer in HomeViewController even I invalidate, it keeps running in OtherViewController. What is wrong with my code?
First of all, when you're overriding life cycle methods, you should include a call to the super version of that method.
Second of all, Objective-C is case sensitive, so even if your app would try to call the life-cycle even, viewDidUnload, your method would simply never be called because that's what you titled your method.
Third of all, viewDidUnload was deprecated in iOS 6.0 and shouldn't be used at all by this point unless you're going way out of your way to support backward compatibility. It will never be called in iOS 6.0 and greater.
If you want the timer to stop when the user navigates away from the current view, you'll want something like this:
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
if (timer1.isValid) {
[timer1 invalidate];
}
timer1 = nil;
}
If you're looking for something else, you'll need to elaborate on what it is you want to accomplish exactly.
If you ARE working on a pre-iOS 6.0 project, for whatever reason, the reason your method isn't being called is at least in part because it is spelled wrong. Again, Objective-C is case sensitive. Your method name should be spelled viewDidUnload.
For future reference, the question shouldn't really be "why isn't my timer invalidating?" You should have start by using breakpoints or NSLog statements to determine whether or not your method, viewdidunload, which tries to invalidate the timer even fires. When you find out it's not being called, do a search to ask "How come viewdidunload isn't called?" Then you'll go fix the capitalization problem and the problem will (probably) remain, so do some more research. And if at the end, you still can't figure it out, as a worst case scenario, the post question should be "how come viewdidunload isn't called?"
timer1 = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(getLastImageName1:) userInfo:nil repeats:YES];
set colon for function in selector
-(void) getLastImageName1 :(NSTimer*)timer1
{
//Do your all process and invalidate after completion
[timer1 invalidate];
}
or if you want to remove timer after moving to next view controller use how #nhgrif mentioned
- (void)viewDidDisappear:(BOOL)animated
{
[timer1 invalidate];
}
[self performSelector:#selector(method) withObject:nil afterDelay:0.0];
-(void)method
{
timer1 = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(getLastImageName1) userInfo:nil repeats:YES];
runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer1 forMode:NSRunLoopCommonModes];
[runLoop run];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[timer1 invalidate];
timer1=nil;
}
There is no need to add the timer (again) on the main run loop. Or is it necessary for You to run it also in commonModes? For me it was never necessary.
From the NSTimer Documentation:
scheduledTimerWithTimeInterval:invocation:repeats:
Creates and returns a new NSTimer object and schedules it on the
current run loop in the default mode.
Since the NSRunLoop Documentation points out that you can add timer on several run loop modes, maybe this is the problem.
addTimer:forMode:
Discussion You can add a timer to multiple input modes. While running
in the designated mode, the receiver causes the timer to fire on or
after its scheduled fire date. Upon firing, the timer invokes its
associated handler routine, which is a selector on a designated
object.
Also I don't get why you are invoking the timer creation with performSelector?
I just wrote a minimalistic sample. thats totally working!
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(doWork:) userInfo:Nil repeats:YES];
}
- (void)viewDidDisappear:(BOOL)animated{
[self.timer invalidate];
[super viewDidDisappear:animated];
}
- (void) doWork:(id) userInfo
{
NSLog(#"Working again");
}
Hope this helps.
-(void)viewDidUnload is a delegate which fires on memory warning only and is deprecated after iOS 6. It will also never fire on the simulator.
Stop timer in
- (void)viewWillDisappear:(BOOL)animated
or in
- (void)viewDidDisappear:(BOOL)animated

viewWillAppear blocks all Animations

I have a GLKView because I use some OpenGLES for some animations with some graphics.
All these animations are working very good until I implement a viewWillAppear method.
I have two timers intialized in the viewDidLoad:
updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(updateScene) userInfo:nil repeats:YES];
paintTimer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:#selector(paintScene) userInfo:nil repeats:YES];
NSLog tells me that the two methods (updateScene and paintScene) are called correctly
and my graphics are changed correctly.
Now as long as I have no viewWillAppear method everything animates fine.
When I implement a viewWillAppear method, updateScene and paintScene are still
called but nothing changes. no animation. viewWillAppear is empty. no code inside.
Why are all animations blocked?
Make sure viewWillAppear calls [super viewWillAppear:animated];
This may fix your problem. Per Apple's docs:
If you override this method, you must call super at some point in your
implementation.

Resources