Извините, это реальный контекст. Я хотел создать бесконечную шкалу времени, например, вкладку iOS календарных дней или шкалу времени в программе для редактирования видео. Я еще не понял, как получить расстояние от начала и конца касания и как связать его с переменной scrollDistance в начале, чтобы я мог создать эффект прокрутки вниз, а затем pinchgesturerecognizer, чтобы установить интервал / масштаб между линиями. Я знаю, что могу использовать UIscrollview или UIcollectionview, но я чувствовал, что было бы неплохо создать собственный xD Еще раз спасибо за поддержку всех! : D
import UIKit
class ViewController: UIViewController {
let lineElements: Array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let spacing: Int = 30 //spacing between elements
var scrollDistance = 90 // 90 for example, this should be the pangesture distance
//UIPanGestureRecogn function to determine the Y scroll distance from touch began to ended
@objc func touchinput (sender: UIPanGestureRecognizer) {
let touchlocation = sender.location(in: view)
var touchStart: CGPoint
var touchEnd: CGPoint
print("touch current location is", touchlocation)
if sender.state == UIGestureRecognizer.State.began {
touchStart = sender.location(in: view)
print("touch start", touchStart)
} else if sender.state == UIGestureRecognizer.State.ended {
touchEnd = sender.location(in: view)
print("touch end", touchEnd)
}
//I haven’t figure out how to get the distance between start and end and link it to the scrollDistance
//let scrollDistance = touchEnd.y - touchStart.y
}
override func viewDidLoad() {
super.viewDidLoad()
// ===================== Timeline a partir daqui =========================
let totalElements: Int = lineElements.count
for n in 1...totalElements {
let yPosition = lineElements[n - 1] * spacing + scrollDistance //Get Line heigh index times the spacing + scroll distance by touch pan gesture
let hourlabel = UILabel()
let linepath = UIBezierPath()
linepath.move(to: CGPoint(x: 60, y: yPosition))
linepath.addLine(to: CGPoint(x: 350, y: yPosition))
linepath.lineWidth = 8
let lineshape = CAShapeLayer()
lineshape.path = linepath.cgPath
lineshape.strokeColor = UIColor.blue.cgColor
lineshape.fillColor = UIColor.white.cgColor
self.view.layer.addSublayer(lineshape)
hourlabel.frame = CGRect(x: 5, y: yPosition - 20, width: 45, height: 40)
hourlabel.text = "\(n):00"
hourlabel.font = UIFont(name: "Avenir-Claro", size: 12)
hourlabel.textColor = UIColor.blue
hourlabel.textAlignment = .right
self.view.addSubview(hourlabel)
}
// ===== GESTURE =====
let gesto = UIPanGestureRecognizer()
gesto.addTarget(self, action: #selector(touchinput))
view.addGestureRecognizer(gesto)
}
}
´´´