【问题标题】:animateWithDuration can't animating UIButton's bounds after UIButton has set an image在 UIButton 设置图像后,animateWithDuration 无法为 UIButton 的边界设置动画
【发布时间】:2022-01-14 00:34:48
【问题描述】:

将图像设置为UIButton 称为bigIcon 后,我将其放入animateWithDuration 并更改其框架,然后运行代码,按钮大小立即更改(没有动画),但它从起点缓慢移动到终点(有动画),我该如何解决这个问题?我发现如果我确实为按钮设置了图像,这个问题就会消失。

代码如下:

- (void)bigImage:(MCGodCell *)godCell withImage:(UIImage *)image {
    self.bigIcon = [UIButton new];
    self.bigIcon.adjustsImageWhenHighlighted = NO;
    [self.bigIcon setBackgroundImage:image forState:UIControlStateNormal];

    CGFloat iconX = self.tableView.frame.size.width / 2.0;
    CGFloat iconY = self.tableView.frame.size.height / 2.0;
    self.bigIcon.frame = CGRectMake(iconX, iconY, 1, 1);
    [self.tableView addSubview:self.bigIcon];

    CGFloat iconW = self.tableView.frame.size.width;
    CGFloat iconH = iconW;
    iconY = (self.view.frame.size.height - iconH) / 2.0;
    [UIView animateWithDuration:0.3 animations:^{
        self.bigIcon.frame = CGRectMake(0, iconY, iconW, iconH);
    }];
}

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    您没有看到框架发生变化,因为您试图在将其添加到视图层次结构后立即对其进行动画处理。

    UIKit 需要将按钮添加为子视图,然后在下一次 UI 更新过程中运行动画。

    将您的动画块包装在 dispatch_async 块中,如下所示:

    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.3 animations:^{
            self.bigIcon.frame = CGRectMake(0, iconY, iconW, iconH);
        }];
    });
    

    【讨论】:

    • 非常感谢!它起作用了....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多