I’m having a trouble with something weird. I’m trying to create a grid of UIViews flipping endlessly. For this purpose, I created the following class:
@implementation LMFlipViewController
@synthesize viewOne=_viewOne, viewTwo=_viewTwo, animationDuration=_animationDuration, pause=_pause, animationFlag=_animationFlag;
-(LMFlipViewController *)initWithViewOne:(UIView *)viewOne viewTwo:(UIView *)viewTwo animationDuration:(CGFloat)animationDuration andPauseInBetweenAnimation:(CGFloat)pause {
[super init];
[self.view setFrame:viewOne.bounds];
_viewOne=viewOne;
_viewTwo=viewTwo;
_animationDuration=animationDuration;
_pause=pause;
return self;
}
-(void)viewDidLoad{
[super viewDidLoad];
NSLog(@"0");
_animationFlag=NO;
}
-(void)viewDidUnload{
[super viewDidUnload];
}
-(void)viewWillAppear:(BOOL)animated {
[self.view addSubview:_viewOne];
// [self performSelector:@selector(loopAnimation) withObject:nil afterDelay:_pause];
[NSTimer scheduledTimerWithTimeInterval:_pause
target:self
selector:@selector(loopAnimation)
userInfo:nil
repeats:NO];
NSLog(@"1");
}
-(void)triggerAnimation {
NSLog(@"5");
if (_animationFlag) {
NSLog(@"Animation flag: YES");
_animationFlag=NO;
[UIView transitionWithView:self.view duration:_animationDuration options:(UIViewAnimationOptionTransitionFlipFromLeft) animations: ^{
[_viewTwo removeFromSuperview];
} completion:^(BOOL finished) {
[UIView transitionWithView:self.view duration:_animationDuration options:(UIViewAnimationOptionTransitionFlipFromLeft) animations: ^{
[self.view addSubview:_viewOne];
} completion:nil];
}];
}else{
NSLog(@"Animation flag: NO");
_animationFlag=YES;
[UIView transitionWithView:self.view duration:_animationDuration options:(UIViewAnimationOptionTransitionFlipFromLeft) animations: ^{
[_viewOne removeFromSuperview];
} completion:^(BOOL finished) {
[UIView transitionWithView:self.view duration:_animationDuration options:(UIViewAnimationOptionTransitionFlipFromLeft) animations: ^{
[self.view addSubview:_viewTwo];
} completion:nil];
}];
}
NSLog(@"6");
}
-(void)loopAnimation {
NSLog(@"2");
[self triggerAnimation];
NSLog(@"3");
// [self performSelector:@selector(loopAnimation) withObject:nil afterDelay:_pause];
[NSTimer scheduledTimerWithTimeInterval:_pause
target:self
selector:@selector(loopAnimation)
userInfo:nil
repeats:NO];
NSLog(@"4");
}
@end
Then, I’ve tried this class like this:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *viewOne=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
viewOne.backgroundColor=[UIColor redColor];
UIView *viewTwo=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
viewTwo.backgroundColor=[UIColor purpleColor];
LMFlipViewController *flipViewController=[[LMFlipViewController alloc] initWithViewOne:viewOne viewTwo:viewTwo animationDuration:0.5 andPauseInBetweenAnimation:3];
[self.view addSubview:flipViewController.view];
UIView *viewThree=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
viewThree.backgroundColor=[UIColor blackColor];
UIView *viewFour=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
viewFour.backgroundColor=[UIColor blueColor];
LMFlipViewController *flipViewControllerTwo=[[LMFlipViewController alloc] initWithViewOne:viewThree viewTwo:viewFour animationDuration:0.5 andPauseInBetweenAnimation:3];
[flipViewControllerTwo.view setFrame:CGRectMake(0, 60, 100, 50)];
[self.view addSubview:flipViewControllerTwo.view];
}
This way worked perfectly. The point is, I need to instanciate several flip views this way and I need them implemented in a “for” loop. So I tried this:
- (void)viewDidLoad {
[super viewDidLoad];
for (int i=0;i<2;i++) {
UIView *viewOne=[[UIView alloc] initWithFrame:CGRectMake(0, i*60, 100, 50)];
viewOne.backgroundColor=[UIColor redColor];
UIView *viewTwo=[[UIView alloc] initWithFrame:CGRectMake(0, i*60, 100, 50)];
viewTwo.backgroundColor=[UIColor purpleColor];
[self.view addSubview:[[LMFlipViewController alloc] initWithViewOne:viewOne viewTwo:viewTwo animationDuration:0.5 andPauseInBetweenAnimation:3].view];
}
}
When I try with the loop, the first view is animating correctly but the others are not. The animation is just skipped and the view switch is made straight.
Does anyone know why ? My guess would be it’s a thread issue or something like this but I’m clueless about these stuffs. Any help is welcomed. Thx.
There is a little inconsistency between the 2 versions of
viewDidLoad. I would write the looped one like this:Don’t know if this will improve things.
There is another issue (since you say you are not using ARC): all of your
LMFlipViewControllerobjects will leak, since they are alloc/inited and never released (they cannot be, since they are not stored and they reference are simply “lost” after exiting theviewDidLoadmethod).The way around this is keeping an
NSMutableArrayof all the controllers you instantiate:and then in
viewDidLoad(ARC case):