从其他类swift重绘drawRect

时间:2015-09-19 16:18:52

标签: ios swift uiviewcontroller drawrect

我试图从UIViewcontroller调用DrawLines类中的drawRect(想要从UIViewController中设置tappoint变量)。我希望DrawLines类绘制的线在用户平移时重绘。我想出了以下产生错误的信息:

的UIViewController

@IBAction func pan(sender: UIPanGestureRecognizer) {
  DrawLines.tappoint = sender.locationInView(GraphView?()) // trying to set the point
  DrawLines.setNeedsDisplay() //trying to redraw display
 }

DrawLines

import UIKit

class DrawLines: UIView{

 required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
 }


 var tappoint: CGPoint!
 var touched = false


 override func drawRect(rect: CGRect) {

    if(touched){
        var xAxispoint: CGPoint = CGPoint(x: tappoint.x,y: 613)

        var vertical = UIBezierPath()
        vertical.moveToPoint(tappoint)
        vertical.addLineToPoint(xAxispoint)
        UIColor.blackColor().setStroke()
        vertical.stroke()

        var yAxispoint: CGPoint = CGPoint(x: 8, y: tappoint.y)
        var horizontal = UIBezierPath()
        horizontal.moveToPoint(tappoint)
        horizontal.addLineToPoint(yAxispoint)
        UIColor.blackColor().setStroke()
        horizontal.stroke()


    }
 }

}

0 个答案:

没有答案