2016-11-04 31 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); 
    } 

}