CAShapeLayer circle not showing on iPhone 6, but its working on iPhone 5 with the same code

≡放荡痞女 提交于 2019-12-12 03:04:03

问题


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
}


来源:https://stackoverflow.com/questions/40614570/cashapelayer-circle-not-showing-on-iphone-6-but-its-working-on-iphone-5-with-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!