CAShapeLayer circle not showing on iPhone 6, but i

2019-08-25 03:47发布

问题:

i made a simple circle programatically with cashapelayer in custom uiview class like this:

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")
}
}

and initialized it in custom collection view cell:

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")
}
}

and the result is that it appears on ip5 simulator, but not on ip6, why is that? Thank you! iphone 5 iphone 6

回答1:

Try exchange start and end angle like this:

leftPhotoArc.path = UIBezierPath(arcCenter: CGPoint(x: 25, y: 25), radius: 25 , startAngle: 0, endAngle: 2 * CGFloat(M_PI), clockwise: true).cgPath

it's reasonable cause you move from 0 to 2π clockwise.



回答2:

Are you using simulators?
Are you sure that each iPhone have the same iOS?
If iPhone5 have iOS < 10 then try using:

private func setupViews() {
    self.layer.mask = leftPhotoArc
}