Objective c 如何循环CATTransaction块中包含的多个Cabasicanation?

Objective c 如何循环CATTransaction块中包含的多个Cabasicanation?,objective-c,xcode,repeat,cabasicanimation,catransaction,Objective C,Xcode,Repeat,Cabasicanimation,Catransaction,在Xcode 4.3.2中为iPhone 5.1使用Obj.-c;我创建了一个Calayer数组,都来自同一个图像。然后,我希望通过CATTransactions分组,对数组中的每个CALayer同时应用Cabasicanation。这一切都是一次性的。但是,我想反复调用CATTransactions中包含的Cabasicanimation块,但每次同时执行该块时,都能够单独修改每个动画的属性。例如,我有动画的from和to值,我希望每次为每个层上的动画随机更改这些值。因为我想重复相同的基本动画

在Xcode 4.3.2中为iPhone 5.1使用Obj.-c;我创建了一个Calayer数组,都来自同一个图像。然后,我希望通过CATTransactions分组,对数组中的每个CALayer同时应用Cabasicanation。这一切都是一次性的。但是,我想反复调用CATTransactions中包含的Cabasicanimation块,但每次同时执行该块时,都能够单独修改每个动画的属性。例如,我有动画的from和to值,我希望每次为每个层上的动画随机更改这些值。因为我想重复相同的基本动画,但进行属性修改;将动画的repeatCount属性设置为某个高值将不起作用。我已尝试在MakeSarm方法中使用for循环重复调用animate方法,使用animationDidStop诱导另一个animate方法调用,但最终发生的是使用CATTransaction块进行新调用,而不是在最后,并且还具有方法调用本身(将[self animate];放在animate方法的末尾);这些都不管用。这是基本代码。我认为这很简单,但我没有看到什么重要的东西。谢谢你,赛斯

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:UIViewController{
UIImage*beeImage;
UIImageView*beeView;
卡莱尔*比耶尔;
卡巴斯卡化*动画;
NSMUTABLEARRY*BEERARRAY;
NSMUTABLEARRY*BEEANMARRAY;
}
@属性(保留,非原子)UIImage*beeImage;
@属性(保留,非原子)NSMutableArray*beeArray;
@属性(保留,非原子)NSMutableArray*beeanimArray;
@属性(保留,非原子)UIImageView*beeView;
@属性(保留,非原子)CALayer*Beepayer;
@属性(保留,非原子)CABasicAnimation*动画;
-(空)动画化;
-(无效)使群集;
@结束
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];     


}
-(无效)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 ImageName:@“bee50x55px.png”];
beeFrame=CGRectMake(0,0,beeImage.size.width,beeImage.size.height);
int i;
CALayer*p=[[CALayer alloc]init];
对于(i=0;i<3;i++){
beeView=[[UIImageView alloc]initWithFrame:beeFrame];
beeView.image=beeImage;
beeLayer=[beeView层];
[beeArray addObject:beeLayer];
p=[beeArray objectAtIndex:i];
[p设置位置:CGPointMake(arc4random()%320,arc4random()%480)];
[self.view.layer addSublayer:p];
} 
[自我动画];
}
-(空)设置动画
{
//从这里到这个方法末尾的代码是我想重复多次的代码
[交易开始];
int i;
对于(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)];
[动画设置填充模式:kCAFillModeForwards];
[动画设置移除完成:否];
动画持续时间=1.0;
CALayer*p=[[CALayer alloc]init];
p=[beeArray objectAtIndex:i];
[p addAnimation:animate forKey:@“position”];
}    
[CATransaction-commit];
}

我相信我自己已经回答了这个问题。我在循环结束时(当I==2)和动画结束时(指示循环结束)设置动画的代理,然后从animationDidStop方法调用animate方法。如果有比这更优雅或无故障的解决方案,我会洗耳恭听,并接受它作为答案。

我相信我自己已经回答了这个问题。我在循环结束时(当I==2)和动画结束时(指示循环结束)设置动画的代理,然后从animationDidStop方法调用animate方法。如果有比这更优雅或无故障的解决方案,我洗耳恭听并接受它作为答案;如果(i==0)animat.delegate=self;将其添加到animate方法中,然后将其作为另一个方法添加-(void)animationDidStop:(CAAnimation*)animation finished:(BOOL)flag{[self animate];}堆栈最终是否会充满动画的方法调用?