【问题标题】:Multiple flip animation not working多次翻转动画不起作用
【发布时间】:2012-10-23 02:08:43
【问题描述】:

我遇到了一些奇怪的事情。我正在尝试创建一个不断翻转的 UIView 网格。为此,我创建了以下类:

@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

然后,我尝试了这样的课程:

- (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];
}

这种方式效果很好。关键是,我需要以这种方式实例化几个翻转视图,并且需要在“for”循环中实现它们。所以我尝试了这个:

- (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];
    }
}

当我尝试使用循环时,第一个视图动画正确,但其他视图动画不正确。只是跳过了动画,直接进行了视图切换。

有人知道为什么吗?我的猜测是这是一个线程问题或类似的东西,但我对这些东西一无所知。欢迎任何帮助。谢谢。

【问题讨论】:

    标签: ios multithreading animation flip


    【解决方案1】:

    viewDidLoad 的 2 个版本之间存在一些不一致。我会这样写循环的:

    - (void)viewDidLoad {
      [super viewDidLoad];
      for (int i=0;i<2;i++) {
        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:viewThree viewTwo:viewFour animationDuration:0.5 andPauseInBetweenAnimation:3];
        [flipViewController.view setFrame:CGRectMake(0, i*60, 100, 50)];
        [self.view addSubview:flipViewController.view];
    
      }
    }
    

    不知道这是否会有所改善。

    还有另一个问题(因为您说您没有使用 ARC):您的所有 LMFlipViewController 对象都会泄漏,因为它们是分配/初始化的并且从未释放(它们不能,因为它们没有被存储并且它们引用退出viewDidLoad 方法后只是“丢失”)。

    解决此问题的方法是保留您实例化的所有控制器的NSMutableArray

    self.flipControllers = [NSMutableArray arrayWithCapacity:...];
    
    ...
    

    然后在viewDidLoad(ARC 案例)中:

    [self.flipControllers addObject:[[LMFlipViewController alloc] initWithViewOne:viewOne viewTwo:viewTwo animationDuration:0.5 andPauseInBetweenAnimation:3]];
    [self.view addSubview:[self.flipControllers lastObject].view];
    

    【讨论】:

    • 感谢您的回复。我已经尝试使用数组来存储我的控制器,然后将它们回调。发生了同样的问题,所以我只是将其删除。顺便说一句,如果有帮助,我不会使用 ARC。
    • 编辑:实际上你帖子的第一部分成功了。谢谢,现在一切正常。
    猜你喜欢
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 2023-03-06
    相关资源
    最近更新 更多