【问题标题】:Rotate a slice of CAShape layer as per dragging根据拖动旋转一片 CAShape 图层
【发布时间】:2023-03-16 22:20:01
【问题描述】:

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

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

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

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

谢谢。

【问题讨论】:

  • 嗨,欢迎来到堆栈溢出。您可以在没有链接的情况下将图片添加到您的帖子中并向我们展示您的尝试吗?
  • 抱歉兄弟无法在帖子中添加图片。要在帖子中添加图片,我需要 10 个声誉。
  • 我得到了结果...完成了任务...谢谢... :)
  • 您也可以发布您的答案的基础知识吗? :)
  • 你想让我发布我的代码 :)

标签: ios calayer cashapelayer


【解决方案1】:

你需要改变你的 layer.anchorPoint。

可以阅读THIS了解anchorPoint。

【讨论】:

  • 谢谢大哥...我已经完成了任务...但是我对锚点有了一些想法。谢谢。
【解决方案2】:
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];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多