UITableViewCell中的UIButton在UITableView宽度动画时跳转

时间:2016-11-29 11:36:52

标签: ios uitableview xamarin xamarin.ios

执行动画以减小桌面视图宽度时,“废纸篓”按钮会在动画实际开始之前先跳到左侧。 动画在ViewController中执行。

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableViewRefresh.dataSource = self;
    self.tableViewRefresh.delegate = self;
    // Do any additional setup after loading the view.
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

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

    static NSString *simpleTableIdentifier = @"cell1";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 1.0f;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

before

在: after

按钮有一个约束,因此从表格视图单元格的右侧(它是一个xib)始终是8像素。

我希望/期望的是按钮随着tableview宽度变化的动画及时滑过。我错误地认为自动布局可以解决这个问题吗?

我可以在桌面视图上调用UIView.Animate(0.05d, 0.15d, UIViewAnimationOptions.CurveEaseInOut, () => { OrderProductsTableView.Frame = new CGRect(LeftPanel.Frame.Width + 130f, OrderProductsTableView.Frame.Location.Y, View.Frame.Size.Width - LeftAddProductPanel.Frame.Width, OrderProductsTableView.Frame.Size.Height); RightPanelFooter.Alpha = 0; }, null); 并在reloadData()中手动重新定位按钮,但希望有一种更优雅的方式。

2 个答案:

答案 0 :(得分:1)

您还需要为按钮设置动画。您可以使用以下命令获取对单元格的引用:

List<UITableViewCell> cells = OrderProductsTableView.VisibleCells.ToList();

然后遍历单元格并修改x位置,例如:

UIView.Animate(0.05d, 0.15d, UIViewAnimationOptions.CurveEaseInOut, () =>
{
    foreach (UITableViewCell cell in cells)
    {
          MyCellType mCell = cell as MyCellType;
          mCell.trashButton.Frame = new CGRect(newXValue, newYValue, newWidthValue, newHeightValue);
    }

}, null);

然后还会像您已经完成的那样设置表视图宽度的动画。动画应该同时运行,并且您的按钮应该与表视图宽度的动画一起移动。

答案 1 :(得分:0)

这是一个自动布局问题,请确保创建2个水平间距约束,其中“更多或相等”&gt; =您想要的最小宽度,“小于或等于”&lt; =您想要的最大空间。< / p>

请不要犹豫,了解更多信息

相关问题