如何循环CATransaction块中包含的几个CABasicAnimations?

在Xcode 4.3.2中使用Obj.-c for iPhone 5.1; 我创建了一组CALayers,全部来自同一图像。 然后我希望通过CATransactions进行分组,同时将CABasicAnimation应用于阵列中的每个CALayer。 这一切都有效。 但是,我想重复调用CATransactions中包含的CABasicAnimations块,但每次同时执行块时,都可以单独修改每个动画的属性。 举例来说,我从每个层次上的动画每次随机更改动画的值和值。 因为我想重复相同的基本动画,但是需要修改属性; 动画的repeatCount属性设置为一些高值不会干活试图调用动画的方法反复使用的makeSwarm方法中循环使用animationDidStop诱导动画的方法的另一个电话,但什么最终发生的是一个新的呼叫使用CATransaction块创建,而不是在最后,并且也有方法调用自己(put [self animate];在animate方法结束时); 并且这些都不起作用。 这是基本的代码。 我认为这很简单,但我没有看到重要的东西。 谢谢,Seth

ViewController.h

#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{
    UIImage *beeImage;

    UIImageView *beeView;
    CALayer *beeLayer;
    CABasicAnimation *animat;   
    NSMutableArray *beeArray;
    NSMutableArray *beeanimArray;

}

@property(retain,nonatomic) UIImage *beeImage;
@property(retain,nonatomic) NSMutableArray *beeArray;
@property(retain,nonatomic) NSMutableArray *beeanimArray;
@property(retain,nonatomic) UIImageView *beeView;
@property(retain,nonatomic) CALayer *beeLayer;
@property(retain,nonatomic)CABasicAnimation *animat;
-(void) animate;
-(void) makeSwarm;


@end

ViewController.m

-(void) makeSwarm{

    self.view.layer.backgroundColor = [UIColor orangeColor].CGColor;
    self.view.layer.cornerRadius = 20.0;
    self.view.layer.frame = CGRectInset(self.view.layer.frame, 20, 20);

    CGRect beeFrame;
    beeArray = [[NSMutableArray alloc] init];
    beeImage = [UIImage imageNamed:@"bee50x55px.png"];
    beeFrame = CGRectMake(0, 0, beeImage.size.width, beeImage.size.height);


    int i;

    CALayer *p = [[CALayer alloc] init];


    for (i = 0; i < 3; i++) {



        beeView = [[UIImageView alloc] initWithFrame:beeFrame];
        beeView.image = beeImage;    
        beeLayer = [beeView layer];
        [beeArray addObject: beeLayer];  


        p = [beeArray objectAtIndex: i];    

        [p setPosition:CGPointMake(arc4random()%320, arc4random()%480)];
        [self.view.layer addSublayer:p];



    } 



    [self animate]; 

}

-(void)animate
{
    //the code from here to the end of this method is what I would like to repeat as many times as I would like
    [CATransaction begin];

    int i;
    for (i = 0; i < 3; i++) {  

        animat = [[CABasicAnimation alloc] init];
        [animat setFromValue:[NSValue valueWithCGPoint:CGPointMake(arc4random()%320, arc4random()%480)]];
        animat.toValue = [NSValue valueWithCGPoint:CGPointMake(arc4random()%320, arc4random()%480)];
        [animat setFillMode:kCAFillModeForwards];
        [animat setRemovedOnCompletion:NO];
        animat.duration=1.0;


        CALayer *p = [[CALayer alloc] init];
        p = [beeArray objectAtIndex: i]; 
        [p addAnimation:animat forKey:@"position"];



    }    

    [CATransaction commit];     


}

我相信我已经为自己回答了这个问题。 我在循环结束时设置动画的委托(当我== 2),当动画结束(指示循环结束),然后从animationDidStop方法,然后再次调用方法动画。 如果有比这更优雅或者无故障的解决方案,我会全神贯注,并且会接受它作为答案。

链接地址: http://www.djcxy.com/p/74213.html

上一篇: how to loop several CABasicAnimations contained in a CATransaction block?

下一篇: pausing and resuming core animation when app enters and exits background