加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 使用CA进行动画制作时的圆形滑块怪异行为

发布时间:2020-12-16 07:05:10 所属栏目:百科 来源:网络整理
导读:我想制作一个可拖动且可动画的圆形滑块.到目前为止,我已经设法构建滑块并使用拖动手柄移动它,甚至动画它.有时动画会出错(错误的方向或最短的方向.我已经将UIView(很快就会成为UIControl,只是想让动画首先正确)的子类增加了一个PanGestureRecognizer和几个绘
我想制作一个可拖动且可动画的圆形滑块.到目前为止,我已经设法构建滑块并使用拖动手柄移动它,甚至动画它.有时动画会出错(错误的方向或最短的方向.我已经将UIView(很快就会成为UIControl,只是想让动画首先正确)的子类增加了一个PanGestureRecognizer和几个绘图层.

那么我该如何解决这种奇怪的行为呢?我有人可以帮助我,我会感激:)

这是示例项目 – > http://cl.ly/2l0O3b1I3U0X

非常感谢!

编辑:

这是绘图代码:

CALayer *aLayer = [CALayer layer];
aLayer.bounds = CGRectMake(0,170,170);
aLayer.position = self.center;
aLayer.transform = CATransform3DMakeRotation(M_PI_4,1);

self.handleHostLayer = [CALayer layer];
self.handleHostLayer.bounds = CGRectMake(0,170);
self.handleHostLayer.position = CGPointMake(CGRectGetMaxX(aLayer.bounds) - 170/2.0,CGRectGetMaxY(aLayer.bounds) - 170/2.0);

[aLayer addSublayer:self.handleHostLayer];
[self.layer addSublayer:aLayer];

self.handle = [CALayer layer];
self.handle.bounds = CGRectMake(0,50,50);
self.handle.cornerRadius = 25;
self.handle.backgroundColor = [UIColor whiteColor].CGColor;
self.handle.masksToBounds = NO;
self.handle.shadowOffset = CGSizeMake(3.0,0.0);
self.handle.shadowRadius = 0;
self.handle.shadowOpacity = .15;
self.handle.shadowColor = [UIColor blackColor].CGColor;

[self.handleHostLayer addSublayer:self.self.handle];

这是动画代码:

CGFloat handleTarget = ToRad(DEG);

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotationAnimation.fromValue = @([[self.handleHostLayer valueForKeyPath:@"transform.rotation"] floatValue]);
rotationAnimation.toValue = @(handleTarget);
rotationAnimation.duration = .5;
rotationAnimation.removedOnCompletion = NO;
rotationAnimation.fillMode = kCAFillModeForwards;
rotationAnimation.cumulative = YES;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaSEOut];

[self.handleHostLayer addAnimation:rotationAnimation forKey:@"transform.rotation"];

解决方法

好的,我查看了你的项目.你的问题是,往返角度都不落在0≤?<2π范围内. 您可以通过添加和删除2π来确保它们完成,直到它们都在该范围内.

CGFloat fromAngle = [[self.handleHostLayer valueForKeyPath:@"transform.rotation"] floatValue];
CGFloat toAngle   = handleTarget;

while (fromAngle >= 2.0*M_PI) { fromAngle -= 2*M_PI; }
while (toAngle   >= 2.0*M_PI) { toAngle   -= 2*M_PI; }
while (fromAngle <  0.0)      { fromAngle += 2*M_PI; }
while (toAngle   <  0.0)      { toAngle   += 2*M_PI; }

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotationAnimation.fromValue = @(fromAngle);
rotationAnimation.toValue   = @(toAngle);
// As before...

你可以改变的另一件事是滥用removeOnCompletion.我在this answer做了一个很长的解释,说明为什么这样做很糟糕.

简而言之:由于您无意中引入了图层属性的值与您在屏幕上看到的内容之间的差异,因此您不会设置您认为自己动画的值的动画.

一起跳过那条线.您也可以跳过跳过累积线,因为您没有重复动画.现在,如果你的动画没有坚持:在将动画添加到它之前将模型值设置为其最终值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读