尝试使用UIPanGesture绘制UIView [英] Trying to draw on a UIView with UIPanGesture
问题描述
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
和py
会更改值.这是较大项目的一部分,所以请告诉我是否有编译错误. >
我的viewController是:
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()
}
}
还可以有人告诉我我到底需要把setNeedsDisplay()
放在哪里吗?我需要在我的视图控制器中使用它吗?我的无用功?谢谢.告诉我您是否需要任何东西.我希望它能在我看来划清界限.就像绘画一样
我将函数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
}
}
其中p
是CGPoint
变量,这帮助我在UIView
上移动了矩形!永远不要忘记setNeedsDisplay()
!
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:
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()
!
这篇关于尝试使用UIPanGesture绘制UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!