【发布时间】:2018-07-30 21:46:50
【问题描述】:
我正在尝试在自定义 UIView 中绘制一个圆形计时器,如下所示:
override func draw(_ rect: CGRect) {
bgShapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: self.frame.midX , y: self.frame.midY), radius:
min(frame.width/2, frame.height/2), startAngle: -90.degreesToRadians, endAngle: 270.degreesToRadians, clockwise: true).cgPath
bgShapeLayer.strokeColor = self.backgroundStrokeColor
bgShapeLayer.fillColor = self.backgroundFillColor
bgShapeLayer.lineWidth = self.backgroundLineWidth
self.layer.addSublayer(bgShapeLayer)
timeLeftShapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: self.frame.midX , y: self.frame.midY), radius:
min(frame.width/2, frame.height/2), startAngle: -90.degreesToRadians, endAngle: 270.degreesToRadians, clockwise: true).cgPath
timeLeftShapeLayer.strokeColor = self.timeLeftSrtokeColor
timeLeftShapeLayer.fillColor = self.timeLeftFillColor
timeLeftShapeLayer.lineWidth = self.timeLeftLineWidth
self.layer.addSublayer(timeLeftShapeLayer)
timeLabel = UILabel(frame: CGRect(x: self.frame.midX-50 ,y: self.frame.midY-25, width: 100, height: 50))
timeLabel.adjustsFontSizeToFitWidth = true
timeLabel.textAlignment = .center
timeLabel.text = self.timeLeft.stringTime
timeLabel.textColor = self.textColor
timeLabel.font = self.textFont
self.addSubview(timeLabel)
strokeIt.toValue = 100 //From value is set in "startTimer(duration, timerProgress)
strokeIt.duration = self.timeLeft
// add the animation to your timeLeftShapeLayer
timeLeftShapeLayer.add(strokeIt, forKey: nil)
// define the future end time by adding the timeLeft to now Date()
}
它在 iPhone 6/7/X 上运行良好,但在较小的 iPhone(如 5/5s/SE)上,计时器超出了视图的范围。像这样:
[![SE][1]][1]
应该如何(iPhone X):
[![X][2]][2]
我试图找出原因几个小时,但我没有找到任何东西:(
有人有想法吗?谢谢! 雷尼尔·梅利安:
[![图片][3]][3]
[![2][4]][4]
注意:timer 视图以编程方式添加到名为 container 的超级视图中,该超级视图通过 Interface Builder 添加并启用了 Clip To Bounds。 container 在 UITableViewCell 内
完整代码:
public var backgroundStrokeColor: CGColor = UIColor.white.cgColor
public var backgroundFillColor: CGColor = UIColor.clear.cgColor
public var backgroundLineWidth: CGFloat = 15
public var timeLeftSrtokeColor: CGColor = UIColor.red.cgColor
public var timeLeftFillColor: CGColor = UIColor.clear.cgColor
public var timeLeftLineWidth: CGFloat = 15
public var textColor: UIColor = UIColor.white
public var textFont: UIFont = UIFont.systemFont(ofSize: 15.0)
fileprivate var timeLeft: TimeInterval = 0
fileprivate var endDate: Date?
fileprivate var timeLeftShapeLayer: CAShapeLayer?
fileprivate var bgShapeLayer: CAShapeLayer?
fileprivate var timeLabel: UILabel?
fileprivate var timer = Timer()
fileprivate let strokeIt = CABasicAnimation(keyPath: "strokeEnd")
//MARK: - UIView
override func draw(_ rect: CGRect) {
drawBgShape()
drawTimeLeftShape()
addTimeLabel()
strokeIt.toValue = 1 //From value is set in "startTimer(duration, timerProgress)
strokeIt.duration = self.timeLeft
// add the animation to your timeLeftShapeLayer
timeLeftShapeLayer?.add(strokeIt, forKey: nil)
// define the future end time by adding the timeLeft to now Date()
}
//MARK: - Public
public func startTimer(duration: TimeInterval, timerProgress: Int) {
self.timeLeft = duration
endDate = Date().addingTimeInterval(timeLeft)
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
strokeIt.fromValue = timerProgress
}
//MARK: - Private
fileprivate func drawBgShape() {
//we initialize and add the layer only if there is not initialized
if(bgShapeLayer == nil){
bgShapeLayer = CAShapeLayer()
self.layer.addSublayer(bgShapeLayer!)
}
bgShapeLayer?.path = UIBezierPath(arcCenter: CGPoint(x: self.frame.midX , y: self.frame.midY), radius:
min(frame.width/2, frame.height/2), startAngle: -90.degreesToRadians, endAngle: 270.degreesToRadians, clockwise: true).cgPath
bgShapeLayer?.strokeColor = self.backgroundStrokeColor
bgShapeLayer?.fillColor = self.backgroundFillColor
bgShapeLayer?.lineWidth = self.backgroundLineWidth
}
fileprivate func drawTimeLeftShape() {
//we initialize and add the layer only if there is not initialized
if(timeLeftShapeLayer == nil){
timeLeftShapeLayer = CAShapeLayer()
self.layer.addSublayer(timeLeftShapeLayer!)
}
timeLeftShapeLayer?.path = UIBezierPath(arcCenter: CGPoint(x: self.frame.midX , y: self.frame.midY), radius:
min(frame.width/2, frame.height/2), startAngle: -90.degreesToRadians, endAngle: 270.degreesToRadians, clockwise: true).cgPath
timeLeftShapeLayer?.strokeColor = self.timeLeftSrtokeColor
timeLeftShapeLayer?.fillColor = self.timeLeftFillColor
timeLeftShapeLayer?.lineWidth = self.timeLeftLineWidth
}
fileprivate func addTimeLabel() {
//we initialize and add the UILabel only if there is not initialized
if(timeLabel == nil){
timeLabel = UILabel()
self.addSubview(timeLabel!)
}
timeLabel?.frame = CGRect(x: self.frame.midX-50 ,y: self.frame.midY-25, width: 100, height: 50)
timeLabel?.adjustsFontSizeToFitWidth = true
timeLabel?.textAlignment = .center
timeLabel?.text = self.timeLeft.stringTime
timeLabel?.textColor = self.textColor
timeLabel?.font = self.textFont
}
//MARK: - Actions
@objc fileprivate func updateTime() {
if timeLeft > 0 {
timeLeft = endDate?.timeIntervalSinceNow ?? 0
timeLabel?.text = self.timeLeft.stringTime
} else {
timeLabel?.text = self.timeLeft.stringTime
timer.invalidate()
}
}
UITableViewCell 代码:
override func layoutSubviews() {
super.layoutSubviews()
let countDownTimer = CountDownTimer(frame: self.countDownTimerContainer.frame)
countDownTimer.frame.origin = CGPoint.zero
countDownTimer.frame = self.countDownTimerContainer.bounds
let secondsUntilEndDate = abs(self.timerEndDate.timeIntervalSinceNow)//abs = absolute value (-x to x)
countDownTimer.startTimer(duration: secondsUntilEndDate, timerProgress: self.timerProgress)
self.countDownTimerContainer.addSubview(countDownTimer)
self.countDownTimerContainer.setNeedsDisplay()
}
【问题讨论】:
-
添加 timeLeftShapeLayer.frame = self.bounds
-
我认为您需要检查您的 timeLeftShapeLayer 是否已添加
-
@ReinierMelian 我尝试将此行添加为
drawRect的第一行,但我看不出有什么区别... -
添加 self.layer.borderWidth = 1 和 self.layer.borderColor = UIColor.red.cgColor 并将图片发布到小设备中
-
你能分享一个演示吗????
标签: ios iphone swift cocoa-touch uiview