如何將CAShapelayer
從圓形變爲方形(或相反)CABasicAnimation
?CAShapelayer如何通過動畫從圓形變爲方形(反之亦然)?
回答
各地做了一些工作之後,我已經結束與此
首先爲方形和圓形路徑以及CAShapeLayer
對象創建全局對象UIBezierPath
。現在
@implementation Demo{
CAShapeLayer *shapeLayer;
UIBezierPath *sqPath;
UIBezierPath *crclePath;
}
,自定義shapeLayer
並添加viewDidLoad
到層像下面。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
shapeLayer = [CAShapeLayer layer];
shapeLayer.strokeColor = [[UIColor redColor] CGColor];
shapeLayer.lineWidth = 2;
shapeLayer.lineJoin = kCALineCapRound;
sqPath = [self makeSquare:self.view.center squareWidth:200];
shapeLayer.path = [sqPath CGPath];
crclePath = [self makeCircle:self.view.center radius:100];
[self.view.layer addSublayer:shapeLayer];
}
下面是方法,這將使完美的廣場
-(UIBezierPath*)makeSquare:(CGPoint)centrePoint squareWidth:(float)gain{
float x=centrePoint.x-(gain/2);
float y=centrePoint.y-(gain/2);
UIBezierPath *apath = [UIBezierPath bezierPath];
[apath moveToPoint:CGPointMake(x,y)];
[apath addLineToPoint:apath.currentPoint];
[apath addLineToPoint:CGPointMake(x+gain,y)];
[apath addLineToPoint:apath.currentPoint];
[apath addLineToPoint:CGPointMake(x+gain,y+gain)];
[apath addLineToPoint:apath.currentPoint];
[apath addLineToPoint:CGPointMake(x,y+gain)];
[apath addLineToPoint:apath.currentPoint];
[apath closePath];
return apath;
}
下面是方法有助於使圓
- (UIBezierPath *)makeCircle:(CGPoint)center radius:(CGFloat)radius
{
UIBezierPath *circlePath = [UIBezierPath bezierPath];
[circlePath addArcWithCenter:center radius:radius startAngle:-M_PI endAngle:-M_PI/2 clockwise:YES];
[circlePath addArcWithCenter:center radius:radius startAngle:-M_PI/2 endAngle:0 clockwise:YES];
[circlePath addArcWithCenter:center radius:radius startAngle:0 endAngle:M_PI/2 clockwise:YES];
[circlePath addArcWithCenter:center radius:radius startAngle:M_PI/2 endAngle:M_PI clockwise:YES];
[circlePath addArcWithCenter:center radius:radius startAngle:M_PI endAngle:-M_PI clockwise:YES];
[circlePath closePath];
return circlePath;
}
最後,動畫的btnDone
其中使用CABasicAnimation
動畫轉換操作完成從正方形到圓形或反之亦然。
- (IBAction)btnDone:(id)sender {
btnDowrk.selected=!btnDowrk.selected;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.duration = 1;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
if (btnDowrk.selected) {
animation.fromValue = (__bridge id)(sqPath.CGPath);
animation.toValue = (__bridge id)(crclePath.CGPath);
shapeLayer.path = crclePath.CGPath;
}
else{
animation.fromValue = (__bridge id)(crclePath.CGPath);
animation.toValue = (__bridge id)(sqPath.CGPath);
shapeLayer.path = sqPath.CGPath;
}
[shapeLayer addAnimation:animation forKey:@"animatePath"];
}
讓我知道這對你有用嗎? –
這是轉變廣場,圓形和副verca,但在你的答案 –
不完全顯示,但我想你一樣在〔實施例 –
你可以和cornerRadius
一起玩。例如,
UIView *testView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 50, 50)];
CAShapeLayer *layer = [[CAShapeLayer alloc]initWithLayer:testView.layer]; // this is square layer
layer.cornerRadius = 25.0; // this is round layer
layer.cornerRadius = 0.0; // this is again square layer
您可以直接從輪切換查看平方和圓像上面的方法!
下面是創建循環路徑:
- (UIBezierPath *)circlePathWithCenter:(CGPoint)center radius:(CGFloat)radius
{
UIBezierPath *circlePath = [UIBezierPath bezierPath];
[circlePath addArcWithCenter:center radius:radius startAngle:0 endAngle:M_PI/2 clockwise:YES];
[circlePath addArcWithCenter:center radius:radius startAngle:M_PI/2 endAngle:M_PI clockwise:YES];
[circlePath addArcWithCenter:center radius:radius startAngle:M_PI endAngle:3*M_PI/2 clockwise:YES];
[circlePath addArcWithCenter:center radius:radius startAngle:3*M_PI/2 endAngle:M_PI clockwise:YES];
[circlePath closePath];
return circlePath;
}
下面是方形路徑:
- (UIBezierPath *)squarePathWithCenter:(CGPoint)center size:(CGFloat)size
{
CGFloat startX = center.x-size/2;
CGFloat startY = center.y-size/2;
UIBezierPath *squarePath = [UIBezierPath bezierPath];
[squarePath moveToPoint:CGPointMake(startX, startY)];
[squarePath addLineToPoint:CGPointMake(startX+size, startY)];
[squarePath addLineToPoint:CGPointMake(startX+size, startY+size)];
[squarePath addLineToPoint:CGPointMake(startX, startY+size)];
[squarePath closePath];
return squarePath;
}
動畫部件
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.duration = 1;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.fromValue = (__bridge id)(self.stateLayer.path);
animation.toValue = (__bridge id)(self.stopPath.CGPath);
self.stateLayer.path = self.stopPath.CGPath;
[self.stateLayer addAnimation:animation forKey:@"animatePath"];
- 1. 圓形動畫
- 2. 圓形動畫
- 3. 如何從上到下過渡活動,反之亦然?
- 4. 從圓形畫出圓形和線段
- 5. 動畫CAShapeLayer通路改變
- 6. 將圓角矩形變換爲圓形
- 7. 動畫效果的顏色變化從頂部到底部,反之亦然
- 8. JavaFX用於圓形動畫的矩形
- 9. 圓形動畫css3和jquery
- 10. jQuery的「圓形狀」動畫
- 11. Jquery動畫圓形導航
- 12. 如何從左到右移動uiview,反之亦然
- 13. 何時通過指針使用數組,或反之亦然
- 14. 何時使用char a []通過char p *,反之亦然?
- 15. 帆布反向畫圓角矩形?
- 16. 沿着圓形的Qt動畫橢圓
- 17. XAML到Html,反之亦然
- 18. NSImage cv :: Mat,反之亦然
- 19. hadoop mapreduce反之亦然
- 20. 從Python調用Jython(或反之亦然)
- 21. 從MinGW使用msysGit,反之亦然
- 22. 從JavaScript調用VBScript或反之亦然?
- 23. 將JavaUtilDate轉換爲NSDate,反之亦然?
- 24. 將lonlat轉換爲xyz,反之亦然
- 25. 將UIImage轉換爲NSString(反之亦然)
- 26. DTO轉換爲實體,反之亦然
- 27. 將ArrayBuffer轉換爲Array,反之亦然
- 28. 將HTML轉換爲Xliff,反之亦然
- 29. Android ImageView變身:從方形到圓形(解決方案更新)
- 30. iOS:帶四個圓形按鈕的動畫圓形
哪個部分很難?一般的動畫技術,或者如何掩飾形狀的細節? – Avi
我想從圓形變爲方形先生 –
拐角半徑向下調整爲零。 – Avi