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
i changed my function
move
to this and worked:where
p
is aCGPoint
variable and this helped me to move a rect on myUIView
!Never Forget thatsetNeedsDisplay()
!setNeedsDisplay()
notifies your UIView that it needs to be redrawn within the next drawing cycle. In thedrawRect
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. callsetNeedsDisplay()
, and only there.Edit
Here is the class you are searching for I believe