Cant make UIlabel text blink in Xcode - ios

I have been trying to make my UILabel blink in Xcode
but the problem is it does not blink
Here is my code:
self.labelCountdownTime.alpha = 0.0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:1.0];
[UIView setAnimationRepeatCount:FLT_MAX];
self.labelCountdownTime.alpha = 1.0;
[UIView commitAnimations];
Is there something I'm doing wrong.
Thanks

You can use NSTimer to blink your text in UILabel like so:
NSTimer *timer = [NSTimer
scheduledTimerWithTimeInterval:(NSTimeInterval)(1.0)
target:self
selector:#selector(blink)
userInfo:nil
repeats:YES];
BOOL blinkStatus = NO;
And the selector will call this function:
-(void)blink{
if(blinkStatus == NO){
yourLabel.alpha = 1.0;
blinkStatus = YES;
}else {
yourLabel.alpha = 0.0;
blinkStatus = NO;
}
}
Or you can use method animationWithDuration of UIView like so:
self.yourLabel.alpha = 0;
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{
self.yourLabel.alpha = 1;
} completion:nil];
Hope this will help you.

I Think you are wanting some thing more like the following:
Create a timer in your controller:
#property (strong, nonatomic) NSTimer *timer;
then start the timer where you need it to start, like maybe in viewDidLoad
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(toggleLabelAlpha) userInfo:nil repeats:YES];
and here is the selector:
- (void)toggleLabelAlpha {
[self.labelCountdownTime setHidden:(!self.labelCountdownTime.hidden)];
}
Try this ?

Related

Xcode Disposing memory - images taking up too much memory and won't deallocate

