在表格部分旋转UIButton

时间:2016-11-04 23:30:01

标签: ios objective-c uitableview

我在名为extendButton的部分的标题上有UIButton。我想按下它时旋转180度。

class Book {
  String title
  static hasMany = [authors: Author]
}

class Author {
  String Name
  Book book  // this will be the belongTo relationship that you need

  static constraints = {
      book nullable:true
  }
}

我的rotateButton方法

- (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];
}

    }

当我点击按钮时,它现在可以旋转180度,并且扩展了该部分的tableView,但是当我再次点击时,按钮保持不变,不旋转。我在这里缺少什么?

2 个答案:

答案 0 :(得分:0)

这里我通过传递参数isExtend

找到了解决方案
- (void)rotateButton:(UIButton*)button isExtend:(BOOL)isExtended{

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

}

答案 1 :(得分:0)

你的答案中的代码有效,但还有更好的方法。

使用函数CGAffineTransformRotate,它将旋转添加到现有变换:

function array_flatten($array) {
if (!is_array($array)) {
    return FALSE;
}
$result = array();
$i=0;
foreach ($array as $item) {
    if (is_array($item)) {
        $result = array_merge($result, array_flatten($item));
    }
    else if(is_string($item)) {
        array_push($result,$item);
    }
}
return $result;
}


function getLongest($array){
$arr_nivo_1 =array_flatten($array);

$longest="";
$maxLen=0;
foreach ($arr_nivo_1 as $item){
    if(strlen($item)>$maxLen){
        $maxLen=strlen($item);
        $longest=$item;
    }
}
return $longest;


}

接下来是动画旋转,而不是让按钮跳转到新的旋转。为此,您可以使用button.transform = CGAffineTransformRotate(button.transform, M_PI) 方法UIView

将按钮旋转180度的按钮方法如下所示:

animateWithDuration