【发布时间】:2017-08-26 12:05:54
【问题描述】:
我在 ViewController 中为一行定义以下变量。
var scanlineRect = CGRect.zero
var scanlineStartY: CGFloat = 0
var scanlineStopY: CGFloat = 0
var topBottomMargin: CGFloat = 30
var scanLine: UIView = UIView()
我可以使用 UIView 绘制一条线,并可以使用 UIView.animate 方法将其沿垂直方向移动。如何用手指拖动一条线?
func drawLine()
{
self.view.addSubview(scanLine)
scanLine.backgroundColor = UIColor.black
scanlineRect = CGRect(x: 15, y: 0, width: self.view.frame.width - 30, height: 5)
scanlineStartY = topBottomMargin
scanlineStopY = self.view.frame.height - topBottomMargin
scanLine.frame = scanlineRect
scanLine.center = CGPoint(x: scanLine.center.x, y: scanlineStartY)
scanLine.isHidden = false
}
func Move(Location : CGPoint)
{
scanLine.isHidden = false
weak var weakSelf = scanLine
UIView.animate(withDuration: 3.0, animations: {
weakSelf!.center = CGPoint(x: weakSelf!.center.x, y: Location.y)
}, completion: nil)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
var lastPoint = CGPoint.zero
if let touch = touches.first
{
lastPoint = touch.location(in: self.view)
}
Move(Location: lastPoint)
}
override func viewDidLoad()
{
super.viewDidLoad()
drawLine()
}
【问题讨论】:
-
仅供参考 - 与您的问题无关,但不要在您的
Move方法中强制解开weakSelf。正确解包,因为它可能是nil并导致您的应用崩溃。 -
你只是在问如何拖动视图???
-
顺便说一句,与 Xcode 或 Core Graphics 无关。
-
@rmaddy 如何正确解包并避免应用崩溃?
-
解开它就像你做任何其他可选的。
标签: ios swift swift3 core-graphics