gpt4 book ai didi

objective-c - cancelPreviousPerformRequestsWithTarget 不工作

转载 作者:搜寻专家 更新时间:2023-10-30 20:10:56 25 4
gpt4 key购买 nike

我在一个类中使用这个方法来开始一个 Action :

[self performSelector:@selector(startRolling) withObject:nil afterDelay:0.1f];

为了阻止它,我正在使用:

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startRolling) object:nil];

但从未停止。

这两个调用都在同一个类和同一个线程中实现。

我也用过这个但是没有解决:

-(void)stopRolling
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startRolling) object:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(commitAnimation) object:nil];

[NSObject cancelPreviousPerformRequestsWithTarget:self];
[[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget:self];
}

我错过了什么?

谢谢。

---编辑---

要知道我正在使用的是不是主线程:

-(void)stopRolling
{
if ([NSThread isMainThread])
NSLog(@"Is Main Thread");

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startRolling) object:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(commitAnimation) object:nil];

[NSObject cancelPreviousPerformRequestsWithTarget:self];
[[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget:self];
}

什么时候工作正常,什么时候不总是在日志中出现是主线程。

正在执行我正在启动的选择器(startRolling 和 commitAnimations)的是使用 [UIView beginAnimation:context:) 的动画

有没有可能是这个原因?

这里是方法:

-(void)startRolling
{
currentPic++;

if (currentPic > count)
currentPic = 1;

[UIView beginAnimations:@"fadeIn" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:RDFadeInDelay];

NSLog(@"RollUp: %@",[NSString stringWithFormat:@"RDInitialRollUp_%d",currentPic]);

background.image = [UIImage imageNamed:[NSString stringWithFormat:@"RDInitialRollUp_%d.jpg",currentPic]];

background.alpha = 1.0f;

[UIView commitAnimations];

}
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *) finished context:(void *) context
{
if ([animationID isEqualToString:@"fadeIn"])
{
[self performSelector:@selector(commitAnimation) withObject:nil afterDelay:RDOnScreenDelay];
}
else
{
[self performSelector:@selector(startRolling) withObject:nil afterDelay:0.1f];

}
}
-(void)commitAnimation
{
[UIView beginAnimations:@"fadeOut" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:RDFadeOutDelay];

background.alpha = 0.0f;

[UIView commitAnimations];
}
-(void)stopRolling
{
if ([NSThread isMainThread])
NSLog(@"Is Main Thread");

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startRolling) object:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(commitAnimation) object:nil];

[NSObject cancelPreviousPerformRequestsWithTarget:self];
[[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget:self];
}

正在使用 performOnMainThread 调用调用 stopRolling 方法的方法:

谢谢。

--编辑---

我已经根据建议修改了方法,但仍然无法正常工作:

-(void)stopRolling
{
if ([NSThread isMainThread])
NSLog(@"Is Main Thread");
else
NSLog(@"Is NOT Main Thread");

[self.layer removeAllAnimations];

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startRolling) object:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(commitAnimation) object:nil];

[NSObject cancelPreviousPerformRequestsWithTarget:self];
[[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget:self];
}

我观察到,如果用户在执行图像之间的转换时点击屏幕是不起作用的。但是如果用户在图像出现在屏幕上时(2 秒内)点击屏幕,一切正常。

始终在主线程上。

:(我很绝望,这让程序因为内存而崩溃。

--编辑--

最后,我使用 BOOL 标志解决了问题。但我认为这是一个糟糕的解决方案,因为这必须没有。执行动画时会发生一些奇怪的事情,因为这仅在动画未执行时有效。

这适用于所有情况:

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *) finished context:(void *) context
{
if (!mustAnimate) return;

if ([animationID isEqualToString:@"fadeIn"])
{
[self performSelector:@selector(commitAnimation) withObject:nil afterDelay:RDOnScreenDelay];
}
else
{
[self performSelector:@selector(startRolling) withObject:nil afterDelay:0.1f];

}
}
-(void)commitAnimation
{
[UIView beginAnimations:@"fadeOut" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:RDFadeOutDelay];

background.alpha = 0.0f;

[UIView commitAnimations];
}
-(void)stopRolling
{
mustAnimate = NO;

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startRolling) object:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(commitAnimation) object:nil];

}

谢谢你所做的一切。

最佳答案

如图所示更改传递给两个方法的参数,

[self performSelector:@selector(SampleMethod) withObject:self afterDelay:delayTime];     
[NSObject cancelPreviousPerformRequestsWithTarget:self];

关于objective-c - cancelPreviousPerformRequestsWithTarget 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9639492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com