gpt4 book ai didi

ios - 沿弧线对 UIView 进行动画处理

转载 作者:技术小花猫 更新时间:2023-10-29 10:50:05 25 4
gpt4 key购买 nike

我希望沿着从位置 1 到位置 2 的图像中所示的圆弧对 View 进行动画处理。

enter image description here

实际发生的是 View 动画描述了一个完整的圆而不是圆弧。我的问题是:

  1. 我应该使用 CGPathAddArc 还是 CGPathAddArcToPoint
  2. 是否需要使用 CGPathMoveToPoint 来描述 View 的初始位置?

当我单步执行代码时,CGPoints 值与我预期的一样,角度也是如此。

我使用以下函数获取圆上的 CGPoints

CGPoint pointOnCircle(CGPoint centre, float radius, float angle)
{
float x = centre.x + radius * cosf(angle);
float y = centre.y + radius * sinf(angle);

return CGPointMake(x, y);
}

我正在使用以下代码及其变体,但我还没有设法破解它。

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = duration;
pathAnimation.repeatCount = 1;

CGPoint viewCenter = dynamicView.objectCenter;
CGMutablePathRef arcPath = CGPathCreateMutable();
// CGPathMoveToPoint(arcPath, NULL, viewCenter.x, viewCenter.y);

//work out the half way point
float halfwayAngle = dynamicView.angle + (0.5f * self.angleIncrement);
float fullwayAngle = dynamicView.angle + self.angleIncrement;
CGPoint halfwayPoint = pointOnCircle(self.center, self.radius, halfwayAngle);
CGPoint fullwayPoint = pointOnCircle(self.center, self.radius, fullwayAngle);

CGPathAddArc(arcPath, NULL, halfwayPoint.x, halfwayPoint.y, self.radius, dynamicView.angle, fullwayAngle, clockwise);
// CGPathAddArcToPoint(arcPath, NULL, viewCenter.x, viewCenter.y, fullwayPoint.x, fullwayPoint.y, self.radius);

pathAnimation.path = arcPath;
CGPathRelease(arcPath);

[dynamicView.layer addAnimation:pathAnimation forKey:@"arc"];

最佳答案

我编写了一个简单示例,希望能将所有内容放在一起。我从“单一 View ”应用程序模板开始,将此操作连同触发该操作的按钮一起添加到 View Controller 。

- (IBAction)animateArc:(id)sender
{
// Create the arc
CGPoint arcStart = CGPointMake(0.2 * self.view.bounds.size.width, 0.5 * self.view.bounds.size.height);
CGPoint arcCenter = CGPointMake(0.5 * self.view.bounds.size.width, 0.5 * self.view.bounds.size.height);
CGFloat arcRadius = 0.3 * MIN(self.view.bounds.size.width, self.view.bounds.size.height);

CGMutablePathRef arcPath = CGPathCreateMutable();
CGPathMoveToPoint(arcPath, NULL, arcStart.x, arcStart.y);
CGPathAddArc(arcPath, NULL, arcCenter.x, arcCenter.y, arcRadius, M_PI, 0, NO);

// The layer we're going to animate (a 50x50pt red box)
UIView* dynamicView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 50, 50)];
dynamicView.layer.backgroundColor = [[UIColor redColor] CGColor];
[self.view addSubview: dynamicView];
dynamicView.center = arcStart;

// An additional view that shows the arc.
BOOL showArc = NO;
UIView* drawArcView = nil;
if (showArc)
{
drawArcView = [[UIView alloc] initWithFrame: self.view.bounds];
CAShapeLayer* showArcLayer = [[CAShapeLayer alloc] init];
showArcLayer.frame = drawArcView.layer.bounds;
showArcLayer.path = arcPath;
showArcLayer.strokeColor = [[UIColor blackColor] CGColor];
showArcLayer.fillColor = nil;
showArcLayer.lineWidth = 3.0;
[drawArcView.layer addSublayer: showArcLayer];
[self.view insertSubview: drawArcView belowSubview: dynamicView];
}

// The animation
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.duration = 5.0;
pathAnimation.path = arcPath;
CGPathRelease(arcPath);

// Add the animation and reset the state so we can run again.
[CATransaction begin];
[CATransaction setCompletionBlock:^{
[drawArcView removeFromSuperview];
[dynamicView removeFromSuperview];
}];
[dynamicView.layer addAnimation:pathAnimation forKey:@"arc"];
[CATransaction commit];
}

关于ios - 沿弧线对 UIView 进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17995681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com