Updated:
I have a lot of view controllers and the app is image heavy. Each is presented modally at the moment. I'm disposing of all my images,timers etc and whatever else in viewwilldisappear on every view controller, but when it segues it just seems to be allocating memory continually on top of the previous VC and everything else without getting rid of a single thing which eventually crashes it (i know this will happen with modal segues).
How can i change this? I have tried embedding my controllers in a navigation controller but the same problem is still happening where it will not release any of the images on the controllers. Here is an example of my code and where i'm getting rid of what i've created in VWD. VWD is definitely being called. Hopefully this makes sense to someone, thanks in advance.
#interface AnimationStartViewController ()
#end
#implementation AnimationStartViewController
SystemSoundID skipintro;
-(void) viewDidLoad
{
[super viewDidLoad];
(sleep(2.5));
NSString *music=[[NSBundle mainBundle]pathForResource:#"1Animation" ofType:#"mp3"];
animationaudio=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:music] error:NULL];
animationaudio.delegate= (id)self;
animationaudio.numberOfLoops=-1;
[animationaudio play];
NSURL *skipintroURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Click" ofType:#"mp3"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)skipintroURL, &skipintro);
//Text Timers
text1timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:#selector(Text1show:) userInfo:nil repeats:NO];
text2timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:#selector(Text2show:) userInfo:nil repeats:NO];
text3timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:#selector(Text3show:) userInfo:nil repeats:NO];
text4timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:#selector(Text4show:) userInfo:nil repeats:NO];
text5timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:#selector(Text5show:) userInfo:nil repeats:NO];
text6timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:#selector(Text6show:) userInfo:nil repeats:NO];
text7timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:#selector(Text7show:) userInfo:nil repeats:NO];
text8timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:#selector(Text8show:) userInfo:nil repeats:NO];
logotimer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:#selector(showlogo) userInfo:nil repeats:NO];
pushtimer = [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:#selector(performthesegue) userInfo:nil repeats:NO];
//Text Timers invalidate
stoptext1timer = [NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:#selector(invalidatetext1) userInfo:nil repeats:NO];
stoptext2timer = [NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:#selector(invalidatetext2) userInfo:nil repeats:NO];
stoptext3timer = [NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:#selector(invalidatetext3) userInfo:nil repeats:NO];
stoptext4timer = [NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:#selector(invalidatetext4) userInfo:nil repeats:NO];
stoptext5timer = [NSTimer scheduledTimerWithTimeInterval:14.5 target:self selector:#selector(invalidatetext5) userInfo:nil repeats:NO];
stoptext6timer = [NSTimer scheduledTimerWithTimeInterval:14.5 target:self selector:#selector(invalidatetext6) userInfo:nil repeats:NO];
stoptext7timer = [NSTimer scheduledTimerWithTimeInterval:14.5 target:self selector:#selector(invalidatetext7) userInfo:nil repeats:NO];
stoptext8timer = [NSTimer scheduledTimerWithTimeInterval:14.5 target:self selector:#selector(invalidatetext8) userInfo:nil repeats:NO];
moveAnimationTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(moveAnimation) userInfo:nil repeats:YES];
stopAnimationTimer = [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:#selector(StopAnimation) userInfo:nil repeats:NO];
}
//scrolling Animation.
- (void) moveAnimation {
animation.center = CGPointMake(animation.center.x, animation.center.y -2);
}
//Stop
- (void) StopAnimation {
[moveAnimationTimer invalidate];
moveAnimationTimer = nil;
}
//====================== Text animations
//AttheGateof
-(void)Text1show:(NSTimer *)timer {
[UIView animateWithDuration:2.0 delay:0.70
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^ {
Atthegateof.alpha = 1.0f;
}
completion:^(BOOL finished) {
Atthegateof.alpha = 1.0f;
}];
}
//MidnightZoo
-(void)Text2show:(NSTimer *)timer {
[UIView animateWithDuration:2.0 delay:1.75
options:UIViewAnimationOptionCurveEaseInOut
animations:^ {
MidnightZoo.alpha = 1.0f;
}
completion:^(BOOL finished) {
MidnightZoo.alpha = 1.0f;
}];
}
//Who lives inside
-(void)Text3show:(NSTimer *)timer {
[UIView animateWithDuration:2.0 delay:2.9
options:UIViewAnimationOptionCurveEaseInOut
animations:^ {
Wholivesinside.alpha = 1.0f;
}
completion:^(BOOL finished) {
}];
}
// Thedeepblackblue
-(void)Text4show:(NSTimer *)timer {
[UIView animateWithDuration:2.0 delay:3.3
options:UIViewAnimationOptionCurveEaseInOut
animations:^ {
Thedeepblackblue.alpha = 1.0f;
}
completion:^(BOOL finished) {
}];
}
//unlockthegate
-(void)Text5show:(NSTimer *)timer {
[UIView animateWithDuration:2 delay:7
options:UIViewAnimationOptionCurveEaseInOut
animations:^ {
Unlockthegate.alpha = 1.0f;
}
completion:^(BOOL finished) {
}];
}
//tocomeinside
-(void)Text6show:(NSTimer *)timer {
//music 'high' point
[UIView animateWithDuration:2 delay:7.5
options:UIViewAnimationOptionCurveEaseInOut
animations:^ {
Tocomeinside.alpha = 1.0f;
}
completion:^(BOOL finished) {
}];
}
//behindeachlock
-(void)Text7show:(NSTimer *)timer {
[UIView animateWithDuration:2 delay:8.2
options:UIViewAnimationOptionCurveEaseInOut
animations:^ {
Behindeachlock.alpha = 1.0f;
}
completion:^(BOOL finished) {
}];
}
//something hides
-(void)Text8show:(NSTimer *)timer {
[UIView animateWithDuration:2 delay:8.7
options:UIViewAnimationOptionCurveEaseInOut
animations:^ {
Somethinghides.alpha = 1.0f;
}
completion:^(BOOL finished) {
}];
}
//======================================= Get rid of text
//indvalidate 1
-(void)invalidatetext1 {
[stoptext1timer invalidate];
stoptext1timer = nil;
[UIView animateWithDuration:0.7 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
Atthegateof.alpha = 0;
} completion:^(BOOL finished) {
[Atthegateof removeFromSuperview];
} ];
}
//invalidate 2
-(void)invalidatetext2 {
[stoptext2timer invalidate];
stoptext2timer = nil;
[UIView animateWithDuration:0.7 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
MidnightZoo.alpha = 0;
} completion:^(BOOL finished) {
[MidnightZoo removeFromSuperview];
} ];
}
//invalidate 3
-(void)invalidatetext3 {
[stoptext3timer invalidate];
stoptext3timer = nil;
[UIView animateWithDuration:0.7 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
Wholivesinside.alpha = 0;
} completion:^(BOOL finished) {
[Wholivesinside removeFromSuperview];
} ];
}
//invalidate 4
-(void)invalidatetext4 {
[stoptext4timer invalidate];
stoptext4timer = nil;
[UIView animateWithDuration:0.7 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
Thedeepblackblue.alpha = 0;
} completion:^(BOOL finished) {
[Thedeepblackblue removeFromSuperview];
} ];
}
//invalidate 5
-(void)invalidatetext5 {
[stoptext5timer invalidate];
stoptext5timer = nil;
[UIView animateWithDuration:0.75 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
Unlockthegate.alpha = 0;
} completion:^(BOOL finished) {
[Unlockthegate removeFromSuperview];
} ];
}
//invalidate 6
-(void)invalidatetext6 {
[stoptext6timer invalidate];
stoptext6timer = nil;
[UIView animateWithDuration:0.75 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
Tocomeinside.alpha = 0;
} completion:^(BOOL finished) {
[Tocomeinside removeFromSuperview];
} ];
}
//invalidate 7
-(void)invalidatetext7 {
[stoptext7timer invalidate];
stoptext7timer = nil;
[UIView animateWithDuration:0.75 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
Behindeachlock.alpha = 0;
} completion:^(BOOL finished) {
[Behindeachlock removeFromSuperview];
} ];
}
//invalidate 8
-(void)invalidatetext8 {
[stoptext8timer invalidate];
stoptext8timer = nil;
[UIView animateWithDuration:0.75 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
Somethinghides.alpha = 0;
} completion:^(BOOL finished) {
[Somethinghides removeFromSuperview];
} ];
}
//=======================================
-(void)showlogo {
[UIView animateWithDuration:2.0 delay:16.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^ {
zoowhologo.alpha = 1.0f;
}
completion:^(BOOL finished) {
zoowhologo.alpha = 1.0f;
}];
}
-(void)performthesegue {
[self performSegueWithIdentifier:#"pushgototitle" sender:nil];
}
- (void)didReceiveMemoryWarning
{
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
NSLog(#"viewilldisappear was called");
[animationaudio stop];
[text1timer invalidate], text1timer=nil;
[text2timer invalidate], text2timer=nil;
[text3timer invalidate], text3timer=nil;
[text4timer invalidate], text4timer=nil;
[text5timer invalidate], text5timer=nil;
[text6timer invalidate], text6timer=nil;
[text7timer invalidate], text7timer=nil;
[text8timer invalidate], text8timer=nil;
[stoptext1timer invalidate], stoptext1timer=nil;
[stoptext2timer invalidate], stoptext2timer=nil;
[stoptext3timer invalidate], stoptext3timer=nil;
[stoptext4timer invalidate], stoptext4timer=nil;
[stoptext5timer invalidate], stoptext5timer=nil;
[stoptext6timer invalidate], stoptext6timer=nil;
[stoptext7timer invalidate], stoptext7timer=nil;
[stoptext8timer invalidate], stoptext8timer=nil;
[logotimer invalidate], logotimer=nil;
[pushtimer invalidate], pushtimer=nil;
[moveAnimationTimer invalidate], moveAnimationTimer=nil,
[stopAnimationTimer invalidate], stopAnimationTimer=nil;
AudioServicesDisposeSystemSoundID(skipintro);
[Atthegateof removeFromSuperview], Atthegateof=nil;
[MidnightZoo removeFromSuperview], MidnightZoo=nil;
[Wholivesinside removeFromSuperview], Wholivesinside=nil;
[Thedeepblackblue removeFromSuperview], Thedeepblackblue=nil;
[Unlockthegate removeFromSuperview], Unlockthegate=nil;
[Tocomeinside removeFromSuperview], Tocomeinside=nil;
[Behindeachlock removeFromSuperview], Behindeachlock=nil;
[Somethinghides removeFromSuperview], Somethinghides=nil;
[topper removeFromSuperview], topper=nil;
[skip removeFromSuperview], skip=nil;
[zoowhologo removeFromSuperview], zoowhologo=nil;
[animation removeFromSuperview], animation = nil;
[stopanimation removeFromSuperview], stopanimation = nil;
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)skipintro:(id)sender {
AudioServicesPlaySystemSound(skipintro);
[animationaudio stop];
[pushtimer invalidate];
[self performSegueWithIdentifier:#"pushgototitle2" sender:self];
[self.presentingViewController dismissViewControllerAnimated:NO completion:nil];
}
Several things.
If you present view controllers modally, they get stacked up one on top of the next, and none of them get released until you dismiss them. Make sure you're not creating a new instance of a previous view controller and pushing it modally instead of dismissing back to the desired VC.
Second,
It used to be that when a view controller was moved off-screen, it's view hierarchy was unloaded, freeing up most of the memory it took. That's no longer true. (and hasn't been for quite a few major iOS releases. I want to say this changed in iOS 4, but am not positive of this.)
If a view controller is in memory, so is it's entire view hierarchy, including any images that are installed in image views inside those views.
You can indeed implement a viewWillDisappear method and use that method to nil out any data structures that are not installed in your view hierarchy. You then need to implement a viewWillAppear method that reloads those data structures.
If your app presents a series of view controllers modally, and then the user "unrolls" each view controller and returns back to the root view controller then your method of using modal presentation is reasonable. (Using a navigation controller and a stack of pushed view controllers might be better, but you claim not.) Both approaches keep a stack of all the previously visited view controllers active and in memory.
If instead your UX has your user move from VC to VC without the need to return, you should instead implement your own custom segue that dismisses the previous view controller, or perhaps uses a child view controller that it swaps for another one as the user navigates.

Adding subviews with time delay on UISlider?

I have a UISlider and I am trying to add subviews dynamically on the track of the UISlider with animation. I need 1 second time delay before adding each subview. How can I achieve this?
This is a basic setup of how you can go about achieving it:
#interface ViewController () {
NSTimer *_timer;
CGRect sliderFrame;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.slider.minimumValue = 0.0f;
self.slider.maximumValue = 100.0f;
sliderFrame = self.slider.frame;
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(addLabel:) userInfo:nil repeats:YES];
[_timer fire];
}
- (void) addLabel:(NSTimer*) timer {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(sliderFrame.origin.x + self.slider.value/100 * self.slider.frame.size.width, sliderFrame.origin.y - 10, 20, 20)];
label.text = #"1";
[self.view addSubview:label];
}
#end
Assuming you are using an animation block, then, within your block you can use setAnimationDelay. For example;
[UIView animateWithDuration:5.0 animations:
^{
[UIView setAnimationDelay:1.0];
...
} completion:^(BOOL finished){
...
}];
This would do a 5 second animation with a delay of 1 second before it started.
you can hook up the valueChangeEvent with an action method.
You can put an animation block in that method with passing the seconds to wait before starting an animation as delay parameter.
[UIView animateWithDuration:(NSTimeInterval) delay:(NSTimeInterval) options:(UIViewAnimationOptions) animations:^(void)animations completion:^(BOOL finished)completion]
then you can change add the view with whatever animation your require.

Getting Timer error

After creating an image at a random location (spawnGood), if that object collides with the user controlled image (userToken) an NSTimer should start to work which uses the 'stick' method. This method is supposed to move spawnGood to the same position as userToken if userToken is moved (eg. sticks itself to userToken). However, I get the 'unrecognised selector sent to instance' error when the two images collide. It may either be a problem with the timer or the method, so is anyone able to suggest a solution to this? Thanks in advance :)
The code below produces the described error:
-(void)spawn{
spawn = [NSTimer scheduledTimerWithTimeInterval:2.0f
target:self
selector:#selector(spawnMechanism)
userInfo:nil
repeats:YES];
}
-(void)stick:(UIImageView *)spawnGood{
[UIView animateWithDuration:3.0
delay: 0
options: UIViewAnimationOptionCurveLinear
animations:^{
spawnGood.center=CGPointMake(userToken.center.x, userToken.center.y);
}
completion:^(BOOL finished){
NSLog(#"complete");
}];
}
-(void)spawnMechanism{
timeSinceLastSpawn++;
if(timeSinceLastSpawn >= 3)
{
if( arc4random() % 10 < 7 ){
UIImageView *spawnGood;
spawnGood=[[UIImageView alloc]initWithFrame:CGRectMake(arc4random() % 700, -100, 70,70)];
UIImage *image;
image=[UIImage imageNamed:#"friendly-01"];
[spawnGood setImage:image];
[array addObject:spawnGood];
[self.view addSubview:spawnGood];
NSLog(#"spawnGood spawned");
[UIView animateWithDuration:2.0
delay: 0.0
options: UIViewAnimationOptionCurveLinear
animations:^{
CGRect frame1 =[spawnGood frame];
frame1.origin.y =frame1.origin.y+950;
[spawnGood setFrame:frame1];
}
completion:^(BOOL finished){
if(CGRectIntersectsRect(userToken.frame, spawnGood.frame)){
NSLog(#"Good Collision");
stick=[NSTimer scheduledTimerWithTimeInterval:rate
target:stick
selector:#selector(stick:)
userInfo:nil
repeats:YES];
}else{
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
CGRect frame1 =[spawnGood frame];
frame1.origin.y =frame1.origin.y+950;
[spawnGood setFrame:frame1];
}
completion:nil];
};
}
];
}
}
}
You have two errors:
the target in this line
stick=[NSTimer scheduledTimerWithTimeInterval:rate
target:stick
selector:#selector(stick:)
userInfo:nil
repeats:YES];
should be 'self', not 'stick' (which is presumably an NSTimer * class variable?)
the parameter of stick should be an NSTimer *, not an image view.
You should prettify your code and add more context.

NSTimer not invalidating in iOS 7, when navigation to new view in iPhone [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am facing some issue related to NSTimer. I need to show a UIButton at the bottom in timeframe of 5 seconds. I implemented NSTimer functionality and it showing button in that time. However when i navigate to a new View. I need to stop that NSTimer or invalidate. I have done that in - (void)viewDidDisappear:(BOOL)animated. However NSTimer is still being executed. Its not been stopped at any time.Below is my code.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self._sizeNowButton = [[UIButton alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height+70, 320, 44)];
[TLStylesheet useTurquoiseStyleForButton:self._sizeNowButton withText:NSLocalizedString(#"Size Now", nil) withFontSize:BUTTON_BOTTOM_FONT_SIZE];
[self._sizeNowButton addTarget:self action:#selector(sizeNowClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self._sizeNowButton];
SEL tSelector = NSSelectorFromString(#"aTimer");
self._aTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:tSelector userInfo:nil repeats:NO];
}
-(void)aTimer
{
self._tTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(showButton) userInfo:nil repeats:YES];
}
-(void)showButton {
if([self._sizeNowButton isHidden] )
{
self._sizeNowButton.hidden = NO;
CGRect newFrame = CGRectMake(self._sizeNowButton .frame.origin.x , self._sizeNowButton .frame.origin.y - 44, self._sizeNowButton .frame.size.width, self._sizeNowButton .frame.size.height);
[UIView animateWithDuration:0.5f
delay:0.0
options:UIViewAnimationOptionTransitionFlipFromBottom
animations:^{
[self._sizeNowButton setFrame:newFrame];
}
completion:nil];
}
else
{
CGRect newFrame = CGRectMake(0, self.view.frame.size.height, 320, 44);
[UIView animateWithDuration:0.5f
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
[self._sizeNowButton setFrame:newFrame];
}
completion:nil];
self._sizeNowButton .hidden = YES;
}
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self._tTimer invalidate];
self._tTimer = nil;
self._sizeNowButton = nil;
}
You are instantiating self._aTimer but invalidating self._tTimer.
As you appear to trigger two different timers (_aTimer and _tTimer), you will want to disable both in your viewWillDisappear:.
if (self._aTimer)
[self._aTimer invalidate];
self._aTimer = nil;
if (self._tTimer)
[self._tTimer invalidate];
self._tTimer = nil;
try this:
start timer like this
self. _tTimer = [NSTimer timerWithTimeInterval:5.0 target:self selector:#selector(showButton) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self. _tTimer forMode:NSDefaultRunLoopMode];
[self. _tTimer fire];
and stop like this-
if (self. _tTimer)
{
[self. _tTimer invalidate];
self. _tTimer = nil;
}

ios animation one by one

I have some views and I want to display them one by one . I can't find how to do this . Instead , if I use the following code , all the views appear at once .
NSTimer *timer1 = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(showStar2) userInfo:nil repeats:NO];
NSTimer *timer2 = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(showStar3) userInfo:nil repeats:NO];
NSTimer *timer3 = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(showStar4) userInfo:nil repeats:NO];
NSTimer *timer4 = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(showStar5) userInfo:nil repeats:NO];
[timer1 fire];
[timer2 fire];
[timer3 fire];
[timer4 fire];
-(void)showStar2
{
[UIView beginAnimations:#"anim1" context:NULL];
[UIView setAnimationDuration:0.5];
[stars[1] setAlpha:1];
[UIView commitAnimations];
}
All the showStar functions are identical except the lines
[UIView beginAnimations:#"anim1" context:NULL];
[stars[1] setAlpha:1];
which have different arguments . stars is an array of UIImageView's.
Any suggestion is welcome .
Actually you are firing all the timers exactly after 2 seconds. Change the timeInterval for different timers.
Instead you can use a single NSTimer that will fire repeatedly.
Declare NSUInteger count; in .h file.
Start a NSTimer as follows:
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(showStar:) userInfo:nil repeats:YES];
And your showStar method should be as follows:
-(void)showStar:(NSTimer*)timer
{
if( count > 0 )
{
[stars[count-1] setAlpha:0];
}
[UIView beginAnimations:#"anim1" context:NULL];
[UIView setAnimationDuration:0.5];
[stars[count] setAlpha:1];
[UIView commitAnimations];
count++;
if( count == 4 )
{
[timer invalidate];
count = 0;
}
}
The following code has been added.
if( count > 0 )
{
[stars[count-1] setAlpha:0];
}
All your timers are using the same time delay, so that's what the all appear at once. Also, don't call fire on the timers, they fire automatically. Calling fire probably makes them fire right away.
Incidentally, you don't need to use timers to trigger the animations, you can just add this to your animations:
[UIView setAnimationDelay:x];
And use a different x for each view.
I would do something like this
- (void)firstAnimation{
[UIView animateWithDuration:2.0f animations:^{
//first animation
} completion:^(BOOL finished){
[self secondAnimation];
}];
}
probably with recursion and a flag for current animation would be cleaner though
In my opinion your implementation is wrong.
Better to place a UImageView on your UIView.
Next create an NSArray of images and load then onto the imageView.
arrAnimations = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"image1.png],.....nil];
then call
imageView.animationImages= arrAnimations;
imageView.animationDuration=2;
[imageView startAnimating];

Resources