Nils
Nils

Reputation: 1507

Hide/Show Button and UIImageVIew if Tap on a ScrollView

How can I hide/show a UIButton and ImageView by Tap on a Scrollview?

EDIT:

I use this for only Button and Tap on the View:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
   UITouch *touch = [touches anyObject];
   CGPoint loc = [touch locationInView:[touch view]];
   if (!CGRectContainsPoint(btn1.frame, loc) || (!CGRectContainsPoint(btn2.frame, loc)
   {
      btn1.hidden = !btn1.hidden;
      btn2.hidden = !btn2.hidden;
   }
}

Upvotes: 3

Views: 1227

Answers (1)

iPatel
iPatel

Reputation: 47099

UITapGestureRecognizer *scrlTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrlTapREcotTap:)];
    [scrlTap setNumberOfTapsRequired:1];
    [self.ScrollView addGestureRecognizer:scrlTap];

Take BOOL isTappFirstTime; in .h file and in viewDidLoad method write isTappFirstTime = YES;

Write following gesture method;

- (void)scrlTapREcotTap:(UITapGestureRecognizer *)gestureRecognizer
{
  if(isTappFirstTime)
  { 
       //put code of hide 

     [UIView animateWithDuration:1.0 animations:^{
           button.alpha = 0;
           imgView.alpha = 0; 
           } completion: ^(BOOL finished) {
             button.hidden = YES;
             imgView.hidden = YES;
      }];        
     isTappFirstTime = NO;
  }
  else 
  {
     // put code of show
     [UIView animateWithDuration:1.0 animations:^{
       button.alpha = 1;
       imgView.alpha = 1; 
       } completion: ^(BOOL finished) {
         button.hidden = NO;
         imgView.hidden = NO;
  }]; 
     isTappFirstTime = YES;
  }
}

Upvotes: 3

Related Questions