Using Obj.-c for iPhone 5.1 in Xcode 4.3.2; I am creating an array of CALayers, all from the same image. Then I want to apply CABasicAnimation to each CALayer in the array at the same time, grouping through CATransactions. All this works once. However, I would like to re-call the CABasicAnimations block, which is contained in CATransactions, but be able to change the properties of each animation individually each time the block is executed at the same time. For example, I also have values for animation, which I would like to change randomly every time for animation at each level. Because I would like to repeat the same basic animation, but make changes to the properties; setting the repeatCount property of the animation to some high value will not work. I tried repeatedly repeating the animate method,using the for loop in the makeSwarm method, using the DidStop animation to invoke another call to the animation method, but what ultimately happens is a new call. made with a CATransaction block, not at the end, as well as 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 suppose this is straightforward, but I don't see anything important. Thanks 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
{
[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];
}
source
share