Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Clear out the predictionLabel text when a user double-taps on the screen. You can achieve this by checking the tapCount

Struggling on the second half of the Animating and Intercepting Events Extra credit section. How do I tell my label to disappear when the user a double taps the screen.

// // THViewController.m // CrystalBall // // Created by Amit Bijlani on 8/27/13. // Copyright (c) 2013 Treehouse. All rights reserved. //

import "THViewController.h"

import "THCrystalBall.h"

import <AudioToolbox/AudioToolbox.h>

@interface THViewController ()

@end

@implementation THViewController { SystemSoundID soundEffect;

}

  • (void)viewDidLoad { [super viewDidLoad];

    NSString *soundPath = [[NSBundle mainBundle]pathForResource:@"MusicStingSciFiOr SDT045201" ofType:@"wav"]; NSURL *soundURL = [NSURL fileURLWithPath:soundPath]; AudioServicesCreateSystemSoundID(CFBridgingRetain(soundURL), &soundEffect);

    self.crystalBallPredictions = [[THCrystalBall alloc]init];

    self.crystalBallColours = [[THCrystalBall alloc]init];

    self.crystalBallFonts = [[THCrystalBall alloc]init];

    //NSLog (@"Font families: %@", [UIFont familyNames]);

    self.backgroundImageView.animationImages = [[NSArray alloc]initWithObjects:[UIImage imageNamed:@"CB00001"], [UIImage imageNamed:@"CB00002"], [UIImage imageNamed:@"CB00003"], [UIImage imageNamed:@"CB00004"], [UIImage imageNamed:@"CB00005"], [UIImage imageNamed:@"CB00006"], [UIImage imageNamed:@"CB00007"], [UIImage imageNamed:@"CB00008"], [UIImage imageNamed:@"CB00009"], [UIImage imageNamed:@"CB00010"], [UIImage imageNamed:@"CB00011"], [UIImage imageNamed:@"CB00012"], [UIImage imageNamed:@"CB00013"], [UIImage imageNamed:@"CB00014"], [UIImage imageNamed:@"CB00015"], [UIImage imageNamed:@"CB00016"], [UIImage imageNamed:@"CB00017"], [UIImage imageNamed:@"CB00018"], [UIImage imageNamed:@"CB00019"], [UIImage imageNamed:@"CB00020"], [UIImage imageNamed:@"CB00021"], [UIImage imageNamed:@"CB00022"], [UIImage imageNamed:@"CB00023"], [UIImage imageNamed:@"CB00024"], [UIImage imageNamed:@"CB00025"], [UIImage imageNamed:@"CB00026"], [UIImage imageNamed:@"CB00027"], [UIImage imageNamed:@"CB00028"], [UIImage imageNamed:@"CB00029"], [UIImage imageNamed:@"CB00030"], [UIImage imageNamed:@"CB00031"], [UIImage imageNamed:@"CB00032"], [UIImage imageNamed:@"CB00033"], [UIImage imageNamed:@"CB00034"], [UIImage imageNamed:@"CB00035"], [UIImage imageNamed:@"CB00036"], [UIImage imageNamed:@"CB00037"], [UIImage imageNamed:@"CB00038"], [UIImage imageNamed:@"CB00039"], [UIImage imageNamed:@"CB00040"], [UIImage imageNamed:@"CB00041"], [UIImage imageNamed:@"CB00042"], [UIImage imageNamed:@"CB00043"], [UIImage imageNamed:@"CB00044"], [UIImage imageNamed:@"CB00045"], [UIImage imageNamed:@"CB00046"], [UIImage imageNamed:@"CB00047"], [UIImage imageNamed:@"CB00048"], [UIImage imageNamed:@"CB00049"], [UIImage imageNamed:@"CB00050"], [UIImage imageNamed:@"CB00051"], [UIImage imageNamed:@"CB00052"], [UIImage imageNamed:@"CB00053"], [UIImage imageNamed:@"CB00054"], [UIImage imageNamed:@"CB00055"], [UIImage imageNamed:@"CB00056"], [UIImage imageNamed:@"CB00057"], [UIImage imageNamed:@"CB00058"], [UIImage imageNamed:@"CB00059"], [UIImage imageNamed:@"CB00060"],nil];

    self.backgroundImageView.animationDuration = 3.5f; self.backgroundImageView.animationRepeatCount = 1;

}

  • (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }

pragma mark - Prediction

  • (void) makePrediction { self.predictionLabel.text = [self.crystalBallPredictions randomPrediction]; self.predictionLabel.textColor = [self.crystalBallColours randomColour]; self.predictionLabel.font = [self.crystalBallFonts randomFont]; [self.backgroundImageView startAnimating];

    [UIView animateWithDuration:2.0f animations:^{ self.predictionLabel.frame = CGRectMake(50 , 190, self.predictionLabel.frame.size.width, self.predictionLabel.frame.size.height); }];

    AudioServicesPlayAlertSound(soundEffect);

    [UIView animateWithDuration:7.0f animations:^{ self.predictionLabel.alpha = 1.0f; }]; }

  • (void) predictionNil { self.predictionLabel.text = nil; self.predictionLabel.textColor = nil; self.predictionLabel.font = nil; self.predictionLabel.alpha = 0.0f;

    [UIView animateWithDuration:3.0f animations:^{ self.predictionLabel.frame = CGRectMake(50 , 2000, self.predictionLabel.frame.size.width, self.predictionLabel.frame.size.height); }]; [UIView animateWithDuration:3.0f animations:^{ self.predictionLabel.frame = CGRectMake(50 , 1000, self.predictionLabel.frame.size.width, self.predictionLabel.frame.size.height); }]; }

pragma mark - Motion Events

  • (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { [self predictionNil]; }

  • (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if (motion == UIEventSubtypeMotionShake) { [self makePrediction]; } }

  • (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event { NSLog(@"motion canclled"); }

pragma mark - Touch Events

  • (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [self predictionNil]; }

  • (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self makePrediction]; }

pragma mark - Button Events

  • (IBAction)buttonPressed { [self predictionNil]; [self makePrediction]; }

@end

1 Answer

From Event Handling Guide for iOS:

If your custom responder is a subclass of UIView or UIViewController, you should implement all of the event handling methods.

Hence, you need to implement ALL of the following events:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

Also check out UIGestureRecognizer which enables you to listen for taps like so e.g. in your viewDidLoad:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(someMethod)];
tapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapGesture];

Best /Rasmus