gpt4 book ai didi

ios - 如何用手指移动线条?

转载 作者:行者123 更新时间:2023-11-30 12:12:19 27 4
gpt4 key购买 nike

我正在 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()
}

最佳答案

尝试使用touchesMoved而不是touchesBegan

 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
guard let location = touch?.location(in: self.view) else{
return
}
scanLine.frame.origin = CGPoint(x: location.x-scanLine.frame.size.width/2, y: location.y-scanLine.frame.size.height/2)
}

已更新

如果您只想在y 轴上移动,则需要更改此行

scanLine.frame.origin = CGPoint(x: location.x-scanLine.frame.size.width/2, y: location.y-scanLine.frame.size.height/2)

改用此请注意,scanline.frame.origin.x 未更改

scanLine.frame.origin = CGPoint(x: scanLine.frame.origin.x, y: location.y-scanLine.frame.size.height/2)

关于ios - 如何用手指移动线条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45895160/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com