2017-05-27 65 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; 
相關問題