how to loop several CABasicAnimations contained in a CATransaction block?

Using Obj.-c for iPhone 5.1 in Xcode 4.3.2; I create an array of CALayers, all from the same image. I then wish to apply a CABasicAnimation to each CALayer in the array simultaneously by grouping via CATransactions. This all works one time. However, I would like to repeatedly call the block of CABasicAnimations that are contained in CATransactions but be able to modify the properties of each animation individually each time the block is executed simultaneously. For instance, I have from and to values for the animation that I would like to change randomly each time for the animation on each layer. Because I would like to repeat the same basic animation, but make property modification; setting repeatCount property of the animation to some high value will not work.I have tried calling the animate method repeatedly using a for loop within the makeSwarm method, using animationDidStop to induce another call of animate method but what ends up happening is a new call is made with the CATransaction block and not at the end , and also have the method call itself (put [self animate]; at the end of the animate method); and none of this works. Here is the basic code. I assume this is straightforward, but I am not seeing something important. Thanks, Seth

the 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

the 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];     


}

I believe I have answered this for myself. I set the delegate for animation at the end of the loop (when i==2) and when that animation ends (indicating loop is over), then from the animationDidStop method I then call the method animate again. If there is a more elegant or trouble free solution than this, I'm all ears and will accept it as the answer.

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

上一篇: 为转换后的UIImageView设置动画效果?

下一篇: 如何循环CATransaction块中包含的几个CABasicAnimations?