2016-04-13 27 views
0

我正在尝试使用长按手势识别器对UITableView单元格重新排序。当我使用单元表示层的UIGraphicsGetImageFromCurrentImageContext()时,CAAnimations被恢复。UIGraphicsGetImageFromCurrentImageContext()不包括CAAnimations

//代码从压制的UITableViewCell

UIGraphicsBeginImageContextWithOptions(cell.bounds.size, false, 0) 
     if let layer = cell.layer.presentationLayer() { 
      layer.renderInContext(UIGraphicsGetCurrentContext()!) 
     } else { 
      cell.layer.renderInContext(UIGraphicsGetCurrentContext()!) 
     } 
     let cellImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

//代码从UIControl子类使图像

//MARK: Properties 

var switchControl = CAShapeLayer() 
var backgroundLayer = CAShapeLayer() 

// MARK:设置

override func setUpView(){ 
    super.setUpView() 
    didSetUpView = true 
    self.backgroundColor = UIColor.clearColor() 
    let frame = CGRectMake(0, 0, self.bounds.width, self.bounds.height) 
    let radius = self.bounds.height/2 - lineWidth 
    let roundedRectPath = UIBezierPath(roundedRect:CGRectInset(frame, lineWidth, lineWidth) , cornerRadius:radius) 
    backgroundLayer.fillColor = stateToFillColor(isOn) 
    backgroundLayer.strokeColor = lineColor.CGColor 
    backgroundLayer.lineWidth = lineWidth 
    backgroundLayer.path = roundedRectPath.CGPath 
    self.layer.addSublayer(backgroundLayer) 

    let innerLineWidth = self.bounds.height - lineWidth*3 + 1 
    let switchControlPath = UIBezierPath() 
    switchControlPath.moveToPoint(CGPointMake(lineWidth, 0)) 
    switchControlPath.addLineToPoint(CGPointMake(self.bounds.width - 2 * lineWidth - innerLineWidth + 1, 0)) 
    var point = backgroundLayer.position 
    point.y += (radius + lineWidth) 
    point.x += (radius) 
    switchControl.position = point 
    switchControl.path = switchControlPath.CGPath 
    switchControl.lineCap  = kCALineCapRound 
    switchControl.fillColor = nil 
    switchControl.strokeColor = circleColor.CGColor 
    switchControl.lineWidth = innerLineWidth 
    switchControl.strokeEnd = 0.0001 
    self.layer.addSublayer(switchControl) 
    changeValueAnimate(isOn,duration: animateDuration) 
} 

//MARK: - Animate 

func changeValueAnimate(turnOn:Bool, duration:Double){ 

    let times = [0,0.49,0.51,1] 

    let switchControlStrokeStartAnim  = CAKeyframeAnimation(keyPath:"strokeStart") 
    switchControlStrokeStartAnim.values = turnOn ? [0,0,0,1] : [1,0,0, 0] 
    switchControlStrokeStartAnim.keyTimes = times 
    switchControlStrokeStartAnim.duration = duration 
    switchControlStrokeStartAnim.removedOnCompletion = true 

    let switchControlStrokeEndAnim  = CAKeyframeAnimation(keyPath:"strokeEnd") 
    switchControlStrokeEndAnim.values = turnOn ? [0,1,1,1] : [1,1,1,0] 
    switchControlStrokeEndAnim.keyTimes = times 
    switchControlStrokeEndAnim.duration = duration 
    switchControlStrokeEndAnim.removedOnCompletion = true 


    let backgroundFillColorAnim  = CAKeyframeAnimation(keyPath:"fillColor") 
    backgroundFillColorAnim.values = [stateToFillColor(!turnOn), 
             stateToFillColor(!turnOn), 
             stateToFillColor(turnOn), 
             stateToFillColor(turnOn)] 
    backgroundFillColorAnim.keyTimes = [0,0.5,0.51,1] 
    backgroundFillColorAnim.duration = duration 
    backgroundFillColorAnim.fillMode = kCAFillModeForwards 
    backgroundFillColorAnim.removedOnCompletion = false 


    if rotateWhenValueChange{ 
     UIView.animateWithDuration(duration, animations: {() -> Void in 
      self.transform = CGAffineTransformRotate(self.transform, CGFloat(M_PI)) 
     }) 
    } 

    UIView.animateWithDuration(duration/3, animations: { 
     self.transform = CGAffineTransformMakeScale(1.15, 1.15) 
    }) { (animate: Bool) in 
     UIView.animateWithDuration(duration/3 * 2, animations: { 
      self.transform = CGAffineTransformMakeScale(1.0, 1.0) 
     }) 
    } 

    let switchControlChangeStateAnim : CAAnimationGroup = CAAnimationGroup() 
    switchControlChangeStateAnim.animations = [switchControlStrokeStartAnim,switchControlStrokeEndAnim] 
    switchControlChangeStateAnim.fillMode = kCAFillModeForwards 
    switchControlChangeStateAnim.removedOnCompletion = false 
    switchControlChangeStateAnim.duration = duration 

    let animateKey = turnOn ? "TurnOn" : "TurnOff" 
    switchControl.addAnimation(switchControlChangeStateAnim, forKey: animateKey) 
    backgroundLayer.addAnimation(backgroundFillColorAnim, forKey: "Color") 

} 

回答

0

解决问题

let cellImage = cell.snapshotViewAfterScreenUpdates(false)