Ios 如何知道视图是否正在设置动画?

Ios 如何知道视图是否正在设置动画?,ios,objective-c,animation,uiview,uikit,Ios,Objective C,Animation,Uiview,Uikit,有没有办法知道我的setFrame:(或可设置动画的属性的其他setter)的自定义实现是从动画块调用的,即它将被设置动画还是直接设置 例如: - (void)setFrame:(CGRect)newFrame { [super setFrame:newFrame]; BOOL willBeAnimated = ????? if (willBeAnimated) { // do something } else { // do s

有没有办法知道我的
setFrame:
(或可设置动画的属性的其他setter)的自定义实现是从动画块调用的,即它将被设置动画还是直接设置

例如:

- (void)setFrame:(CGRect)newFrame {
    [super setFrame:newFrame];
    BOOL willBeAnimated = ?????
    if (willBeAnimated) {
        // do something 
    } else {
        // do something else
    }
}
在上述设置中,
willBeAnimated
应为
YES
,其名称如下:

- (void)someMethod {
    [UIView animateWithDuration:0.2 
                     animations:^{view.frame = someRect;}
                     completion:nil];
}
在这种情况下,
NO

- (void)someMethod {
    view.frame = someRect;
}

someMethod
这是UIKit内部的一个私有方法,我无法访问或更改,因此我必须从“外部”确定它。

在更改帧后,您应该能够检查
UIView
子类的
动画关键帧,以查看它是否正在被设置动画

- (void)setFrame:(CGRect)newFrame {
    [super setFrame:newFrame];
    BOOL willBeAnimated = [super.layer animationForKey:@"position"] ? YES : NO;
    if (willBeAnimated) {
        // do something 
    } else {
        // do something else
    }
}
您还可以使用
animationskies
来检查是否有任何动画,在这种情况下,它只会返回
position

此外,如果要强制更改不设置动画,可以使用
performWithoutAnimation:

 [UIView performWithoutAnimation:^{
        [super setFrame:newFrame];
    }];
编辑

我通过测试发现的另一个小贴士是,如果动画已经在进行中,您实际上可以停止动画,而是通过从层中删除动画,然后使用上述方法立即进行更改

- (void)setFrame:(CGRect)newFrame {
    [super setFrame:newFrame];
    BOOL willBeAnimated = [super.layer animationForKey:@"position"] ? YES : NO;
    BOOL shouldBeAnimated = // decide if you want to cancel the animation
    if (willBeAnimated && !shouldBeAnimated) {
        [super removeAnimationForKey:@"position"];
        [UIView performWithoutAnimation:^{
             [super setFrame:newFrame];
        }];
    } else {
        // do something else
    }
}

你能将动画:(BOOL)作为参数添加到你的实现中吗?如果你想调用set frame,你可以传递它,不管它是否在动画块中被调用?@Mike,很遗憾,我没有调用setFrame,我也没有访问该代码的权限。