Cố gắng làm hoạt hình một hình elip đeo mặt nạ trên một UIView được quy mô chuyển đổi còn lại ở vị trí trung tâm.CABasicAnimation - quy mô biến đổi giữ ở trung tâm
Tôi đã tìm thấy CALayer - CABasicAnimation not scaling around center/anchorPoint, và sau đó bằng cách thêm một tài sản bounds
đến maskLayer
CAShapeLayer
Tuy nhiên, điều này kết thúc với mặt nạ được vị trí ở góc trái với chỉ 1/4 của nó hiển thị. Tôi muốn mặt nạ ở lại giữa màn hình.
@synthesize maskedView;
- (void)viewDidLoad
{
[super viewDidLoad];
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"Next"];
vc.view.frame = CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height);
vc.view.layer.bounds = self.view.layer.bounds;
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
CGRect maskRect = CGRectMake((self.view.frame.size.width/2)-50, (self.view.frame.size.height/2)-50, 100, 100);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, nil, maskRect);
[maskLayer setPath:path];
CGPathRelease(path);
vc.view.layer.mask = maskLayer;
[self.view addSubview:vc.view];
maskedView = vc.view;
[self startAnimation];
}
Animation ...
-(void)startAnimation
{
maskedView.layer.mask.bounds = CGRectMake((self.view.frame.size.width/2)-50, (self.view.frame.size.height/2)-50, 100, 100);
maskedView.layer.mask.anchorPoint = CGPointMake(.5,.5);
maskedView.layer.mask.contentsGravity = @"center";
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.duration = 1.0f;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.fromValue = [NSNumber numberWithFloat:1.0f];
animation.toValue = [NSNumber numberWithFloat:5.0f];
[maskedView.layer.mask addAnimation:animation forKey:@"animateMask"];
}
Cập nhật
tôi dường như đã cố định này với một hình ảnh động thứ hai trên phím position
. Đây có phải là chính xác hoặc là có một cách tốt hơn để làm điều này?
CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"position"];
animation2.duration = 1.0f;
animation2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation2.fromValue = [NSValue valueWithCGPoint:CGPointMake((self.view.frame.size.width/2), (self.view.frame.size.height/2))];
animation2.toValue = [NSValue valueWithCGPoint:CGPointMake((self.view.frame.size.width/2), (self.view.frame.size.height/2))];
[toBeMask.layer.mask addAnimation:animation2 forKey:@"animateMask2"];
Không phải việc mở rộng quy mô ảnh hưởng đến bản dịch bạn làm sau nó? Tôi đang thử nghiệm này (cũng như với biến đổi affine bình thường) và quan điểm luôn luôn kết thúc trên cùng bên trái từ vị trí ban đầu của nó. – Ixx
Điều này đã được làm việc trong trường hợp của tôi 'CABasicAnimation * scale = [CABasicĐộng vật sinh độngWithKeyPath: @" transform "]; CATransform3D tr = CATransform3DIdentity; tr = CATransform3DScale (tr, 3, 3, 1); scale.toValue = [Giá trị NSValueWithCATransform3D: tr]; ' –