Reputation: 555
I'm using in a class this method to start an action:
[self performSelector:@selector(startRolling) withObject:nil afterDelay:0.1f];
In order to stop it I'm using:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(startRolling) object:nil];
But never is stopped.
Both call are implemented in the same class, and the same thread.
I've used this too but it doesn't solve it:
-(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];
}
What I'm missing?
Thanks.
---EDIT---
To know if is Main Thread I'm using:
-(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];
}
When works ok and when doesn't always in the Log appears Is Main Thread.
Which are doing the selectors I'm launching (startRolling and commitAnimations) are animations using [UIView beginAnimation:context:)
Is possible that this is the reason?
Here the methods:
-(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];
}
The method that calls the stopRolling method is being called using performOnMainThread:
Thanks.
--EDIT---
I've modified the method with the suggestions but still not working:
-(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];
}
I've observed that if the user taps the screen when a transition between images is being executed is when doesn't works. But If user taps on screen when the image is on screen (during 2 seconds) all works fine.
Always on Main Thread.
:( I'm desesperated, this makes the program crashes because memory.
--EDIT--
Finally I've solved using a BOOL flag. But I think is a poor solution because this must works without. Something strange is happening while an animation is being executed because this only worked when the animation is not being executed.
This works for all the cases:
- (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];
}
Thanks for everything.
Upvotes: 3
Views: 5403
Reputation: 141
change the arguments passed to both the methods as shown,
[self performSelector:@selector(SampleMethod) withObject:self afterDelay:delayTime];
[NSObject cancelPreviousPerformRequestsWithTarget:self];
Upvotes: 4