Tuesday, October 28, 2014

Toggle animation of view (up/down) on button click

In the below code snippet SearchBar toggles up and down with animation effect

[self animateView:DOWN];

////////////////////////////////////////////////////////
// Method : animateView
// Params : direction
// Return : animate the searchview appearance
////////////////////////////////////////////////////////

- (void)animateView:(NSString *)direction {
    
    [self.view bringSubviewToFront:patientSearchbar];
    
    if ([direction isEqualToString:DOWN]) {
        
        CGRect initialFrame = CGRectMake(0, 0 - searchView.frame.size.height  
        + 44, searchView.frame.size.width, searchView.frame.size.height);
        searchView.frame = initialFrame;
        
        searchView.layer.borderColor = VIOLET_BORDER_COLOR;
        searchView.layer.masksToBounds = NO;
        searchView.layer.shadowColor = [[UIColor blackColor] CGColor];
        searchView.layer.shadowOpacity = 1.0;
        searchView.layer.shadowRadius = 10.0;
        searchView.layer.shadowOffset = CGSizeMake(0, 3);
        
        [UIView animateWithDuration:0.5 delay:0.0 
        options:UIViewAnimationOptionCurveEaseOut animations:^{  
            
            CGRect viewRect =  CGRectMake(initialFrame.origin.x
        initialFrame.origin.y + initialFrame.size.height
        initialFrame.size.width, initialFrame.size.height);
           
            searchView.frame = viewRect;
            
        } completion:^(BOOL finished) {
             // Perform completion task here
        }];
        
    } else if ([direction isEqualToString:UP]){
        
        [UIView animateWithDuration:0.5 delay:0.0 
         options:UIViewAnimationOptionCurveEaseOut animations:^{  
       
              CGRect originalFrame = CGRectMake(0, 0
         searchView.frame.size.height + 44, searchView.frame.size.width
         searchView.frame.size.height);
        
         searchView.frame = originalFrame;
            
        } completion:^(BOOL finished) {

            [searchView removeFromSuperview];
        }];
    }

}

No comments:

Post a Comment