CAShapeLayer circle not showing on iPhone 6, but its working on iPhone 5 with the same code
我在自定义uiview类中使用cashapelayer以编程方式制作了一个简单的圆圈,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class UserProfileCircleAnimated: UIView { private lazy var leftPhotoArc: CAShapeLayer = { let leftPhotoArc = CAShapeLayer() leftPhotoArc.lineWidth = 6 leftPhotoArc.strokeColor = UIColor.rgb(red: 232, green: 72, blue: 85, alpha: 1).cgColor leftPhotoArc.fillColor = nil leftPhotoArc.path = UIBezierPath(arcCenter: CGPoint(x: 25, y: 25), radius: 25 , startAngle: 2 * CGFloat(M_PI), endAngle: 0, clockwise: true).cgPath return leftPhotoArc }() override init(frame: CGRect) { super.init(frame: frame) translatesAutoresizingMaskIntoConstraints = false backgroundColor = UIColor.yellow setupViews() } private func setupViews(){ layer.addSublayer(leftPhotoArc) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } |
并在自定义集合视图单元格中对其进行了初始化:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class AllCell: UICollectionViewCell { let userProfileCircleAnimated = UserProfileCircleAnimated() override init(frame: CGRect) { super.init(frame: frame) addSubview(userProfileCircleAnimated) userProfileCircleAnimated.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true userProfileCircleAnimated.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true userProfileCircleAnimated.widthAnchor.constraint(equalToConstant: 50).isActive = true userProfileCircleAnimated.heightAnchor.constraint(equalToConstant: 50).isActive = true } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } |
,结果是它出现在ip5模拟器上,而不是出现在ip6上,为什么?谢谢!
iphone 5
iphone 6
尝试像这样交换起点和终点angular:
1 | leftPhotoArc.path = UIBezierPath(arcCenter: CGPoint(x: 25, y: 25), radius: 25 , startAngle: 0, endAngle: 2 * CGFloat(M_PI), clockwise: true).cgPath |
这是合理的原因,因为您顺时针从0移到2π。
您在使用模拟器吗?
您确定每个iPhone都具有相同的iOS吗?
如果iPhone5的iOS <10,请尝试使用:
1 2 3 | private func setupViews() { self.layer.mask = leftPhotoArc } |