iOS 简易的画板



//截取制定view 将其转成UIImage

    class func getAppImage(view:UIView) -> UIImage {

        UIGraphicsBeginImageContext(view.frame.size)

        let context = UIGraphicsGetCurrentContext()

        view.layer.renderInContext(context!)

        let screenimg:UIImage = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        return screenimg

    }


///保存到相册

    func showImage() {

        drawimageView.image = AppDelegate.getAppImage(drawview)

        if let i = drawimageView.image {

        UIImageWriteToSavedPhotosAlbum(i, nil, nil, nil)

        }

    }




class DrawView: UIView {

    //声明一个贝塞尔曲线

    var path:UIBezierPath!

    var previousPoint:CGPoint!

    ///手势是否被激活

    var panGRBool:Bool = false

    

    override init(frame: CGRect) {

        super.init(frame: frame)

        self.backgroundColor = UIColor.whiteColor()

        initGesture()

        initBezierPath()

    }

    

    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    

    //创建一个拖动的手势    

    func initGesture() {

        let pan = UIPanGestureRecognizer(target: self,action: #selector(DrawView.panGRclick(_:)))

        pan.maximumNumberOfTouches = 1

        self.addGestureRecognizer(pan)    

    }

    

    func initBezierPath() {

        path = UIBezierPath()

    }

    


///手势是否所以需要用被激活 来进行初始的判断如果通过手势响应事件

 因为刚开始滑动时不会相应sender.state == UIGestureRecognizerState.Began 

势是否被所以需要用 touches 事件 来进行初始的判断如果通过 panGRBool 来判断否拖动手势被激活

  

    func panGRclick(sender:UIPanGestureRecognizer) {

        let point = sender.locationInView(self)

        let newpoint = getNewPoint(previousPoint, maxpo: point)

        

  

        if sender.state == UIGestureRecognizerState.Began {

            panGRBool = true

            path.addLineToPoint(point)

        }else if sender.state == UIGestureRecognizerState.Changed {

            if let n = newpoint {

    ///画弧线

            path.addQuadCurveToPoint(n, controlPoint: previousPoint)

            }else {

             path.addLineToPoint(point)

            }

        }

        if sender.state == UIGestureRecognizerState.Ended {

            panGRBool = false

        }

        previousPoint = point

        self.setNeedsDisplay()

    }

    

    override func drawRect(rect: CGRect) {

        UIColor.redColor().setStroke()

        path.stroke()

    }

    

    func getNewPoint(minpo:CGPoint?,maxpo:CGPoint) -> CGPoint? {

        if let p = minpo {

        return CGPoint(

            x: (p.x + maxpo.x)/2.0,

            y: (p.y + maxpo.y)/2.0

            )

        }

        return nil

    }

    

   

    

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        if  let touch  = (touches as NSSet).anyObject() {

            let point = touch.locationInView(self)

            path.moveToPoint(point)

        }


    }


    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

        if  let touch  = (touches as NSSet).anyObject() {

            if  !panGRBool {

            let point = touch.locationInView(self)

            path.addLineToPoint(point)

            self.setNeedsDisplay()

            }

        }

    }   

}




你可能感兴趣的:(学习方法)