美好的一天,大家!
嗯,我有一个非常简单的麻烦。 我有一个带有两个子层的CATransformLayer,每个子层都有“假”双面属性,这就形成了一张双面卡。 问题是,我无法设置动画持续时间或任何其他选项,例如,UIViewAnimationCurveEaseInOut等。 我尝试了新的和旧的方法来为我的图层添加动画选项和持续时间,但不幸的是,它旋转大约0.25秒,如默认值所示。怎么了?任何建议,将不胜感激。
以下是代码:
1)所有内容的初始化:
#ifndef SUPPORTED_MACRO
#define DEGREES_TO_RADIANS(angle) (angle * M_PI / 180.0)
#endif
- (void)viewDidLoad
{
[super viewDidLoad];
[super viewDidLoad];
CATransformLayer *cardContainer = [CATransformLayer layer];
cardContainer.anchorPoint = CGPointMake( 0.5 , 1.0 ) ;
cardContainer.position = CGPointMake(100.0, 200.0) ;
cardContainer.bounds = CGRectMake(0.0, 0.0, 150.0, 170.0);
CALayer *cardFront = [CALayer layer]; // the front part of card
cardFront.bounds = CGRectMake(0.0, 0.0, cardContainer.bounds.size.width, cardContainer.bounds.size.height) ;
cardFront.frame = cardContainer.bounds ;
cardFront.contentsGravity = kCAGravityResizeAspect ;
cardFront.borderColor = [UIColor blackColor].CGColor;
cardFront.borderWidth = 2.0;
cardFront.cornerRadius = 30.0;
[cardContainer addSublayer:cardFront];
CALayer *cardBack = [CALayer layer]; //back part of card
cardBack.bounds = CGRectMake(0.0, 0.0, cardContainer.bounds.size.width, cardContainer.bounds.size.height) ;
cardBack.frame = cardContainer.bounds;
cardBack.contentsGravity = kCAGravityResizeAspect ;
cardBack.borderColor = [UIColor blackColor].CGColor;
cardBack.borderWidth = 2.0;
cardBack.cornerRadius = 30.0;
cardBack.doubleSided = NO;
cardBack.transform = CATransform3DMakeRotation(DEGREES_TO_RADIANS(180.0),0.0,1.0,0.0);
[cardContainer addSublayer:cardBack];
((CALayer*)[cardContainer.sublayers objectAtIndex:0]).contents = (id)[UIImage imageNamed:@"1.png"].CGImage ;
((CALayer*)[cardContainer.sublayers objectAtIndex:1]).contents = (id)[UIImage imageNamed:@"2.png"].CGImage ;
TL = cardContainer ;
[self.view.layer addSublayer:cardContainer];
}
2)旋转的程序(以不推荐的方式):
-(void)Rotate
{
static double CallCounter = 1.0 ;
auto CATransform3D t3d = CATransform3DIdentity;
t3d.m34 = 1.0/-500.0;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.75];
TL.sublayerTransform = CATransform3DRotate(t3d, -(CallCounter*M_PI) , 1.0, 0.0, 0.0) ;
[UIView commitAnimations];
CallCounter += 1.0 ;
}
答案 0 :(得分:2)
嗯,问题很简单。问题是,并非对象的每个属性都可以动画化。例如,我们无法为此表达式设置动画:
int *a = ( int* ) malloc ( 5 * sizeof ( int ) ) ;
或者这个:
int x = 5 ;
或者,最后,这个:
TL.sublayerTransform = CATransform3DRotate(t3d, -(CallCounter*M_PI) , 1.0, 0.0, 0.0) ;
因为transform属性的想法是生成一个矩阵,它将被写入sublayerTransfrom类字段。并且,基本上对于编译器,它是相同的表达式,如malloc调用,或初始化x变量 - 它是一个分配操作。看起来,CATransformLayer类有某种检查器,只有当属性改变它的值时才会调用它。当属性发生变化时,Cocoa将自动发送消息。通过关键价值观察机制。
因此,为了动画这个不可动画的属性,我决定使用CATransaction块。他们在这种情况下真的很有帮助。希望,有人会发现这篇文章有用!对不起我的英语,我来自俄罗斯)