Tôi đang cố gắng triển khai hoạt ảnh lật được sử dụng trong trò chơi trên bảng như ứng dụng trên iPhone. Các hình ảnh động được cho là trông giống như một mảnh trò chơi mà quay và thay đổi màu sắc của mặt sau của nó (loại giống như một Reversi piece). Tôi đã quản lý để tạo ra một hình ảnh động lật các mảnh xung quanh trục trực giao của nó, nhưng khi tôi cố gắng lật nó xung quanh một trục chéo bằng cách thay đổi luân phiên xung quanh trục z hình ảnh thực tế cũng được xoay (không ngạc nhiên). Thay vào đó, tôi muốn xoay hình ảnh "nguyên trạng" quanh trục chéo.Làm thế nào để tôi xoay một CALayer xung quanh một đường chéo?
Tôi đã cố thay đổi layer.sublayerTransform
nhưng không thành công.
Đây là triển khai hiện tại của tôi. Nó hoạt động bằng cách thực hiện một mẹo để giải quyết vấn đề nhận được một hình ảnh được nhân đôi ở cuối hoạt ảnh. Giải pháp là không thực sự xoay lớp 180 độ, thay vào đó nó xoay nó 90 độ, thay đổi hình ảnh và sau đó xoay nó trở lại.
Phiên bản cuối cùng: Dựa trên đề xuất Lorenzos để tạo hoạt ảnh có khóa rời rạc và tính ma trận chuyển đổi cho mỗi khung. Phiên bản này thay vì cố gắng ước tính số lượng khung "hướng dẫn" cần thiết dựa trên kích thước lớp và sau đó sử dụng hoạt ảnh có khóa tuyến tính. Phiên bản này quay với một góc tùy ý để xoay quanh đường chéo sử dụng góc 45 độ.
sử dụng Ví dụ:
[someclass flipLayer:layer image:image angle:M_PI/4]
Thực hiện:
- (void)animationDidStop:(CAAnimationGroup *)animation
finished:(BOOL)finished {
CALayer *layer = [animation valueForKey:@"layer"];
if([[animation valueForKey:@"name"] isEqual:@"fadeAnimation"]) {
/* code for another animation */
} else if([[animation valueForKey:@"name"] isEqual:@"flipAnimation"]) {
layer.contents = [animation valueForKey:@"image"];
}
[layer removeAllAnimations];
}
- (void)flipLayer:(CALayer *)layer
image:(CGImageRef)image
angle:(float)angle {
const float duration = 0.5f;
CAKeyframeAnimation *rotate = [CAKeyframeAnimation
animationWithKeyPath:@"transform"];
NSMutableArray *values = [[[NSMutableArray alloc] init] autorelease];
NSMutableArray *times = [[[NSMutableArray alloc] init] autorelease];
/* bigger layers need more "guiding" values */
int frames = MAX(layer.bounds.size.width, layer.bounds.size.height)/2;
int i;
for (i = 0; i < frames; i++) {
/* create a scale value going from 1.0 to 0.1 to 1.0 */
float scale = MAX(fabs((float)(frames-i*2)/(frames - 1)), 0.1);
CGAffineTransform t1, t2, t3;
t1 = CGAffineTransformMakeRotation(angle);
t2 = CGAffineTransformScale(t1, scale, 1.0f);
t3 = CGAffineTransformRotate(t2, -angle);
CATransform3D trans = CATransform3DMakeAffineTransform(t3);
[values addObject:[NSValue valueWithCATransform3D:trans]];
[times addObject:[NSNumber numberWithFloat:(float)i/(frames - 1)]];
}
rotate.values = values;
rotate.keyTimes = times;
rotate.duration = duration;
rotate.calculationMode = kCAAnimationLinear;
CAKeyframeAnimation *replace = [CAKeyframeAnimation
animationWithKeyPath:@"contents"];
replace.duration = duration/2;
replace.beginTime = duration/2;
replace.values = [NSArray arrayWithObjects:(id)image, nil];
replace.keyTimes = [NSArray arrayWithObjects:
[NSNumber numberWithDouble:0.0f], nil];
replace.calculationMode = kCAAnimationDiscrete;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.duration = duration;
group.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionLinear];
group.animations = [NSArray arrayWithObjects:rotate, replace, nil];
group.delegate = self;
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;
[group setValue:@"flipAnimation" forKey:@"name"];
[group setValue:layer forKey:@"layer"];
[group setValue:(id)image forKey:@"image"];
[layer addAnimation:group forKey:nil];
}
đang Original:
+ (void)flipLayer:(CALayer *)layer
toImage:(CGImageRef)image
withAngle:(double)angle {
const float duration = 0.5f;
CAKeyframeAnimation *diag = [CAKeyframeAnimation
animationWithKeyPath:@"transform.rotation.z"];
diag.duration = duration;
diag.values = [NSArray arrayWithObjects:
[NSNumber numberWithDouble:angle],
[NSNumber numberWithDouble:0.0f],
nil];
diag.keyTimes = [NSArray arrayWithObjects:
[NSNumber numberWithDouble:0.0f],
[NSNumber numberWithDouble:1.0f],
nil];
diag.calculationMode = kCAAnimationDiscrete;
CAKeyframeAnimation *flip = [CAKeyframeAnimation
animationWithKeyPath:@"transform.rotation.y"];
flip.duration = duration;
flip.values = [NSArray arrayWithObjects:
[NSNumber numberWithDouble:0.0f],
[NSNumber numberWithDouble:M_PI/2],
[NSNumber numberWithDouble:0.0f],
nil];
flip.keyTimes = [NSArray arrayWithObjects:
[NSNumber numberWithDouble:0.0f],
[NSNumber numberWithDouble:0.5f],
[NSNumber numberWithDouble:1.0f],
nil];
flip.calculationMode = kCAAnimationLinear;
CAKeyframeAnimation *replace = [CAKeyframeAnimation
animationWithKeyPath:@"contents"];
replace.duration = duration/2;
replace.beginTime = duration/2;
replace.values = [NSArray arrayWithObjects:(id)image, nil];
replace.keyTimes = [NSArray arrayWithObjects:
[NSNumber numberWithDouble:0.0f], nil];
replace.calculationMode = kCAAnimationDiscrete;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.removedOnCompletion = NO;
group.duration = duration;
group.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionLinear];
group.animations = [NSArray arrayWithObjects:diag, flip, replace, nil];
group.fillMode = kCAFillModeForwards;
[layer addAnimation:group forKey:nil];
}
một là gì trục chéo? – kennytm
Bằng trục chéo, tôi có nghĩa là vòng xoay phải nằm quanh đường chéo của lớp. Giống như nếu bạn nắm lấy hai góc chéo của lớp và xoay nó xung quanh. –
Giống như, xoay về đường thẳng 'y = x'? – kennytm