从固定点向后和向前移动一个出气筒

时间:2014-08-17 14:06:58

标签: ios cabasicanimation

我正在创建一个拳击应用程序。我不得不在后退和前进的方向移动一个出气筒..不能做到这一点。有什么建议吗?

self.punchingBag.layer.anchorPoint =CGPointMake(0.5,.5);

CATransform3D t = CATransform3DIdentity;
if(!check)
{
    check = true;

    t = CATransform3DRotate(t,60.0f * M_PI / 180.0f, 1,0,1);

    self.punchingBag.layer.transform = t;


}

1 个答案:

答案 0 :(得分:1)

所以我猜你想要的是一个动画,好像沙袋会向你摆动,然后从你身边回来。这意味着围绕视图顶部的x轴旋转,同时还应用透视变换。这是一种方法:

UIView *bag = [[UIView alloc] initWithFrame:CGRectMake(20, 200, 50, 100)];
bag.backgroundColor = [UIColor redColor];
[self.window addSubview:bag];

bag.layer.anchorPoint = CGPointMake(0.5, 0); // Where we want to rotate around.
CATransform3D perspectiveTransform = CATransform3DIdentity;
perspectiveTransform.m34 = 1.0/-800; // Perspective transformation. Experiment with it.

// The animation.
CABasicAnimation *swing = [CABasicAnimation animationWithKeyPath:@"transform"];
swing.fromValue = [NSValue valueWithCATransform3D:CATransform3DRotate(perspectiveTransform, -M_PI_4, 1, 0, 0)];
swing.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(perspectiveTransform, M_PI_4, 1, 0, 0)];
swing.repeatCount = HUGE_VALF;
swing.duration = 1;
swing.autoreverses = YES;
swing.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[bag.layer addAnimation:swing forKey:@"swing"];