SanitLee SanitLee - 1 month ago 6
Objective-C Question

Rotate UIButton on table section

I have UIButton on header of a a section named extendButton. I want to rotate it 180 degree when it's pressed.

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

}


And my rotateButton method

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


When I tap the button it now can rotate 180 degree and tableView for that section expanded but when I tap again the button stays the same no rotate. What am I missing here?

Answer

The code in your answer works, but there is a better way.

Use the function CGAffineTransformRotate, which adds rotation to an existing transform:

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

Next is animating the rotation rather than having the button jump to its new rotation. To do that you can use the UIView method animateWithDuration.

A button method to rotate the button by 180 degrees would look something like this:

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