0

我正在尝试将两个 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];        


}
4

1 回答 1

0

我还不知道如何(或是否有可能)接受评论中留下的答案。因此,为了将其标记为已回答,我将引用第一个正确答案。如果这不能正确地帮助 lnafziger 在这里代表,我很抱歉,但我不知道还能做什么。

纳夫齐格说……

“这可能是因为它是同一个对象。尝试 [beeArray addObject:[beeLayer copy]]; 代替。 – lnafziger”

这行得通。非常感谢。

于 2012-04-13T17:50:19.173 回答