0
我想將兩個CALayers作爲數組的子層添加到我的視圖層。以下代碼僅添加了最後一個嘗試的子層(名爲q)。我試圖讓這個在數組中首先使用兩個,這樣我可以在後面添加儘可能多的for循環。任何建議讓兩個子層同時顯示?在Xcode 4中添加多個子層?只有一個顯示
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];
}
這可能是因爲它是同一個對象。嘗試'[beeArray addObject:[beeLayer copy]];'而不是。 – lnafziger 2012-04-13 02:47:03
是的,因爲它們是同一個對象,我非常確定CALayers不支持複製,所以創建CALayer的全新實例將是最佳選擇 – Otium 2012-04-13 03:20:55
謝謝!去我的第一組動畫,所以這是我的必要的先決條件。我認爲這個問題對指針的理解不夠(對我來說這是一個新概念)? – 2012-04-13 17:47:35