如何將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. 從圓形到方形的iOS動畫/變形形狀
- 2. 通過PHP變量,反之亦然
- 3. CGAffineTransformMakeScale圓形UIView動畫爲正方形
- 4. 將一個CAShapeLayer圓動畫成一個圓角三角形
- 5. 圓形動畫
- 6. 圓形動畫
- 7. 圓形動畫
- 8. 如何從上到下過渡活動,反之亦然?
- 9. 從上到下動畫UIview從上到下,反之亦然
- 10. 從圓形畫出圓形和線段
- 11. 動畫CAShapeLayer通路改變
- 12. 將圓角矩形變換爲圓形
- 13. 從Bigtable到GCS(反之亦然)通過Dataflow
- 14. 通過LDAP從域名獲取UPN別名,反之亦然
- 15. 動畫效果的顏色變化從頂部到底部,反之亦然
- 16. 如何在iphone或xcode中使用CAShapeLayer製作多色圓形動畫?
- 17. 動畫Android設備上查看Z方向(從後轉發,反之亦然)
- 18. 如何以編程方式將LTR改爲RTL,反之亦然?
- 19. JavaFX用於圓形動畫的矩形
- 20. 如何從左到右移動uiview,反之亦然
- 21. jQuery的「圓形狀」動畫
- 22. Jquery動畫圓形導航
- 23. 圓形車輪動畫
- 24. 圓形動畫css3和jquery
- 25. 如何從十六進制翻譯爲摘要,反之亦然?
- 26. 如何將XML從XML轉換爲Java,反之亦然?
- 27. 何時通過指針使用數組,或反之亦然
- 28. 何時通過Vue指令使用Vue mixin,反之亦然?
- 29. 何時使用char a []通過char p *,反之亦然?
- 30. 如何通過installscript在InstallShield中將現有服務的模式從自動更改爲手動,反之亦然?
哪個部分很難?一般的動畫技術,或者如何掩飾形狀的細節? – Avi
我想從圓形變爲方形先生 –
拐角半徑向下調整爲零。 – Avi