因为Xcode升级,最新版不支持这个touches方法了,因此,需要在之前的基础上做一些调整:
// // DrawBoardView.swift // R06DrawShapes // // Created by SteveKing on 16/4/18. // Copyright © 2016年 rancho. All rights reserved. // import UIKit class DrawBoardView: UIView { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } var path = CGPathCreateMutable() override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { // let p = touches.anyObject.localtionInView(self) // let p = (touches as NSSet).anyObject()?.locationInView(self) // CGPathMoveToPoint(path, nil, p.x, p.y) let touch:UITouch = touches.first as UITouch! let tmp_point:CGPoint = touch.locationInView(touch.view) CGPathMoveToPoint(path, nil, tmp_point.x, tmp_point.y) } override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { // let p = touches.anyObject.localtionInView(self) // let p = (touches as NSSet).anyObject()?.locationInView(self)// // CGPathAddLineToPoint(path, nil, p!.x, p!.y) let touch:UITouch = touches.first as UITouch! let tmp_point:CGPoint = touch.locationInView(touch.view) CGPathAddLineToPoint(path, nil, tmp_point.x, tmp_point.y) self.setNeedsDisplay() } // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func drawRect(rect: CGRect) { // Drawing code // let context = UIGraphicsGetCurrentContext() // // let path = CGPathCreateMutable() // // CGPathMoveToPoint(path, nil, 100, 100) // CGPathAddLineToPoint(path, nil, 200, 200) // CGContextAddPath(context, path) // CGContextStrokePath(context) let context = UIGraphicsGetCurrentContext() CGContextAddPath(context, path) CGContextStrokePath(context) } }
效果:
重点就是:
let touch:UITouch = touches.first as UITouch! let tmp_point:CGPoint = touch.locationInView(touch.view) CGPathMoveToPoint(path, nil, tmp_point.x, tmp_point.y)
和:
let touch:UITouch = touches.first as UITouch! let tmp_point:CGPoint = touch.locationInView(touch.view) CGPathAddLineToPoint(path, nil, tmp_point.x, tmp_point.y)