2012-06-19 43 views
5

Tôi đang cố gắng tạo hiệu ứng cho chiều rộng của hình chữ nhật tròn, vấn đề là khi đi từ chiều rộng lớn hơn đến chiều rộng mỏng hơn, hoạt ảnh sẽ thực hiện "nhảy dễ dàng sai".(iOS) Làm thế nào để animate hình chữ nhật tròn với shapeLayer?

Dưới đây là các mã:

shapeLayer = [CAShapeLayer layer]; 
shapeRect = CGRectMake(0.0f, 0.0f, 150.0f, 200.0f); 
[shapeLayer setBounds:shapeRect]; 
[shapeLayer setPosition:CGPointMake(iniPosX, 80.0f)]; 
[shapeLayer setFillColor:[[UIColor blackColor] CGColor]]; 
[shapeLayer setStrokeColor:[[UIColor clearColor] CGColor]]; 
[shapeLayer setLineWidth:1.0f]; 
[shapeLayer setLineJoin:kCALineJoinRound]; 
[shapeLayer setOpacity:0.2]; 
path = [UIBezierPath bezierPathWithRoundedRect:shapeRect cornerRadius:15.0]; 
[shapeLayer setPath:path.CGPath]; 
[self.layer addSublayer:shapeLayer]; 

Và khi tôi bắt đầu hoạt hình:

- (void)adjustSelectorToPosAndSize:(float)posX andWidth:(float)width 
{ 
    shapeRect = CGRectMake(0.0f, 0.0f, width, 200.0f); 
    [shapeLayer setBounds:shapeRect]; 
    [shapeLayer setPosition:CGPointMake(posX, 80.0f)]; 
    path = [UIBezierPath bezierPathWithRoundedRect:shapeRect cornerRadius:15.0]; 
    [shapeLayer setPath:path.CGPath]; 
} 

Tôi đang làm gì sai?

+0

Cảm ơn sự điều chỉnh của Alexis. – murb83

Trả lời

3

Làm điều này với một CAShapeLayer có vẻ quá phức tạp với tôi nếu bạn chỉ sử dụng nó cho một hình chữ nhật tròn. Tại sao không chỉ đơn giản sử dụng một CALayer với the cornerRadius properly thiết lập?

Hoạt hình khung bằng gócĐộ cao sẽ hoạt động tốt.

myLayer = [CALayer layer]; 
shapeRect = CGRectMake(0.0f, 0.0f, 150.0f, 200.0f); 
[myLayer setBounds:shapeRect]; 
[myLayer setPosition:CGPointMake(iniPosX, 80.0f)]; 
[myLayer setBackgroundColor:[[UIColor blackColor] CGColor]]; 
[myLayer setBorderColor:[[UIColor clearColor] CGColor]]; 
[myLayer setBorderWidth:1.0f]; 
[myLayer setOpacity:0.2]; 
[myLayer setCornerRadius:15.0]; 
[self.layer addSublayer:myLayer]; 

Animating nó được thực hiện bằng cách đơn giản chaining khung (không phải là con đường)

- (void)adjustSelectorToPosAndSize:(float)posX andWidth:(float)width 
{ 
    shapeRect = CGRectMake(0.0f, 0.0f, width, 200.0f); 
    [myLayer setBounds:shapeRect]; 
    [myLayer setPosition:CGPointMake(posX, 80.0f)]; 
} 

Chú ý:

Một số các tính chất đổi tên như fillColor trở thành backgroundColorstrokeColorlineWidth đã trở thành borderColorborderWidth v.v.

+0

Cảm ơn! Nhưng không làm một hoạt hình, tức thời. – murb83

+0

Tôi vừa thử nó. Nó hoạt hình bằng cách sử dụng hoạt ảnh mặc định là 0,3. Bạn có làm gì khác với lớp không? –

+0

Nếu bạn muốn tùy chỉnh hình động bạn cần thực hiện một cách rõ ràng –

3

Mặc dù sở hữu đường CAShapeLayer là animatable, nó không làm như vậy mặc nhiên như giới hạn và vị trí, bạn phải thực hiện một hình ảnh động rõ ràng:

CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath: @"path"]; 
anim.fromValue = (id) fromPath.CGPath; 
anim.toValue = (id) toPath.CGPath; 
anim.duration = 1.0; 
[layer addAnimation: anim forKey: @“resize”]; 

Vì bạn có thể muốn cả ba hình ảnh động để xảy ra cùng nhau, bạn có thể làm cho họ tất cả rõ ràng và nhóm lại với nhau:

CABasicAnimation* anim1 = [CABasicAnimation animationWithKeyPath: @"position"]; 
anim1.fromValue = [NSValue valueWithCGPoint: fromPosition]; 
anim1.toValue = [NSValue valueWithCGPoint: toPosition]; 
anim1.duration = 1.0; 

CABasicAnimation* anim2 = [CABasicAnimation animationWithKeyPath: @"bounds"]; 
anim2.fromValue = [NSValue valueWithCGRect: fromBounds]; 
anim2.toValue = [NSValue valueWithCGRect: toBounds]; 
anim2.duration = 1.0; 

CABasicAnimation* anim3 = [CABasicAnimation animationWithKeyPath: @"path"]; 
anim3.fromValue = (id) fromPath.CGPath; 
anim3.toValue = (id) toPath.CGPath; 
anim3.duration = 1.0; 

CAAnimationGroup* animG = [CAAnimationGroup animation]; 
animG.animations = [NSArray arrayWithObjects: anim1, anim2, anim3, nil]; 
animG.duration = 1.0; 
[layer addAnimation: animG forKey:@"resize"]; 

Nếu hình của bạn không có nội dung hoặc trẻ em sau đó bạn có thể sử dụng một CALayer thay vì:

CALayer* rectLayer = [CALayer layer]; 
rectLayer.masksToBounds = YES; 
rectLayer.bounds = startBounds; 
rectLayer.backgroundColor = [[UIColor blackColor] CGColor]; 
rectLayer.cornerRadius = 15.0; 
[self.layer addSublayer: rectLayer]; 

// Then later implicitly animate the bounds: 
rectLayer.bounds = newBounds; 
+0

Cảm ơn !! Tôi đang thử nghiệm mã của bạn, tôi sẽ cho bạn biết khi bạn hoàn thành thử nghiệm. – murb83

+0

Tôi xin lỗi, nhưng không hoạt động tốt, tôi nghĩ rằng tôi đang làm điều gì đó sai, nhưng lớp không di chuyển đến nơi tôi muốn, tôi nghĩ rằng điểm tham chiếu là khác nhau khi tôi làm các hình ảnh động. Xin lỗi tiếng anh của tôi không tốt. Cách mà bạn sử dụng hình ảnh động, thực sự là sở thích của tôi, bạn đã mở cho tôi một cách mới cho tôi trong hoạt ảnh nhóm. ^^ – murb83

Các vấn đề liên quan