2016-01-29 1 views
1

Je suis novice et j'ai été frustré par ce problème. Ce que j'essaie de faire est très simple: tracez une ligne droite au centre de l'écran et animez-la. Cela semble très simple, non? Ouais .. alors c'est ce que j'ai en ce moment.Animation de dessin au trait simple dans Swift

import UIKit 

class Drawings: UIView { 


    override func drawRect(rect: CGRect) { 

     let screensize: CGRect = UIScreen.mainScreen().bounds 
     let screenWidth = screensize.width 
     let screenHeight = screensize.height 

     //context 
     let context = UIGraphicsGetCurrentContext() 
     CGContextSetLineWidth(context, 7.0) 
     CGContextSetStrokeColorWithColor(context, UIColor.whiteColor().CGColor) 

     //path 
     CGContextMoveToPoint(context, screenWidth, screenHeight * 0.5) 
     CGContextAddLineToPoint(context, screenWidth * 0.5, screenHeight * 0.5) 

     CGContextSetLineCap(context, CGLineCap.Round) 

     //draw 
     CGContextStrokePath(context) 

    } 

} 

La raison pour laquelle je demande est que j'ai trouvé que je ne peux pas l'animer, car il est en fonction drawRect. Est-ce vrai? Besoin d'explications de votre part. Merci!

Image

+1

Vous ne devriez pas accéder à l'écran ou à sa taille. Utilisez 'self.bounds' pour déterminer où dessiner (et ne dessinez pas en dehors de ces limites). Le 'drawRect:' est utilisé "une fois" (vous pouvez le faire redessiner) pour effectuer un dessin pour une vue, et le système de coordonnées local est en vigueur. – bshirley

+1

Si vous n'avez pas vu Apple [iOS Drawing Concepts] (https://developer.apple.com/library/ios/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GraphicsDrawingOverview/GraphicsDrawingOverview.html) – bshirley

+0

Note (sans importance à ce stade)): le paramètre 'rect' est généralement équivalent aux bornes. Il peut s'agir d'un sous-ensemble qui dit «vous n'avez besoin que de * dessiner une partie du cadre» Il est courant de tout redessiner (à moins d'avoir une vue complexe et coûteuse) – bshirley

Répondre

5

Vous ne devriez pas dessiner des formes dans drawRect comme ça, surtout si vous allez animer. A la place, créez un CAShapeLayer, et vous pouvez utiliser Core Animation pour l'animer.

// Create the path using UIBezierPath. 
// Position can start at 0,0 because we'll move it using the layer 
let linePath = UIBezierPath() 
linePath.moveToPoint(CGPoint(x: 0, y: 0)) 
linePath.addLineToPoint(CGPoint(x: CGRectGetMidX(frame), y: 0)) 

// Create the shape layer that will draw the path 
let lineLayer = CAShapeLayer() 

// set the bounds to the size of the path. 
// You could also set to the size of your view, 
// but if i know the size of my shape ahead of time, 
// I like to set it to be the same. 
lineLayer.bounds = linePath.bounds 

// give it the same position as our view's layer. 
// layers have an anchor point of 0.5, 0.5 (their center) 
// so this will put the layer in the center of the view. 
lineLayer.position = layer.position 

// finally, set up the shape properties. set the path and stroke etc 
lineLayer.path = linePath.CGPath 
lineLayer.strokeColor = UIColor.whiteColor().CGColor 
lineLayer.lineWidth = 4 

// add it to your view's layer and you're done! 
layer.addSublayer(lineLayer) 

Vous pouvez maintenant animer des propriétés sur lineLayer en utilisant Core Animation.

+1

Merci beaucoup! –