试图用UIPanGesture绘制UIView

时间:2015-09-22 15:06:44

标签: ios swift uiview

Iterator<T>

这是我在我看来的代码。我错过了什么,它没有显示在我的屏幕上?它确实识别我的移动class theView: UIView { var kl = 1 var px : CGFloat = 0.0 var py : CGFloat = 0.0 override func drawRect(rect: CGRect) { color() setNeedsDisplay() } func move(gesture : UIPanGestureRecognizer) { let state = gesture.state switch state { case .Began: print("started") case .Ended: fallthrough case .Changed: let translation = gesture.translationInView(self) px = translation.x py = translation.y print("x:\(px), y:\(py)") default: break } } func color() { let path = UIBezierPath() path.moveToPoint(CGPointZero) path.addLineToPoint(CGPoint(x: px, y: py)) UIColor.blueColor().set() path.stroke() self.setNeedsDisplay() } } px更改值。这是更大项目的一部分所以请告诉我是否有任何强制错误..

我的viewController是:

py

也有人可以告诉我,我需要把它class ViewController: UIViewController, Moveable { var k = 1 @IBOutlet weak var sdasd: theView! { didSet { sdasd.k = self sdasd.addGestureRecognizer(recognizer) let panRecognizer = UIPanGestureRecognizer(target: sdasd, action: "move:") sdasd.addGestureRecognizer(panRecognizer) sdasd.setNeedsDisplay() } } 放在哪里?我需要在视图控制器中使用它吗?我的覆盖绘图?谢谢。告诉我你是否需要任何东西。我希望它在我看来绘制线条。喜欢绘画

2 个答案:

答案 0 :(得分:0)

setNeedsDisplay()通知您的UIView需要在下一个绘图周期内重绘它。在drawRect函数中,指定应在UIView上绘制的内容。您希望每次在GestureRecognizer中具有Changed状态时更改UIView的外观,这样您也需要通知您重新绘制视图,即调用setNeedsDisplay(),并且仅在那里。

修改

以下是您要搜索的课程

class theView: UIView
{
    var kl = 1
    var px : CGFloat = 0.0
    var py : CGFloat = 0.0
    var pxStart : CGFloat = 0.0
    var pyStart : CGFloat = 0.0

    override init(frame: CGRect) {
        super.init(frame: frame)
        let panRecognizer = UIPanGestureRecognizer(target: self, action: "move:")
        self.addGestureRecognizer(panRecognizer)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func drawRect(rect: CGRect)
    {
        let path = UIBezierPath()
        path.moveToPoint(CGPoint(x: pxStart, y: pyStart))
        path.addLineToPoint(CGPoint(x: pxStart+px, y: pyStart+py))
        UIColor.blueColor().set()
        path.stroke()
    }

    func move(gesture : UIPanGestureRecognizer)
    {
        let state = gesture.state
        switch state
        {
        case .Began:
            pxStart = gesture.locationInView(self).x
            pyStart = gesture.locationInView(self).y
        case .Ended: fallthrough
        case .Changed:
            let translation = gesture.translationInView(self)
            px = translation.x
            py = translation.y
            self.setNeedsDisplay()
        default: break
        }
    }
}

答案 1 :(得分:0)

我将我的功能move更改为此并且有效:

func move(gesture : UIPanGestureRecognizer)
    {
        let state = gesture.state
        switch state
        {
        case .Ended:
            fallthrough
        case .Changed:
            gesture.setTranslation(gesture.locationInView(self), inView: self)
           p = gesture.translationInView(self)
            setNeedsDisplay()
        default:
            break
        }
    }

其中pCGPoint变量,这有助于我在我的UIView上移动一个矩形!永远不要忘记setNeedsDisplay()