2013-07-17 31 views
4

动画怪异的行为我想作一个圆形滑块,可拖动和动画。到目前为止,我已经设法构建滑块,并使用拖动手柄来移动它并进行动画处理。有时,动画出错(错误的方向或最短的方向。我已经子类化一个UIView(将是一个UIControl很快,只是想立刻先拿到动画)加入了PanGestureRecognizer和若干层的图纸。圆形滑块,同时与CA

enter image description here

那么,如何解决这个怪异的行为我已经有人可以帮助我在这里,我要感谢:)

这里的样本项目 - >http://cl.ly/2l0O3b1I3U0X

非常感谢!

编辑:

这里的绘图代码:

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

self.handleHostLayer = [CALayer layer]; 
self.handleHostLayer.bounds = CGRectMake(0, 0, 170, 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, 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

动画代码似乎是问题。你只能发布动画和可能的图纸代码。我不想通过项目来找到它。 –

+0

非常感谢您的帮助!我发布了上面的绘图代码(绘图在drawRect中完成:) – reiserD

回答

1

OK,我看着你的项目。您的问题是,从角度和角度都不落在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中做了一个很长的解释,说明为什么这样做不好。

简而言之:你是不是从你以为你是动画,因为你无意中引入层属性的值,哪些是你在屏幕上看到的差异值动画。

跳过该行一起。您也可以跳过累计行,因为您不重复该动画。现在,如果您的动画没有粘贴:在将动画添加到动画之前,将模型值设置为其最终值。

+0

谢谢。不幸的是我的问题并没有完全解决。手柄仍在整个圆周滑动。我认为这与我的旋转材料有关。手柄是一个圆形层,作为底层插入到旋转的矩形层中。如果我旋转过零,则以rad为单位的角度是例如5.8 ...层的转换旋转在-0.45 ...。所以这可能就是为什么它全身而退的原因。但如何解决这个问题..我会试试:) – reiserD

+0

联合国遗憾的不是。的NSLog(@ “handleTarget:%F,C:%F”,handleTarget,[[self.handleHostLayer valueForKeyPath:@ “transform.rotation”]的floatValue]); - > handleTarget:6.178466,c:-0.104720 – reiserD

+0

非常感谢您的努力。我几乎投降。低劣的圈子的东西。现在它是闪烁的,仍然在整个圈子中移动。不能让我的头靠近它。特别是如果它是大约86°F ... – reiserD