CATransaction(仍然)不动画

时间:2010-08-20 23:31:24

标签: iphone core-animation

我有一个与此非常相似的问题:

CATransaction Not Animating

我只是想尝试使用CATransaction为视图层设置动画。我的问题是变换立即应用于视图。

我尝试使用performSelector执行动画:withObject:afterDelay:没有成功。

这是我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];

 view = [[[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)] autorelease];
 view.backgroundColor = [UIColor blackColor];
 [self.view addSubview:view];

 [self performSelector:@selector(animateView) withObject:nil afterDelay:0.1];
}

-(void) animateView {
 [CATransaction begin];
 [CATransaction setValue:[NSNumber numberWithFloat:3.0f] forKey:kCATransactionAnimationDuration];
    CALayer *layer = view.layer;
    layer.position = CGPointMake(20, 
         300);
    CATransform3D transform = CATransform3DMakeScale(2.0f, 2.0f, 1.0f);
    transform = CATransform3DRotate(transform, acos(-1.0f)*1.5f, 1.5f, 1.5f, 1.5f);
    layer.transform = transform;
    [CATransaction commit];
}

有人知道出了什么问题吗?

谢谢,文森特。

2 个答案:

答案 0 :(得分:6)

在为视图的背景层设置动画时,您需要位于UIView动画块内,而不仅仅是CATransaction

[UIView beginAnimations:nil context:NULL];
// ... your animation code
[UIView commitAnimations];

答案 1 :(得分:0)

或者你可以使用CAAnimationGroup并对像这样的缩放和旋转动画进行分组..

CAAnimationGroup *aniGroup = [CAAnimationGroup animation];
// setup aniGroup properties

CABasicAnimation *scaleAnimation = [CABasicAnimation defaultValueForKey:@"transform.scale"];
scaleAnimation.fromValue = [NSNumber numberWithFloat:0.0];
scaleAnimation.toValue = [NSNumber numberWithFloat:1.0];
// setup scaleAnimation properties

CABasicAnimation *rotateAnimation = [CABasicAnimation defaultValueForKey:@"transform.rotate"];
// setup rotateAnimation properties
// ....

aniGroup.animations = [NSArray arrayWithObjects:rotateAnimation, scaleAnimation, nil];

[self.layer layoutIfNeeded];
[self.layer addAnimation:aniGroup forKey:@"myAnimation"];

类似的东西....可能不完全是复制,粘贴和可构建的。 ;)

希望这有帮助!