Trying to draw on a UIView with UIPanGesture

2019-03-04 02:59发布

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()
        }
}

This is the code i have in my view. What am i missing and it doesn't show up on my screen?it does recognise my movement since px and py change values.This is part of bigger project so tell me if there are any compilling errors..

my viewController is :

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()
            }
        }

Also can someone tell me where exactly do i need to put that setNeedsDisplay()?? do i need to have it in my view controller? my override drawract? thanks. Tell me if you need anything. I want it to draw the line in my view. like painting does

标签: ios swift uiview
2条回答
叛逆
2楼-- · 2019-03-04 03:25

i changed my function move to this and worked:

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
        }
    }

where p is a CGPoint variable and this helped me to move a rect on my UIView!Never Forget that setNeedsDisplay()!

查看更多
时光不老,我们不散
3楼-- · 2019-03-04 03:29

setNeedsDisplay() notifies your UIView that it needs to be redrawn within the next drawing cycle. In the drawRect function, you specify what should be drawn on the UIView. You want to change the appearance of your UIView everytime you have the Changed state in your GestureRecognizer, so that's also where you need to notify your view to be redrawn, i.e. call setNeedsDisplay(), and only there.

Edit

Here is the class you are searching for I believe

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
        }
    }
}
查看更多
登录 后发表回答