XCode7画图-touches不支持的解决方法

因为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)
        
        
    }
 

}

效果:

QQ20160418-0

重点就是:

        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)

 

Tagged on: , ,

发表评论

电子邮件地址不会被公开。 必填项已用*标注