2016-11-04 34 views
0

我在名爲extendButton的節標題上有UIButton。按下時我想旋轉180度。旋轉UIButton表部分

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    if (section == 3) { 
     OrderHistoryHeaderView *orderHeaderView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"OrderHistoryHeaderView"]; 
     if (orderHeaderView == nil) { 
      orderHeaderView = [[[NSBundle mainBundle] loadNibNamed:@"OrderHistoryHeaderView" owner:self options:nil] objectAtIndex:0]; 
      orderHeaderView.contentView.backgroundColor = [UIColor whiteColor]; 
      [orderHeaderView.extendButton addTarget:self action:@selector(extendButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 
     } 
     return orderHeaderView; 
    } 

    ProductDetailHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"ProductDetailHeaderView"]; 
    if (headerView == nil) { 
     headerView = [[[NSBundle mainBundle] loadNibNamed:@"ProductDetailHeaderView" owner:self options:nil] objectAtIndex:0]; 
     headerView.contentView.backgroundColor = [UIColor whiteColor]; 
    } 

    headerView.mainLabel.text = headerTitleArray[section]; 
    return headerView; 
} 


    - (void)extendButtonPressed { 
    if (isExtend) { 

    //call rotate method 
    [self rotateButton:sender]; 

    //insert row 
    unsigned long rowCount = orderHistoryArray.count; 

    //show msg if no history data 
    if (rowCount == 0) { 
     [ShareFunction showSimpleAlertWithString:@"No order history data!!" inVC:self]; 
    } 

    orderHistoryArray = nil; 
    NSMutableArray *indexPathSet = [[NSMutableArray alloc] init]; 
    for (int i = 0 ; i < rowCount; i++) { 
     [indexPathSet addObject:[NSIndexPath indexPathForRow:i inSection:3]]; 
    } 
    [_mainTableView beginUpdates]; 
    [_mainTableView deleteRowsAtIndexPaths:indexPathSet withRowAnimation:UITableViewRowAnimationAutomatic]; 
    [_mainTableView endUpdates]; 

} else { 
    //delete row 

    //call rotate method 
    [self rotateButton:sender]; 

    orderHistoryArray = productHistoryArray; 

    //show msg if no history data 
    //if (orderHistoryArray.count == 0) { 
    // [ShareFunction showSimpleAlertWithString:@"No order history data!!" inVC:self]; 
    //} 


    NSMutableArray *indexPathSet = [[NSMutableArray alloc] init]; 
    for (int i = 0 ; i < orderHistoryArray.count; i++) { 
     [indexPathSet addObject:[NSIndexPath indexPathForRow:i inSection:3]]; 
    } 
    [_mainTableView beginUpdates]; 
    [_mainTableView insertRowsAtIndexPaths:indexPathSet withRowAnimation:UITableViewRowAnimationAutomatic]; 
    [_mainTableView endUpdates]; 
} 

    } 

而且我rotateButton方法

- (void)rotateButton:(UIButton*)button { 
    button.transform = CGAffineTransformMakeRotation(M_PI); 
} 

當我點擊按鈕,現在可以旋轉180度的tableView該節展開,但是當我再次點擊該按鈕保持不變,沒有旋轉。我在這裏錯過了什麼?

+0

要動畫一個180度旋轉? –

+0

@DuncanC是的先生 – SanitLee

+0

看看你是否可以自己解決這個問題。看看'UIView'方法'animateWithDuration:動畫:'。你想爲按鈕的transform屬性設置動畫效果。也看看函數'CGAffineTransformRotate()'。該函數進行轉換並對其應用旋轉。 (提示,M_PI是一個180度旋轉) –

回答

0

的代碼在你的答案的作品,但有一個更好的辦法。

使用功能CGAffineTransformRotate,增加了旋轉到現有的變換:

button.transform = CGAffineTransformRotate(button.transform, M_PI) 

接下來是動畫旋轉,而不是按鈕跳轉到其新的旋轉。要做到這一點,您可以使用UIView方法animateWithDuration

A鍵的方法來旋轉180度的按鈕會是這個樣子:

- (IBAction)buttonAction:(UIButton *)sender { 
    CGAffineTransform transform = sender.transform; 
    transform = CGAffineTransformRotate(sender.transform, M_PI); 
    [UIView animateWithDuration: 0.5 animations: ^{ 
    sender.transform = transform; 
    } 
    ]; 
} 
0

在這裏,我找到了一個解決方案通過傳遞參數isExtend

- (void)rotateButton:(UIButton*)button isExtend:(BOOL)isExtended{ 

    if (isExtended) { 
     button.transform = CGAffineTransformMakeRotation(0); 
    } else { 
     button.transform = CGAffineTransformMakeRotation(M_PI); 
    } 

}