UILabel to appear for 3 seconds after UIButton is pressed - objective-c

After I hit a pause button, I want a label to appear until the user taps on the screen. How do I do this?
So far I have this
- (IBAction)ButtonPausePressed:(id)sender {
PauseLabel.hidden = false (//how do i make it only visible until user taps?//)
if (GameEnd != true){
if ([GameUpdate isValid]){
[GameUpdate invalidate];
[BirdUpdate invalidate];
}else{
BirdUpdate = [NSTimer scheduledTimerWithTimeInterval:0.015
target:self
selector:#selector(UpdateBird)
userInfo:nil
repeats:YES];
GameUpdate = [NSTimer scheduledTimerWithTimeInterval:0.025
target:self
selector:#selector(GameUpdate)
userInfo:nil
repeats:YES];
}
}
}

- (IBAction)ButtonPausePressed:(id)sender {
PauseLabel.hidden = false
[self performSelector:#selector(hiddenLabel) withObject:nil afterDelay:3];
...
}
- (void)hiddenLabel{
PauseLabel.hidden = YES;
}

Try following
[your_view addSubview:your_label];
your_label.hidden = YES;
[your_label performSelector:#selector(setHidden:) withObject:#NO afterDelay:3];

Try like this:
-(void)viewDidLoad
{
UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(btnSingleClicked:)];
[singleTapGesture setDelegate:self];
singleTapGesture.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTapGesture];
}
-(void)btnSingleClicked:(UITapGestureRecognizer *)recognizer
{
if(recognizer.state == UIGestureRecognizerStateEnded && !self.PauseLabel.hidden)
{
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[UIView animateWithDuration:3.0f // 3 sec to hide it
delay:0.0f
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[self.PauseLabel setAlpha:0.0f];
}
completion:^(BOOL finished)
{
// remove from super view if needed.
// Now you can handle touch events etc
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}];
}
}
The idea behind the line is add a single tap gesture on your viewcontroller's view and if the pause label is displayed and a touch is happend on view check its state is ended and hide label with animation.

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
PauseLabel.hidden = YES;
});

Related

Animated dots in loading text

I want to do loading animation. When I tap the button text become "loading" then "loading." then "loading.." and from the beginning. How to do this?
I tried [UIView animateWithDuration] but it doesn't help.
It's not very difficult to achieve. Create a NSTimer property and then the start/stop/update methods like so:
- (void)startLoadingAnimation {
[self.myButton setTitle:#"Loading" forState:UIControlStateNormal];
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:#selector(updateLoadingLabel) userInfo:nil repeats:YES];
}
- (void)updateLoadingLabel {
NSString *ellipses = #"...";
if ([self.myButton.titleLabel.text rangeOfString:ellipses].location == NSNotFound) {
[self.myButton setTitle:[NSString stringWithFormat:#"%#.",self.myButton.titleLabel.text] forState:UIControlStateNormal];
} else {
[self.myButton setTitle:#"Loading" forState:UIControlStateNormal];
}
}
- (void)endLoadingAnimation {
[self.myTimer invalidate];
self.myTimer = nil;
[self.myButton setTitle:#"Loading" forState:UIControlStateNormal];
}
In this example the label is updated every 0.5 seconds.

How to elegantly transition images in slide show?

After some research on here I found a solution to creating a slide show of images in an iphone app. All working fine, currently the images show one after another.
My question is, can I make the images cross dissolve/fade rather than just appearing, and if so could I get some advice on that.
My Code
.m
}
int topIndex = 0, prevTopIndex = 1;
- (void)viewDidLoad
{
imagebottom = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,160,240)];
[self.view addSubview:imagebottom];
imagetop = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,160,240)];
[self.view addSubview:imagetop];
imageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"],
[UIImage imageNamed:#"ip2.png"],
nil];
NSTimer *timer = [NSTimer timerWithTimeInterval:5.0
target:self
selector:#selector(onTimer)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[timer fire];
[super viewDidLoad];
}
-(void)onTimer{
if(topIndex %2 == 0){
[UIView animateWithDuration:5.0 animations:^
{
imagebottom.alpha = 0.0;
}];
imagetop.image = [imageArray objectAtIndex:prevTopIndex];
imagetop.image = [imageArray objectAtIndex:topIndex];
}else{
[UIView animateWithDuration:5.0 animations:^
{
imagetop.alpha = 1.0;
}];
imagetop.image = [imageArray objectAtIndex:topIndex];
imagebottom.image = [imageArray objectAtIndex:prevTopIndex];
}
prevTopIndex = topIndex;
if(topIndex == [imageArray count]-1){
topIndex = 0;
}else{
topIndex++;
}
You have a bunch of options. If you have a "container" view, you can make a new UIImageView transparent (alpha = 0), then use a UIView animation block to fade one image in and the other out (or leave alpha = 1 on both and slide one in from any side you want.
For instance, you have your main view, self.view. You have one UIImageView *oldView, that is now at rect (0,0,320,100) and you want to slide it to the right as you slide a newView imageView in. First you set the newView frame to (-320,0,320,100) then [self.view addSubview newView]. To animate the change:
[UIView animateWithDuration:2 animations:^
{
oldView.frame = CGRectMake(320, 0, 320, 100);
newView.frame = CGRectMake(0,0,320, 100);
}
completion:^(BOOL finished)
{
[oldView removeFromSuperView];
} ];
You also have the option of using UiView's
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion
which gives you more/different options (and its less work too!). For instance, using the same basic objects in the first example, but the newView having the same frame as the oldView:
transitionFromView:oldView toView:newView duration:2 options: UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished)) { /*whatever*/}];

