以编程方式更改动画的UISwitch状态

zka*_*aje 5 iphone animation objective-c uikit uiswitch

我有一个UITableView,其中包含一些包含UISwitches的单元格:

UISwitch* actSwitch = (UISwitch*)[cell viewWithTag: SWITCH_TAG];
[actSwitch addTarget: self
              action: @selector(actSwitchChanged:) 
    forControlEvents: UIControlEventValueChanged];
BOOL value = [appSettings.lock_when_inactive boolValue];
[actSwitch setOn: value animated:NO];
Run Code Online (Sandbox Code Playgroud)

我还想覆盖didSelectRowAtIndexPath:方法来执行相应UISwitch的切换:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *cellType = [self typeOfCellForIndexPath:indexPath];
    if ( cellType  == @"CellWithSwitch" )
    {
        UISwitch* actSwitch = (UISwitch*)[[[self tableView] cellForRowAtIndexPath:indexPath ] viewWithTag: SWITCH_TAG];
        BOOL value = [actSwitch isOn];
        [actSwitch setOn:!value animated:YES]; // <-- HERE IS THE PROBLEM
        [self actSwitchChanged: actSwitch];
    }
    else if ( cellType == @"CellWithoutSwitch" )
    {
        // other actions
    }
}
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,无论是直接点击UISwitch还是单击单元格,它都会更改状态并actSwitchChanged:正确调用.

但是如果我点击单元格,我的UISwitch没有动画从一个状态到另一个状态的翻转,它只是在一个瞬间改变它的状态.

那么[actSwitch setOn:!value animated:YES]它是不是足以告诉动画动画?


以下是我设置和调用配置单元格的方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = nil;
    NSString *EnabledCellIdentifier = [self typeOfCellForIndexPath:indexPath];  
    cell = [tableView dequeueReusableCellWithIdentifier:EnabledCellIdentifier];

    if (cell == nil) 
    {
        cell = [self cellForType:EnabledCellIdentifier];
    }
    [self cofigureCell:cell forIndexPath:indexPath];

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

这是我配置单元格的方式:

- (void)cofigureCell:(UITableViewCell*)cell forIndexPath:(NSIndexPath*)indexPath {
    switch ( indexPath.section )
    {
        case 0: 
            // not this
        case 1: 
        {
            switch ( indexPath.row )
            {
                case 0:
                {
                    cell.textLabel.text = @"Limit passwords attempts";
                    UISwitch* actSwitch = (UISwitch*)[cell viewWithTag: SWITCH_TAG];
                    [actSwitch addTarget: self
                                  action: @selector(actSwitchChanged:) 
                        forControlEvents: UIControlEventValueChanged];
                    BOOL value = [appSettings.limit_password_attempts boolValue];
                    [actSwitch setOn: value animated:NO];

                    break;
                }
                //other rows of this section here are being configured
            }
            break;
        }

        case 2: 
        {
            switch ( indexPath.row )
            {
                case 0:
                {
                    cell.textLabel.text = @"Lock when inactive";
                    UISwitch* actSwitch = (UISwitch*)[cell viewWithTag: SWITCH_TAG];
                    [actSwitch addTarget: self
                                  action: @selector(actSwitchChanged:) 
                        forControlEvents: UIControlEventValueChanged];
                    BOOL value = [appSettings.lock_when_inactive boolValue];
                    [actSwitch setOn: value animated:NO];

                    break;
                }
                //other rows of this section here are being configured
            }
            break;
        }
        default:

            break;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我逐步调试,并完成此步骤:

[actSwitch setOn:!value animated:YES]; // <-- HERE IS THE PROBLEM

actSwitch仅在[self actSwitchChanged: actSwitch];更改数据模型和调用后才更改其状态[self.tableView reloadData];

可能是因为我有两个带有UISwitch的单元格,并且它们之间存在一些冲突?可能有更好的方法从单元格获取UISwitch然后我的代码?:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *cellType = [self typeOfCellForIndexPath:indexPath];
    if ( cellType  == @"CellWithSwitch" )
    {
        UISwitch* actSwitch = (UISwitch*)[[[self tableView] cellForRowAtIndexPath:indexPath ] viewWithTag: SWITCH_TAG];
        BOOL value = [actSwitch isOn];
        [actSwitch setOn:!value animated:YES]; // <-- HERE IS THE PROBLEM
        [self actSwitchChanged: actSwitch];
    }
    else if ( cellType == @"CellWithoutSwitch" )
    {
        // other actions
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

只需延迟调用actSwitchChanged函数:

UISwitch* actSwitch = (UISwitch*)[[[self tableView] cellForRowAtIndexPath:indexPath ]  viewWithTag: SWITCH_TAG];
    BOOL value = [actSwitch isOn];
    [actSwitch setOn:!value animated:YES]; // <-- HERE IS THE PROBLEM
[self performSelector:@selector(actSwitchChanged:) withObject:actSwitch afterDelay:0.55];
Run Code Online (Sandbox Code Playgroud)