2017-02-19 9 views
0

私はユーザーが何かを描くことができるビューで画面を作ろうとしています。私はそのようなコードでカスタムビューを作成しました:UISplitViewControllerの描画に奇妙なバグがあります

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    swiped = false 
    if let touch = touches.first { 
     lastPoint = touch.location(in: imageView) 
    } 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    swiped = true 
    if let touch = touches.first { 
     let currentPoint = touch.location(in: imageView) 
     drawLine(fromPoint: lastPoint, toPoint: currentPoint) 

     lastPoint = currentPoint 
    } 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if !swiped { 
     // draw a single point 
     drawLine(fromPoint: lastPoint, toPoint: lastPoint) 
    } 

と私はビューがビューコントローラですべてが正常であることを示しているとき

func drawLine(fromPoint: CGPoint, toPoint: CGPoint) { 
    UIGraphicsBeginImageContext(imageView.frame.size) 
    let context = UIGraphicsGetCurrentContext() 
    imageView.image?.draw(in: CGRect(x: 0, y: 0, width: imageView.frame.size.width, height: imageView.frame.size.height)) 

    context?.move(to: fromPoint) 
    context?.addLine(to: toPoint) 

    context?.setLineCap(.round) 
    context?.setLineWidth(lineWidth) 
    context?.setStrokeColor(lineColor.cgColor) 

    context?.strokePath() 

    imageView.image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
} 

を描画機能:

enter image description here

が、私が示しますUISplitViewControllerの詳細ビューで、ユーザーは描画を続けますが、既に描画されたイメージの一部が移動してフェードアウトします。 enter image description here

私はそのような行動

は、誰もがそのことについてどんな考えを持っているされていますが生成されているもの見当がつかないウェブでどのようなバグは約何かを見つけることができませんか?

あなたがそのバグ再現できるサンプルプロジェクトです:スプリットビューコントローラの実際のプロジェクトだけでなく、マスタービューで、ところで https://github.com/fizzy871/DrawingBug

を、しかし、ナビゲーションバーは、それが動作することが判明すぎ

答えて

1

を描くに影響小数点以下の桁数のimageViewフレームがあるためです。

enter image description here

私はちょうど2と問題解決によってコンテキストを描く乗じ:

func drawLine(fromPoint fromPoint: CGPoint, toPoint: CGPoint) { 
    // multiply to avoid problems when imageView frame value is XX.5 
    let fixedFrameForDrawing = CGRect(x: 0, y: 0, width: imageView.frame.size.width*2, height: imageView.frame.size.height*2) 
    let point1 = CGPoint(x: fromPoint.x*2, y: fromPoint.y*2) 
    let point2 = CGPoint(x: toPoint.x*2, y: toPoint.y*2) 
    UIGraphicsBeginImageContext(fixedFrameForDrawing.size) 
    if let context = UIGraphicsGetCurrentContext() { 
     imageView.image?.draw(in: fixedFrameForDrawing) 

     context.move(to: point1) 
     context.addLine(to: point2) 

     context.setLineCap(.round) 
     context.setLineWidth(lineWidth*2) 
     context.setStrokeColor(lineColor.cgColor) 

     context.strokePath() 

     let imageFromContext = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     imageView.image = imageFromContext 
    } 
+0

これはちょうど私のアプリ、クレイジーなものに起こりました。今、私はフレームサイズを切り捨て、それは正常に動作します。解り覚えておかなければならないでしょう。 –

関連する問題