我正在尝试将两个 CALayers 作为子层从一个数组添加到我的视图层。下面的代码只添加了最后一个尝试的子层(它被命名为 q)。我试图让它首先与数组中的两个一起工作,这样我以后就可以使用 for 循环添加尽可能多的东西。关于让两个子层同时显示的任何建议?
ViewController.h 是
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
UIImage *beeImage;
UIImageView *beeView;
CALayer *beeLayer;
CABasicAnimation *animation;
NSMutableArray *beeArray;
}
@property(retain,nonatomic) UIImage *beeImage;
@property(retain,nonatomic) NSArray *beeArray;
@property(retain,nonatomic) UIImageView *beeView;
@property(retain,nonatomic) CALayer *beeLayer;
@property(retain,nonatomic)CABasicAnimation *animation;
-(void) animate;
-(void) makeSwarm;
@end
这是 ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self makeSwarm];
}
-(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);
beeView = [[UIImageView alloc] initWithFrame:beeFrame];
beeView.image = beeImage;
beeLayer = [beeView layer];
CALayer *p = [[CALayer alloc] init];
CALayer *q = [[CALayer alloc] init];
[beeArray addObject: beeLayer];
[beeArray addObject: beeLayer];
p = [beeArray objectAtIndex: 0];
[p setPosition:CGPointMake(10, 10)];
p.zPosition=0;
[self.view.layer addSublayer:p];
q = [beeArray objectAtIndex: 1];
[q setPosition:CGPointMake(160,240)];
q.zPosition=1;
[self.view.layer insertSublayer:q below:p];
}