CAShapeLayer 圆圈未在 iPhone 6 上显示,但它在 iPhone 5 上使用相同的代码工作
CAShapeLayer circle not showing on iPhone 6, but its working on iPhone 5 with the same code
我在自定义 uiview class 中使用 cashapelayer 以编程方式制作了一个简单的圆,如下所示:
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")
}
}
并在自定义集合视图单元格中对其进行初始化:
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
你在使用模拟器吗?
您确定每个 iPhone 具有相同的 iOS 吗?
如果 iPhone5 有 iOS < 10 然后尝试使用:
private func setupViews() {
self.layer.mask = leftPhotoArc
}
尝试像这样交换开始和结束角度:
leftPhotoArc.path = UIBezierPath(arcCenter: CGPoint(x: 25, y: 25), radius: 25 , startAngle: 0, endAngle: 2 * CGFloat(M_PI), clockwise: true).cgPath
这是合理的,因为你顺时针从 0 移动到 2π。
我在自定义 uiview class 中使用 cashapelayer 以编程方式制作了一个简单的圆,如下所示:
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")
}
}
并在自定义集合视图单元格中对其进行初始化:
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
你在使用模拟器吗?
您确定每个 iPhone 具有相同的 iOS 吗?
如果 iPhone5 有 iOS < 10 然后尝试使用:
private func setupViews() {
self.layer.mask = leftPhotoArc
}
尝试像这样交换开始和结束角度:
leftPhotoArc.path = UIBezierPath(arcCenter: CGPoint(x: 25, y: 25), radius: 25 , startAngle: 0, endAngle: 2 * CGFloat(M_PI), clockwise: true).cgPath
这是合理的,因为你顺时针从 0 移动到 2π。