IOS - animation not responding

I want to create an animated menu.
first it's hidden outside screen
when user taps the screen, it will slide in
user can tap it and it do something
then after 3s it will slide outside screen
But problem is it not response when I tap it. Why?
example code: All code is in my UIView class.
showing = NO;
box = [[UIView alloc] initWithFrame:CGRectMake(500,-200,200,200)];
box.backgroundColor = [UIColor redColor];
UITapGestureRecognizer *tapBox = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapMenuHandler:)] autorelease];
[box addGestureRecognizer:tapBox];
[self addSubView:box];
and:
-(void) tapMenuHandler: (UIView *)obj {
//Do something
NSLog(#"tap box");
}
and:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (!showing) {
box.frame = CGRectMake(500, -200, 200, 200);
[UIView animateWithDuration:0.5f delay:0.3f options:(UIViewAnimationOptionAllowUserInteraction) animations:^{
box.frame = CGRectMake(500, 200, 200, 200);
} completion:^(BOOL finished) {
showing = YES;
[UIView animateWithDuration:0.5f delay:3.0f options:(UIViewAnimationOptionAllowUserInteraction) animations:^{
box.frame = CGRectMake(500, -200, 200,200);
} completion:^(BOOL finished) {
showing = NO;
}];
}];
}
}
Thank for your help and sorry for my English :)
Edit: more detail
I tried set start my box's origin in screen (ex. 500,600), It still always response when tap at start point (500,600) even though my box move to another position.
Update:
I changed my way to move box outside screen by use NSTimer then it's work!
[UIView animateWithDuration:0.5f delay:0.3f options:UIViewAnimationOptionAllowUserInteraction animations:^{
box.frame = CGRectMake(500,200,200,200);
} completion:^(BOOL finished) {
isShowMenubar = YES;
//use NSTimer instead of delay
[NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:#selector(hideBox) userInfo:nil repeats:NO];
}];
and
-(void)hideBox {
[UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationOptionAllowUserInteraction animations:^{
box.frame = CGRectMake(500,-200,200,200);} completion:^(BOOL finished) {
isShowMenubar = NO;
}];
}
The reason why the box is not receiving touches has to do with how animation works. Your box is actually offscreen, but visually, it's going through the animation.
Even though you set the delay as 3.0, it still moves the box beforehand (due to the animation method still being called). So in your current code, if you tapped on the box at (500, -200) then it will receive the touch.
To fix this, either use an NSTimer or Grand Central Dispatch to delay.
NSTimer:
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self action:#selector(dismissBox) userInfo:nil repeats:NO];
The problem with NSTimer is that you'll have to put that animation code in a separate method.
Grand Central Dispatch:
double delayInSeconds = 3.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[UIView animateWithDuration:0.5f animations:^{
box.frame = CGRectMake(box.frame.origin.x, -200, 200,200);
}completion:^(BOOL finished) {
showing = NO;
}];
});
I ran the code and didn't see the red view either. Then playing around for a bit, I got that your view is animating but is still out of bounds so you never see it. try changing the animation block to something like (the origin of the frame is at the left bottom corner)
box.frame = CGRectMake(100, 100, 200, 200);
and hopefully it will come animating.
Also, assuming that you run these in your view controller, the correct method should be
[self.view addSubview:box];
hope this helps

