UIGraphicsGetImageFromCurrentImageContext()不包括CAAnimations

时间:2016-04-13 21:38:30

标签: ios swift uitableview caanimation

我试图使用长按手势识别器重新排序UITableView单元格。当我从单元格表示层使用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")

}

1 个答案:

答案 0 :(得分:0)

已解决的问题

让cellImage = cell.snapshotViewAfterScreenUpdates(false)