How to hide an image after one second - cocoa-touch

I want to hide an image after one second, as a flip animation ends. Please someone help me.

Assuming that you need to hide after loading from viewDidLoad, Declare following in your viewDidLoad;
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(hideYourImage) userInfo:nil repeats:NO];
-(void)hideYourImage
{
[yourImageView setHidden:TRUE];
}
Here you are. Hope it helps.

If you are doing flip animation using UIView animation, then you can specify code for animation completion.
[UIView animateWithDuration:0.2
animations:^{// Put your flip animation}
completion:^(BOOL finished){ [imageView removeFromSuperview]; }];
Else, the timer method will also do.

Related

CGRectMake Does not move image (Sometimes)

When click on the image in my app and the image is at the top, the image will always move down perfectly. But when the image is at the bottom sometimes the image does not move up. Even though it does not move the process is still run and it logs as if it was at the top. I tried everything I know how to do but do you see anything wrong? Thanks!!
- (void )imageTapped:(UITapGestureRecognizer *) gestureRecognizer
{
NSLog(#"imageTapped");
[UIView beginAnimations:#"move" context:nil];
[UIView setAnimationDuration:0.3];
if (self.imgV.frame.origin.y == 20) {
NSLog(#"Top");
upOrDown = #"1";
self.invalidateTheTimer = #"";
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateLabel) userInfo:nil repeats:YES];
self.imgV.frame=CGRectMake(0, self.view.frame.size.height-self.imgV.frame.size.height, self.imgV.frame.size.width, self.imgV.frame.size.height);
self.vBottom.frame=CGRectMake(0, self.imgV.frame.origin.y+self.imgV.frame.size.height, self.vBottom.frame.size.width, self.vBottom.frame.size.height);
} else if (self.imgV.frame.origin.y >= 507) {
NSLog(#"Bottom");
self.imgV.frame=CGRectMake(0, 20, self.imgV.frame.size.width, self.imgV.frame.size.height);
self.vBottom.frame=CGRectMake(0, self.imgV.frame.origin.y+self.imgV.frame.size.height, self.vBottom.frame.size.width, self.vBottom.frame.size.height);
[hour setText:#""];
[minute setText:#""];
[second setText:#""];
[timer invalidate];
self.invalidateTheTimer = #"INVALIDATE";
}
NSLog(#"%f", self.imgV.frame.origin.y);
[UIView commitAnimations];
}
Considering you haven't posted your entire code, it's hard to figure out what's wrong exactly, but looking at the mess you've made, I suggest you take a look into block based animations which should simplify what you're trying to accomplish.
Additionally, if I had to guess, I'd say the issue is in your timer and whatever method it's invoking is probably messing with your animations.

Changing the random image

With the help of a few threads here I created a slideshow that starts as soon as my view loads
However I am trying to make a slight modification where, I want the images to be random Not showing in the order in the array or in any particular order. This is my codde
-(void)viewDidAppear:(BOOL)animated
{
NSArray *myImages=[NSArray arrayWithObjects:
[UIImage imageNamed:#"Pitt Bull"],
[UIImage imageNamed:#"German Sherpard"],
[UIImage imageNamed:#"Pincer"],
nil];
int indexed=arc4random()%[myImages count];
UIImage *image=[myImages objectAtIndex:indexed];
NSArray *imageAr=[NSArray arrayWithObject:image];
[NSTimer scheduledTimerWithTimeInterval:12.0
target:self
selector:#selector(viewDidAppear:)
userInfo:Nil
repeats:YES];
[self.kenView animageWithImages:imageAr
transitionDuration:12
loop:YES
isLandscape:YES];
}
However it only works once. it loads a random picture at the start, but after that it keeps loading the same image. I can see that is because indexed is only assigned an integer when the view "did appear" . I was wondering if there was another way too select a new random number since I am not using a button/action as I saw many of other members doing. OR a better way of accomplishing this.
Thank you
You may want to try this function, + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats from NSTimer.
For example, in ViewDidLoad
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(showImage:) userInfo:nil repeats:YES];
and declare a function
- (void)showImage:(NSTimer*)timer
{
// your code to generate random index and animate image transition
}
if for some reason, you want to stop the timer
[timer invalidate];
Update
To avoid the timer messing up issue, trigger timer inside showImage, in ViewDidLoad, do [self showImage]
- (void)showImage
{
// your code to generate random index and animate image transition
// trigger the timer when the transition is finished
// You can leverage the method animateWithDuration:animations:completion:
// from UIView
[UIView animateWithDuration:12
animations:^(){
// image transition
}
completion:^(BOOL finished){
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(showImage) userInfo:nil repeats:NO];
}]
}
This should provide you the idea.
Consider using this method to randomly shuffle the elements of your array at startup.

Detect collisions with UIAnimation

Im using this code to try to detect collisions between two images, one of which is in an animation, but it doesn't work.
[UIView animateWithDuration:5 animations:^{
bird.center = CGPointMake(bird.center.x, 600);
fallTimer = [NSTimer scheduledTimerWithTimeInterval:.001 target:self selector:#selector(check) userInfo:nil repeats:YES];
}];
-(void)check {
if (CGRectIntersectsRect(bird.frame, cat.frame)) {
NSLog(#"YES");
}
}
How can i detect the collision?
You can't use the frame of a view while it's being animated, the returned value will not be accurate. Instead, you should be able to get the presentationLayer from the views layer and check its frame.

How to flash a button on UI thread?

I have a button on my UI that I would like to flash (turn on and then off again) every 800ms, once the button has been pressed. I do that with the following code:
- (void)flickEmergencyButton {
// Check whether an emergency is in progress...
if (model.emergencyInProgress) {
// ...and if so, flick the state
self.emergencyButton.selected = !self.emergencyButton.selected;
// Make this method be called again in 800ms
[self performSelector:#selector(flickEmergencyButton) withObject:nil afterDelay:0.8];
} else {
// ...otherwise, turn the button off
self.emergencyButton.selected = NO;
}
}
...which works really well, except: There is a UIScrollView on the UI as well and while the user has his finger down on it and is scrolling around, the button freezes. While I completely understand why that is, I am not sure what to do about it.
The performSelector:withObject:afterDelay message schedules the message to be send on the current thread, which is the main thread, ie. the UI tread and hence does not get to process the message until all other UI activity has come to an end. Correct? But I need to do this on the UI thread as I cannot select/un-select the button on any other thread, right? So what is the solution here?
I would recommend using Core Animation. Try something like this:
-(void) flash{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3f];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
if( emergency ){
// Start flashing
[UIView setAnimationRepeatCount:1000];
[UIView setAnimationRepeatAutoreverses:YES];
[btn setAlpha:0.0f];
}else{
// Stop flashing
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationRepeatCount:1];
[btn setAlpha:1.0f];
}
emergency = !emergency;
[UIView commitAnimations];
}
Where btn is declared as
#property(nonatomic, retain) IBOutlet UIButton *btn;
and emergency is a simple BOOL variable.
Call flash to start and to stop the animation.
In this example we animate the alpha attribute for simplicity, but you can do the same with the button backcolor, as Sam said in his answer, or whatever attribute you like.
Hope it helps.
UPDATE:
Regarding making the transition between two images, try calling imageFlash instead of flash:
-(void) imageFlash{
CABasicAnimation *imageAnimation = [CABasicAnimation animationWithKeyPath:#"contents"];
[btn setImage:normalState forState:UIControlStateNormal];
if( emergency ){
imageAnimation.duration = 0.5f;
imageAnimation.repeatCount = 1000;
}else{
imageAnimation.repeatCount = 1;
}
imageAnimation.fromValue = (id)normalState.CGImage;
imageAnimation.toValue = (id)emergencyState.CGImage;
[btn.imageView.layer addAnimation:imageAnimation forKey:#"animateContents"];
[btn setImage:normalState forState:UIControlStateNormal]; // Depending on what image you want after the animation.
emergency = !emergency;
}
Where normalState and emergencyState are the images you want to use:
Declared as:
UIImage *normalState;
UIImage *emergencyState;
Assigning the images:
normalState = [UIImage imageNamed:#"normal.png"];
emergencyState = [UIImage imageNamed:#"alert.png"];
Good luck!
While this feels like a job for CoreAnimation (perhaps animating the backgroundColor of a custom UIControl) you could accomplish this with an NSTimer running in the appropriate run loop mode.
e.x.
NSTimer *timer = [NSTimer timerWithTimeInterval:0.8f target:self selector:#selector(flicker:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
and when you want to stop animation:
[timer invalidate], timer = nil;
button.selected = NO;
By adding the timer to all NSRunLoopCommonModes, you not only add it to the default run loop mode but the mode it is in while user interaction is being continuously processed (UITrackingRunLoopMode.)
Apple's docs give a more thorough explanation of run loop modes and run loops in general.

how to show a loading bar or progress bar in iPhone application whiele fetching data

I have an iPad application when view is loaded, i am calling method after some time duration so i want to display a loading bar till method is called and data is loaded.
I am Using NSTimer to call a method in viewDidLoad
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(loadNews) userInfo:nil repeats:NO];
Use Activity Indicator to show progress bar or loading view.... like below.... in viewdidload..
CGRect frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
self.activityIndicator = [[UIActivityIndicatorView alloc]
initWithFrame:frame];
[self.activityIndicator sizeToFit];
self.activityIndicator.autoresizingMask =
(UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
[activityIndicator startAnimating];
UIBarButtonItem *loadingView = [[UIBarButtonItem alloc]
initWithCustomView:self.activityIndicator];
loadingView.target = self;
self.navigationItem.rightBarButtonItem = loadingView;
You can use a UIProgressView to display loading progress. The Mail app uses this when downloading messages. For indefinite time periods, use a UIActivityIndicatorView instead.
It's discussed in the iOS HIG.
Rather Do
[NSThread detachNewThreadSelector:#selector(loadNews) toTarget:self withObject:nil];
Then stop the loadingview when you are finished using something like
[activityIndicator setHidden:YES];
Hope this helps you.
I like to use MBProgressHUD. It lets you easily customize your loading progression, with different states. Like initialization, downloading, cleaning...