如何在iOS中沿X轴将旋转轮的方向从180改为0?

时间:2015-10-08 09:20:33

标签: ios animation math core-animation catransform3d

我的代码(下方)正常工作,并通过将轮子从 X0旋转到X180 来设置轮子动画(因此移动发生在 Z轴上,请参阅插图)。我想将旋转方向从X180更改为X0

rotation direction of current working code

当我按下我的按钮时,会调用以下 - (void)展开方法,并且是动画发生的位置:

- (void)expand {

   [CATransaction begin];

    CGFloat step =  DegreesToRadians(kApertureAngle);

     for (NSInteger i=[_items count]-1; i>=0; i--) {

        CALayer* layer = nil;

        CGFloat angle = -step * (CGFloat)i - (step * 0.65);

        CATransform3D transform = CATransform3DConcat(CATransform3DMakeScale(1.0f, 1.0f, 1.0f),
                                                      CATransform3DMakeRotation(angle, 0.0f, 0.0f, 1.0f));

        CABasicAnimation* leafAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];

        [leafAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        [leafAnimation setToValue:[NSValue valueWithCATransform3D:transform]];
        [leafAnimation setFillMode:kCAFillModeForwards];
        [leafAnimation setRemovedOnCompletion: NO];
        [leafAnimation setDuration:0.4f];

        layer = [_leavesLayers objectAtIndex:i];
        [layer addAnimation:leafAnimation forKey:@"expand"];


        CABasicAnimation* scaleImageAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
        [scaleImageAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];

        [scaleImageAnimation setToValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0f, 1.0f, 1.0f)]];
        [scaleImageAnimation setFillMode:kCAFillModeForwards];
        [scaleImageAnimation setRemovedOnCompletion: NO];


        CGPoint point = CGPointMake(0.65*97.0f * cos(angle) + CGRectGetMidX(self.bounds), 0.65*97.0f * sin(angle) + CGRectGetMidY(self.bounds));

        CABasicAnimation* positionImageAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
        [positionImageAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
        [positionImageAnimation setToValue:[NSValue valueWithCGPoint:point]];
        [positionImageAnimation setFillMode:kCAFillModeForwards];
        [positionImageAnimation setRemovedOnCompletion: NO];

        CAAnimationGroup* group = [CAAnimationGroup animation];
        [group setAnimations:[NSArray arrayWithObjects:scaleImageAnimation, positionImageAnimation, nil]];
        [group setFillMode:kCAFillModeForwards];
        [group setRemovedOnCompletion: NO];
        [group setDuration:0.2f];
        [group setBeginTime:CACurrentMediaTime () + 0.27f];

        layer = [_imagesLayers objectAtIndex:i];
        [layer addAnimation:group forKey:@"show"];

    }

    [CATransaction commit];


}

这是将图像添加到旋转轮部分的代码:

- (void)addLeaves {

    [_leavesLayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
    [_leavesLayers removeAllObjects];

    // iterate all images
    [_items enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        UIImage *image = [UIImage imageNamed:kLeafImageName];

        CALayer* layer = [CALayer layer];

        layer.contents = (id)image.CGImage;
        layer.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
        layer.anchorPoint = CGPointMake(0.0f, 0.5f);
        layer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
        layer.transform = CATransform3DMakeScale(0.15f, 0.15f, 1.0f);

        // add layer
        [self.layer addSublayer:layer];
        [_leavesLayers addObject:layer];

    }];


}

- (void)addImages {
    // remove from superlayer
    [_imagesLayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];

    // clean array
    [_imagesLayers removeAllObjects];


    // iterate all images
    [_items enumerateObjectsUsingBlock:^(AURosetteItem* obj, NSUInteger idx, BOOL *stop) {
        // set content image
        UIImage* image = [obj normalImage];

        CALayer* imageLayer = [CALayer layer];

        imageLayer.contents = (id)image.CGImage;
        imageLayer.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
        imageLayer.anchorPoint = CGPointMake(0.5f, 0.5f);
        imageLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
        imageLayer.transform = CATransform3DMakeScale(0.01f, 0.01f, 1.0f);
        imageLayer.opacity = 1.0f;

        [self.layer addSublayer:imageLayer];
        [_imagesLayers addObject:imageLayer];        
    }];
}

1 个答案:

答案 0 :(得分:0)

尝试将角度定义为

CGFloat angle = - M_PI + step * (CGFloat)i - (step * 0.65);

也就是说,保持角度的恒定部分,但将变化的部分step*i更改为π - step*i