2015-08-28 80 views
0

我有一個UIImageView,它從正方形動畫來圈與此代碼:動畫的CALayer陰影圓圈(或圓角)

CABasicAnimation *circleShapeAnimation = [CABasicAnimation animationWithKeyPath:@"cornerRadius"]; 
circleShapeAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; 
circleShapeAnimation.toValue = [NSNumber numberWithFloat:cornerRadius]; 

我也有一個單獨的的UIView(其的backgroundColor是clearColor),我專門用來實現動畫陰影。動畫一切正常,除了形狀動畫:

CABasicAnimation* shadowShapeAnimation = [CABasicAnimation animationWithKeyPath: @"shadowPath"]; 
shadowShapeAnimation.fromValue = (id)[UIBezierPath bezierPathWithRoundedRect: cell.placeImage.bounds cornerRadius:0.0f].CGPath; 
shadowShapeAnimation.toValue = (id)[UIBezierPath bezierPathWithRoundedRect: cell.placeImage.bounds cornerRadius:cornerRadius].CGPath; 

其中,簡單地說,看起來比我早上宿醉醜陋。

anim1

anim2

anim3

我想實現圓角的一個不錯的,流暢的動畫效果。任何想法如何?

回答

0

的關鍵是使用這些方法:

func circlePathWithCenter(center: CGPoint, radius: CGFloat) -> UIBezierPath { 
    let circlePath = UIBezierPath() 
    circlePath.addArcWithCenter(center, radius: radius, startAngle: -CGFloat(M_PI), endAngle: -CGFloat(M_PI/2), clockwise: true) 
    circlePath.addArcWithCenter(center, radius: radius, startAngle: -CGFloat(M_PI/2), endAngle: 0, clockwise: true) 
    circlePath.addArcWithCenter(center, radius: radius, startAngle: 0, endAngle: CGFloat(M_PI/2), clockwise: true) 
    circlePath.addArcWithCenter(center, radius: radius, startAngle: CGFloat(M_PI/2), endAngle: CGFloat(M_PI), clockwise: true) 
    circlePath.closePath() 
    return circlePath 
} 

func squarePathWithCenter(center: CGPoint, side: CGFloat) -> UIBezierPath { 
    let squarePath = UIBezierPath() 
    let startX = center.x - side/2 
    let startY = center.y - side/2 
    squarePath.moveToPoint(CGPoint(x: startX, y: startY)) 
    squarePath.addLineToPoint(squarePath.currentPoint) 
    squarePath.addLineToPoint(CGPoint(x: startX + side, y: startY)) 
    squarePath.addLineToPoint(squarePath.currentPoint) 
    squarePath.addLineToPoint(CGPoint(x: startX + side, y: startY + side)) 
    squarePath.addLineToPoint(squarePath.currentPoint) 
    squarePath.addLineToPoint(CGPoint(x: startX, y: startY + side)) 
    squarePath.addLineToPoint(squarePath.currentPoint) 
    squarePath.closePath() 
    return squarePath 
} 

這裏找到:iOS animate/morph shape from circle to square