繁体   English   中英

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

[英]Change the state of UISwitch with animation programmatically

我有一个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];

而且我还想覆盖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
    }
}

在这两种情况下,我都直接单击UISwitch或单击单元格,它会更改其状态并正确调用actSwitchChanged:

但是,如果我单击该单元格,我的UISwitch不会为从一种状态切换到另一种状态设置动画,它只是在一瞬间更改其状态。

因此[actSwitch setOn:!value animated:YES]不足以告诉perfom动画吗?


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

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

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

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

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

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

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

可能是因为我有两个带有UISwitches的单元,并且它们之间存在一些冲突? 可能有比我的代码更好的方法来从单元中获取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
    }
}

只需稍微延迟调用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];

SWIFT 4

使用委托协议创建一个UITableViewCell子类。 我添加了标题标签和开关,如下所示:

protocol CellWithSwitchDelegate: class {
    func didToggleSwitch(on: Bool)
}

class CellWithSwitchTableViewCell: UITableViewCell {

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var toggleSwitch: UISwitch!
    weak var delegate: CellWithSwitchDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    @IBAction func switchWasToggled(_ sender: UISwitch) {
        delegate?.didToggleSwitch(on: sender.isOn)
    }    

}

在您的UITableViewController中,添加委托方法,如下所示:

class UITableViewController: CellWithSwitchDelegate {

    func didToggleSwitch(on: Bool) {
        // This is the delegate function that's called when the switch is toggled
        print("toggled switch")
    }
}

在您的UITableViewController中,将以下内容添加到didSelectRowAtIndexPath中:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath) as! CellWithSwitchTableViewCell
    let settingsSwitch = cell.toggleSwitch!
    didToggleSwitch(on: !settingsSwitch.isOn)
    settingsSwitch.setOn(!settingsSwitch.isOn, animated: true)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM