我可以使用以下代码向 imageView 层添加阴影。
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"]];
self.imageView.center = self.view.center;
CALayer *containerLayer= [CALayer layer];
containerLayer.shadowColor = [UIColor blackColor].CGColor;
containerLayer.shadowRadius = 10.0f;
containerLayer.shadowOffset = CGSizeMake(10.0f, 5.0f);
containerLayer.shadowOpacity = .8f;
[containerLayer addSublayer:self.imageView.layer];
[self.view.layer addSublayer:containerLayer];
1. 问题是不知道为什么要在containerLayer中加入imageView.layer才能得到imageView的阴影效果。但是,如果我将containerLayer添加到imageView.layer,imageView中没有阴影,为什么?
错误代码是:
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"]];
self.imageView.center = self.view.center;
CALayer *containerLayer= [CALayer layer];
/*same as before*/
[self.imageView.layer addSublayer:containerLayer];
[self.view.layer addSublayer:self.imageView.layer];
问题2:containerLayer的(用来给imageView提供阴影)frame = {{0, 0}, {0, 0}},但最终的位置是在屏幕的中心。为什么?