按拖动旋转一片CAShape图层

时间:2014-05-26 11:27:21

标签: ios calayer cashapelayer

如何将图层(CAShapeLayer)旋转为一个点作为中心。

查看下面的图片,以便更清楚地了解我的问题。

enter image description here

我需要在触摸移动时单独旋转特定的三角形图层。中心不应移动的地方。

如果anyOne有想法,请告诉我。

感谢。

2 个答案:

答案 0 :(得分:0)

您需要更改layer.anchorPoint。

您可以阅读THIS以了解anchorPoint。

答案 1 :(得分:0)

double prevLayerRotationRadius;

  double StartToAngle;
  double EndToAngle;

@property(nonatomic, assign) CGFloat seperateSliceStartAngle;

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
           UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:View];
        [self performActionForSeperateSliceRotation:point];

}

#pragma mark - RotateSeperateLayers

-(void)performActionForSeperateSliceRotation:(CGPoint)point{
    double rotationRadius = atan2(  point.y -  _pieCenter.y , point.x - _pieCenter.x );

    if (rotationRadius <=0) {
        rotationRadius =  radian180 + (radian180 - ABS(rotationRadius));
    }
    if (prevLayerRotationRadius && prevLayerRotationRadius !=0) {

        double radiusDiff = (rotationRadius  - prevLayerRotationRadius);

        if (radiusDiff <=0) {
            radiusDiff = ABS(radiusDiff) - ABS ((int)radiusDiff ) ;

            _SliceStartAngle = _SliceStartAngle - ABS(radiusDiff);


        }else {
            radiusDiff = ABS(radiusDiff) - ABS ((int)radiusDiff ) ;

            _SliceStartAngle = _SliceStartAngle + ABS(radiusDiff);

        }
        [self rotateSeperateLayers:_SliceStartAngle];

    }

    prevLayerRotationRadius = rotationRadius;

}

- (void)rotateSeperateLayers:(float)sperateSliceAngle{

    StartToAngle = 2.660850; // Angle of particular Piece (To get in angle use R2D)
    EndToAngle = StartToAngle;

    double angle = [[self.sliceAngleArray objectAtIndex:selectedBeganPanLayerOne]doubleValue];
    EndToAngle += angle;

    double startFromAngle = 2.660850;
    double endFromAngle = 2.660850;

    [Layer createArcAnimationForKey:@"startAngle"
                          fromValue:[NSNumber numberWithDouble:startFromAngle]
                            toValue:[NSNumber numberWithDouble:StartToAngle+SliceAngle]
                           Delegate:self];
    [Layer createArcAnimationForKey:@"endAngle"
                          fromValue:[NSNumber numberWithDouble:endFromAngle]
                            toValue:[NSNumber numberWithDouble:EndToAngle+SliceAngle]
                           Delegate:self];

}

- (void)createArcAnimationForKey:(NSString *)key fromValue:(NSNumber *)from toValue:(NSNumber *)to Delegate:(id)delegate
{
    CABasicAnimation *arcAnimation = [CABasicAnimation animationWithKeyPath:key];
    NSNumber *currentAngle = [[self presentationLayer] valueForKey:key];

    if(!currentAngle) currentAngle = from;

    [arcAnimation setFromValue:currentAngle];
    [arcAnimation setToValue:to];         
    [arcAnimation setDelegate:delegate];
    [arcAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    [self addAnimation:arcAnimation forKey:key];
    [self setValue:to forKey:key];
}