我正在尝试将两个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];
}
答案 0 :(得分:0)
我还不知道如何(或者是否可能)接受评论中留下的答案。因此,为了将此标记为已回答,我将引用第一个正确答案。我很抱歉,如果这不能正确帮助lnafziger在这里代表,但我不知道还能做什么。
lnafziger说......“可能是因为它是同一个对象。请尝试[beeArray addObject:[beeLayer copy]];而不是.-- lnafziger”
这很有用。非常感谢你。