![](https://img.haomeiwen.com/i10362904/b6506cf2d4f584c9.png)
实现以上图片旋转堆叠效果:
var orderImg =UIImageView()
foriin0..<5{
letimg = UIImageView()
if(i==0) {
orderImg = img
aboveView.addSubview(orderImg)
}else{
aboveView.insertSubview(img, belowSubview: orderImg)
orderImg = img
}
letaboveViewWidth =UIScreen.main.bounds.width*0.5
letimgWidth =calculate(w:200.0)
letimgHeight =calculate(wh:282.0, pwh:200.0, cpwh:imgWidth)
letimgX = (aboveViewWidth-imgWidth)*0.5
letimgY =calculate(h:70.0)
img.frame=CGRect(x: imgX, y:imgY, width: imgWidth, height: imgHeight)
img.image=UIImage(named:"select_small_photo")
img.alpha=1-0.1*CGFloat(i)
//自身的旋转点
img.layer.anchorPoint=CGPoint(x:0.5, y:1.0)
//相对于父控件的旋转点(img的父控件为aboveView)
img.layer.position=CGPoint(x: imgX+imgWidth*0.5, y: imgY+imgHeight)
//设置图片的旋转角度
letangle = (CGFloat.pi/18.0)*CGFloat(i)
img.transform=CGAffineTransform(rotationAngle: angle)
}
注意事项:
1、用frame,不要用Masonry或snap布局
![](https://img.haomeiwen.com/i10362904/24a5a7d89bce8612.png)
2、anchorPoint和position两个属性必须要设置,否则显示的坐标有问题
![](https://img.haomeiwen.com/i10362904/e98c4501fe48c6c4.png)
网友评论