繁体   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