簡體   English   中英

在表格部分旋轉UIButton

[英]Rotate UIButton on table section

我在一個名為extendButton的節的標題上有UIButton。 我想在按下時將其旋轉180度。

- (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會展開,但是當我再次點擊時,按鈕保持不變,不會旋轉。 我在這里想念什么?

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

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

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

}

您的答案中的代碼有效,但是有更好的方法。

使用函數CGAffineTransformRotate,該函數將旋轉添加到現有轉換中:

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

接下來是為旋轉設置動畫,而不是讓按鈕跳轉到新的旋轉。 為此,您可以使用UIView方法animateWithDuration

將按鈕旋轉180度的按鈕方法如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM