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

            }

        }

    }   

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值