질문을하기 전에, 나는 SO 수색 :뷰/레이어의 베 지어 곡선 경로로의 이동 경로를 제한하는 방법은 무엇입니까?
iPhone-Move UI Image view along a Bezier curve path
그러나 그것은 명백한 대답을하지 않았다.
필자의 요구 사항은 다음과 같습니다. 베 지어 경로와 뷰 (또는 레이어가 OK 인 경우)가 있고, 뷰에 pan gesture
을 추가하고 뷰 (또는 레이어)의 이동 경로가 베 지어 경로.
내 코드는 다음과 같습니다 :
MainDrawView.swift
:
import UIKit
class MainDrawView: UIView {
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
drawArc()
}
func drawArc() {
let context = UIGraphicsGetCurrentContext()
// set start point
context?.move(to: CGPoint.init(x: 0, y: 400))
//draw curve
context?.addQuadCurve(to: CGPoint.init(x: 500, y: 250), control: CGPoint.init(x: UIScreen.main.bounds.size.width, y: 200))
context?.strokePath()
}
}
ViewController.swift
:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var clickButton: UIButton!
lazy var view1:UIView = {
let view: UIView = UIView.init(frame: CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: 10, height: 10)))
view.center = CGPoint.init(x: UIScreen.main.bounds.size.width/2.0, y: UIScreen.main.bounds.size.height/2.0)
view.backgroundColor = UIColor.red
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
initData()
initUI()
}
func initData() {
}
func initUI() {
self.clickButton.isHidden = true
// init the view
self.view.addSubview(self.view1)
}
}
편집 -1
내가보기는 베 지어 패스에 이동보기/레이어를 이동 pan guester
사용할 때 내 깊은의 requirment입니다.
CALayers가 될 수 있습니까? 적어도 내 의견으로는 UIView를 훨씬 쉽게 수행 할 수 있습니다. 단일 UIView는 여러 개의 CALayer를 가질 수 있습니다. 이 방법이 효과가 있다면 트릭을 할 수있는 코드가 있습니다. – dfd
@dfd, ok, use CALayer도 구현할 수 있습니다. – aircraft
@aircraft - 그렇다면 사용자가 'UIPanGestureRecognizer'를 활성화 시키려고한다는 것을 올바르게 이해할 수 있습니까? 업데이트로 인해 사각형이 경로를 따라 이동하게됩니까? – Pierce