I wanted to create a circular slider that you can drag and animate. So far I have managed to create a slider and use the drag handle to move it and even animate it. Sometimes the animation goes wrong (wrong direction or shortest direction). I subclassed UIView (soon there will be UIControl, I just wanted to get the animation on the right) added a PanGestureRecognizer and several layers for the drawing.

So how do I fix this strange behavior? I could help me here, I would be grateful :)
Here's a sample project -> http://cl.ly/2l0O3b1I3U0X
Thanks a lot!
EDIT:
Here's the drawing code:
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];
Here's the animation code:
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"];
source share