【问题标题】:How to move line?如何移动线路?
【发布时间】:2017-08-20 09:51:19
【问题描述】:

如何在垂直方向移动水平线? 我可以画一条水平直线,我需要长按来移动它。 我该怎么做?

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
    if let touch = touches.first 
    {
        let currentPoint = touch.location(in: self.view)
        DrawLine(FromPoint: currentPoint, toPoint: currentPoint)
    }
}
func DrawLine(FromPoint: CGPoint, toPoint: CGPoint)
{
    UIGraphicsBeginImageContext(self.view.frame.size)
    imageView.image?.draw(in: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
    let context = UIGraphicsGetCurrentContext()
    let lineWidth : CGFloat = 5
    context?.move(to: CGPoint(x: FromPoint.x, y: FromPoint.y))
    context?.addLine(to: CGPoint(x: FromPoint.x + 200, y: FromPoint.y))
    context?.setBlendMode(CGBlendMode.normal)
    context?.setLineCap(CGLineCap.round)
    context?.setLineWidth(lineWidth)
    context?.setStrokeColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1.0).cgColor)
    context?.strokePath()
    imageView.image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
}

【问题讨论】:

    标签: ios swift xcode swift3 core-graphics


    【解决方案1】:

    您可以使用 UIView 绘制一条水平线,并可以通过提供所需选项使用 UIView.animate 方法将其沿垂直方向移动。

    在您的 ViewController 中定义以下变量:

    var scanlineRect = CGRect.zero
    var scanlineStartY: CGFloat = 0
    var scanlineStopY: CGFloat = 0
    var topBottomMargin: CGFloat = 30
    var scanLine: UIView = UIView()
    

    viewDidLoad 方法中调用 drawLine 方法并在您的触摸事件上调用 moveVertically 方法:

    func drawLine() {
            self.addSubview(scanLine)
            scanLine.backgroundColor = UIColor(red: 0.4, green: 0.8, blue: 0.4, alpha: 1.0) // green color
            scanlineRect = CGRect(x: 0, y: 0, width: self.frame.width, height: 2)
            scanlineStartY = topBottomMargin
            scanlineStopY = self.frame.size.height - topBottomMargin
        }
    
    func moveVertically() {
            scanLine.frame  = scanlineRect
            scanLine.center = CGPoint(x: scanLine.center.x, y: scanlineStartY)
            scanLine.isHidden = false
            weak var weakSelf = scanLine
            UIView.animate(withDuration: 1.0, delay: 0.0, options: [.repeat, .autoreverse, .beginFromCurrentState], animations: {() -> Void in
                weakSelf!.center = CGPoint(x: weakSelf!.center.x, y: scanlineStopY)
                }, completion: nil)
        }
    

    【讨论】:

    • 如果我有 3 行呢?我怎样才能区分它们并移动需要的?
    • 您可以通过将框架 CGRect 传递给该方法来绘制三条线,并为要移动的线实例调用 moveVertically 方法。所以你可以根据你的要求修改上面的代码。
    • 上面的代码是画一条线,垂直移动。
    • CGRect 框架是什么意思?不知道怎么用。
    • 您正在使用UIView创建线条,因此您需要使用包含视图位置和尺寸(例如线条)的CGRect结构来定义视图的框架
    【解决方案2】:

    您可以做的是在 UIView 上绘制这条线,然后将 UIView 向上移动或移动到您想要的任何位置,如下所示:

    UIView.animate(withDuration: 0.3, animations: {
                    yourView.origin = CGPoint(yourView.frame.origin.x, yourYPosition)
    
                }, completion: { (value: Bool) in
                    //do anything after animation is done
                })
    

    【讨论】:

    • 是否可以使用 UIImageView 做到这一点?
    • 当然可以,只需将 UIView 替换为 UIImageView
    猜你喜欢
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 2016-11-05
    • 1970-01-01
    相关资源
    最近更新 更多