2017-05-27 24 views
0

我的代码如下: -如何在UISearchbar处于活动状态时进行放大和缩小?

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar 
{ 
    self.navigationController.navigationBar.hidden = TRUE; 
    _r = self.view.frame; 
    _r.origin.y = -44; 
    _r.size.height += 44; 
    self.view.frame = _r; 

    [searchBar setShowsCancelButton:YES animated:YES]; 

    return YES; 
} 

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar 
{ 

    NSLog(@"cancel button pressed"); 
    //This'll Hide The cancelButton with Animation 
    [searchBar setShowsCancelButton:NO animated:YES]; 
    _r.origin.y = 44; 
    _r.size.height -= 44; 
    self.view.frame = _r; 
    [searchBar resignFirstResponder]; 
    //remaining Code'll go here 
} 

上面的代码并没有像预期的那样。它破坏了视图,而不是放大和缩小。当按下“取消”按钮时,它变成下图所示。我如何在iPhone的“设置”中拥有完美的放大和缩小效果,例如Apple的搜索栏。 Spoiled view

回答

0

您需要使用UISearchBar对象中的CGAffineTransform

CGFloat zoomIn = 1, zoomOut = .1; 

CGAffineTransform tfIn = CGAffineTransformMakeScale(zoomIn, zoomIn); 
CGAffineTransform tfOut = CGAffineTransformMakeScale(zoomOut, zoomOut); 

[UIView animateWithDuration: .2 delay: 0 options: 0 animations: ^{ 
    self.transform = tfIn; 

} completion: ^(BOOL finished) { 
    // Nothing 
}]; 

// Action from button to zoom out 
self.transform = tfOut; 
相关问题