How to change alpha of UILabel by clicking?

I have an iOS (4.0) application that I would like to change the alpha of a specific UILabel by simply clicking anywhere on the screen. I don’t have my interface done programmatically, I just used the interface builder to put labels on things on the screen.

How can I use a tap gesture to change the alpha of a specific UILabel in my program?

Thanks in advance!

+5
source share
2 answers

In viewDidLoad:

UITapGestureRecognizer *oneTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [oneTap setNumberOfTapsRequired:1];
    [oneTap setNumberOfTouchesRequired:1];
    [self.view oneTap];

Then define the method:

-(void)tapAction:(UIGestureRecognizer *)gesture 
    {
          [self.yourLabel setAlpha:0.5f]; // set the alpha to whatever you want, or animate the fade, whatever
    }
+4
source
-(IBAction)myPressedButton:(id)sender { 
    float x;
    x = theLabel.alpha;
    foat a=0.1;
    self.theLabel.alpha = x-a;
}
-1
source

All Articles