Display an image for 1 sec on the view

What I want to realize is:
When I touch a button, the image display on the view for 1 sec, then the image disappear.
I know that a NSTimer will help, but i dont know how to write the right code...need your help, thanks.
- (IBAction)bodytouched:(id)sender {
bodytouchedimage.hidden = NO;
bodytouchedimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"beated.jpg"]];
bodytouchedimage.userInteractionEnabled = YES;
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(showPictures:) userInfo:nil repeats:NO];
}
- (void)showPictures:(NSTimer *)timer {
bodytouchedimage.hidden = YES;
}
What you should to is call the showPictures function when you touch the button and then within the showPictures method you add a NSTimer that will call a hidePictures method 1 second later
- (void)showPictures
{
bodytouchedimage.hidden = NO;
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(hidePictures) userInfo:nil repeats:NO];
}
- (void)hidePictures
{
bodytouchedimage.hidden = YES;
}
Rather than using NSTimer, it would be easier to simply call your method to hide the image like this:
- (IBAction)bodytouched:(id)sender {
bodytouchedimage.hidden = NO;
bodytouchedimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"beated.jpg"]];
bodytouchedimage.userInteractionEnabled = YES;
[self performSelector:#selector(hidePicture) withObject:nil afterDelay:1];
}
- (void)hidePicture {
bodytouchedimage.hidden = YES;
}
performSelector:withObject:afterDelay: is a method of the NSObject class.

Attempting to "turn on/off" different UITapGestureReconginzer methods

I have this lovely nav bar asset:
navBar view http://dl.dropbox.com/u/1734050/ViewWithNavBar.jpg
and when users tap on the middle button (one with the arrows) it will bring up this other lovely "share box" asset
shareBox dropdown from navBar http://dl.dropbox.com/u/1734050/navBarWithShareBox.jpg
What I have in the code so far is that when the screen is tapped, the nav bar will appear, and disappear on another tap. Secondly, when the user taps on the "share" button, the share box comes up, and the user can tap outside of the box to dismiss it.
Here is the problem: I cannot dismiss the nav bar after bringing up the share box!
Here is some code:
-(void)viewDidLoad {
...
...
...
UITapGestureRecognizer *tapNavBar = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapForNavBar:)];
self.tapForNavBar = tapNavBar;
[self.viewForTaps addGestureRecognizer:self.tapForNavBar];
// btw, tapForNavBar is a UITapGestureRecognizer instance variable.
// also, viewForTaps is a view (instance variable) I made to handle
// where the user could tap (most of the screen)
}
#pragma mark -
#pragma mark Tap and Gesture Methods
-(void)handleTapForNavBar:(UIGestureRecognizer *)gestureRecognizer {
if (self.navBarIsHidden) {
[UIView animateWithDuration:0.5 animations:^ {
//self.primeViewController.view.alpha = 0.8;
self.navBar.alpha = 1.0;
}];
self.detailViewButton.userInteractionEnabled = YES;
self.shareButton.userInteractionEnabled = YES;
self.aboutAppButton.userInteractionEnabled = YES;
self.navBarIsHidden = NO;
}
else {
[UIView animateWithDuration:0.5 animations:^ {
self.navBar.alpha = 0.0;
//self.primeViewController.view.alpha = 1.0;
}];
self.detailViewButton.userInteractionEnabled = NO;
self.shareButton.userInteractionEnabled = NO;
self.aboutAppButton.userInteractionEnabled = NO;
self.navBarIsHidden = YES;
}
}
Ok, so that should look all dandy (and works like it too!) -- now here is perhaps where it gets unorthodox?
-(IBAction)showShareMenu:(id)sender {
if (self.navBarShareBoxIsHidden) {
[UIView animateWithDuration:0.5 animations:^ {
self.navBarShareBox.alpha = 1.0;
}];
[self.tapForNavBar removeTarget:nil action:NULL];
UITapGestureRecognizer *tapShareBox = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapForShareBox:)];
self.tapForShareBox = tapShareBox;
[self.viewForTaps addGestureRecognizer:self.tapForShareBox];
self.navBarShareBoxIsHidden = NO;
self.twitterButton.userInteractionEnabled = YES;
self.facebookButton.userInteractionEnabled = YES;
self.googlePlusButton.userInteractionEnabled = YES;
}
else {
[UIView animateWithDuration:0.5 animations:^ {
self.navBarShareBox.alpha = 0.0;
}];
[self.tapForShareBox removeTarget:nil action:NULL];
[self.tapForNavBar addTarget:self action:#selector(handleTapForNavBar:)];
self.navBarShareBoxIsHidden = YES;
self.twitterButton.userInteractionEnabled = NO;
self.facebookButton.userInteractionEnabled = NO;
self.googlePlusButton.userInteractionEnabled = NO;
}
}
I then create this method to handle the specific shareBox tapping:
-(void)handleTapForShareBox:(UIGestureRecognizer *)gestureRecognizer {
if (!self.navBarShareBoxIsHidden) {
[UIView animateWithDuration:0.5 animations:^ {
self.navBarShareBox.alpha = 0.0;
}];
self.navBarShareBoxIsHidden = YES;
self.twitterButton.userInteractionEnabled = NO;
self.facebookButton.userInteractionEnabled = NO;
self.googlePlusButton.userInteractionEnabled = NO;
[self.tapForShareBox removeTarget:nil action:NULL];
[self.tapForNavBar addTarget:self action:#selector(handleTapForNavBar:)];
}
}
I'm assuming my problem is coming from alloc/initing the new UITapGestureRecognizer in the -(IBAction)showShareMenu method. I thought by using the removeTarget... and addTarget messages I could easily tell my code which TapGesture method it should use, but considering it isn't working, I was wrong! Where did I go wrong? If you need more info, I'd be happy to give more.
Ah Ha! Looks like I found a solution! In my handleTapForShareBox method I did this:
-(void)handleTapForShareBox:(UIGestureRecognizer *)gestureRecognizer {
if (!self.navBarShareBoxIsHidden) {
[UIView animateWithDuration:0.5 animations:^ {
self.navBarShareBox.alpha = 0.0;
}];
self.navBarShareBoxIsHidden = YES;
self.twitterButton.userInteractionEnabled = NO;
self.facebookButton.userInteractionEnabled = NO;
self.googlePlusButton.userInteractionEnabled = NO;
[self.tapForShareBox removeTarget:nil action:NULL];
//[self.tapForNavBar addTarget:self action:#selector(handleTapForNavBar:)];
// Here is the new stuff, I added another alloc init of a UITapGestureRecognizer
// and re-assigned it to my UITap instance variable
UITapGestureRecognizer *tapNavBar = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapForNavBar:)];
self.tapForNavBar = tapNavBar;
[self.viewForTaps addGestureRecognizer:self.tapForNavBar];
}
}
It works great now! However, there were a couple things:
1) Do I have to release the UITapGestureRecognizer local pointer I created? Or because I initWithTarget it will be autoreleased?
2) Is this a hack? Or is this a proper way of passing different tap methods around? I'm just a little confused why the removeTarget and addTarget didn't do the trick.