计算扇形点的坐标:
//计算扇形坐标
- (NSMutableArray *)getXWithTanAngle:(double)tanAngle {
double x;
double y;
NSMutableArray *locationArray = [[NSMutableArray alloc]init];
x = r / (sqrt(1 + tanAngle * tanAngle));
y = tanAngle * x ;
[locationArray addObject:[NSString stringWithFormat:@"%f",x]];
[locationArray addObject:[NSString stringWithFormat:@"%f",y]];
return locationArray;
}
按钮点击事件:
- (IBAction)menuButton:(UIButton *)sender {
double tan0 = tan(0);
double tan1 = tan(M_PI_2 / (n - 1));
double tan2 = tan((M_PI_2 / (n - 1) * 2));
double tan3 = tan((M_PI_2 / (n - 1) * 3));
double tan4 = tan((M_PI_2 / (n - 1) * 4));
double x0 = [[self getXWithTanAngle:tan0] [0] doubleValue];
double x1 = [[self getXWithTanAngle:tan1] [0] doubleValue];
double x2 = [[self getXWithTanAngle:tan2] [0] doubleValue];
double x3 = [[self getXWithTanAngle:tan3] [0] doubleValue];
double y1 = [[self getXWithTanAngle:tan1] [1] doubleValue];
double y2 = [[self getXWithTanAngle:tan2] [1] doubleValue];
double y3 = [[self getXWithTanAngle:tan3] [1] doubleValue];
double y4 = [[self getXWithTanAngle:tan4] [1] doubleValue];
if (isOpen) {
isOpen = NO;
[self.btn1.layer addAnimation:[YMCAAnimationGroup fromEndPoint:CGPointMake(centerX - x0, centerY) toStartPoint:self.menuButton.center duration:0.15 button:self.btn1] forKey:nil];
[self.btn2.layer addAnimation:[YMCAAnimationGroup fromEndPoint:CGPointMake(centerX - x1, centerY - y1) toStartPoint:self.menuButton.center duration:0.15 button:self.btn2] forKey:nil];
[self.btn3.layer addAnimation:[YMCAAnimationGroup fromEndPoint:CGPointMake(centerX - x2, centerY - y2) toStartPoint:self.menuButton.center duration:0.15 button:self.btn3] forKey:nil];
[self.btn4.layer addAnimation:[YMCAAnimationGroup fromEndPoint:CGPointMake(centerX - x3, centerY - y3) toStartPoint:self.menuButton.center duration:0.15 button:self.btn4] forKey:nil];
[self.btn5.layer addAnimation:[YMCAAnimationGroup fromEndPoint:CGPointMake(centerX, centerY - y4) toStartPoint:self.menuButton.center duration:0.15 button:self.btn5] forKey:nil];
}
else {
isOpen = YES;
[self.btn1.layer addAnimation:[YMCAAnimationGroup fromPoint:self.menuButton.center toPoint:CGPointMake(centerX - x0, centerY) duration:0.3 button:self.btn1] forKey:nil];
[self.btn2.layer addAnimation:[YMCAAnimationGroup fromPoint:self.menuButton.center toPoint:CGPointMake(centerX - x1, centerY - y1) duration:0.3 button:self.btn2] forKey:nil];
[self.btn3.layer addAnimation:[YMCAAnimationGroup fromPoint:self.menuButton.center toPoint:CGPointMake(centerX - x2, centerY - y2) duration:0.3 button:self.btn3] forKey:nil];
[self.btn4.layer addAnimation:[YMCAAnimationGroup fromPoint:self.menuButton.center toPoint:CGPointMake(centerX - x3, centerY - y3) duration:0.3 button:self.btn4] forKey:nil];
[self.btn5.layer addAnimation:[YMCAAnimationGroup fromPoint:self.menuButton.center toPoint:CGPointMake(centerX, centerY - y4) duration:0.3 button:self.btn5] forKey:nil];
}
}
动画组合:
//打开菜单
+ (CAAnimationGroup *)fromPoint:(CGPoint)from toPoint:(CGPoint)to duration:(CFTimeInterval)duration button:(UIButton *)button
{
//路径曲线
UIBezierPath *movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:from];
//[movePath addLineToPoint:to];
[movePath addQuadCurveToPoint:to
controlPoint:CGPointMake( to.x - 10, to.y - 10)];
[movePath addQuadCurveToPoint:to
controlPoint:CGPointMake( to.x + 10, to.y + 10)];
//关键帧
CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnim.path = movePath.CGPath;
moveAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
moveAnim.removedOnCompletion = YES;
CABasicAnimation *TransformAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
TransformAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
//沿Z轴旋转
TransformAnim.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI,0,0,1)];
TransformAnim.cumulative = YES;
TransformAnim.duration = duration / 4;
//旋转1遍,360度
TransformAnim.repeatCount = 4;
TransformAnim.removedOnCompletion = YES;
CAAnimationGroup *animGroup = [CAAnimationGroup animation];
animGroup.animations = [NSArray arrayWithObjects: TransformAnim, moveAnim,nil];
animGroup.duration = duration;
button.center = to;
return animGroup;
}
//收回菜单
+ (CAAnimationGroup *)fromEndPoint:(CGPoint)from toStartPoint:(CGPoint)to duration:(CFTimeInterval)duration button:(UIButton *)button
{
//路径曲线
UIBezierPath *movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:from];
[movePath addLineToPoint:to];
//关键帧
CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnim.path = movePath.CGPath;
moveAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
moveAnim.removedOnCompletion = YES;
CABasicAnimation *TransformAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
TransformAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
//沿Z轴旋转
TransformAnim.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI,0,0,1)];
TransformAnim.cumulative = YES;
TransformAnim.duration = duration / 3;
//旋转1遍,360度
TransformAnim.repeatCount = 3;
TransformAnim.removedOnCompletion = YES;
CAAnimationGroup *animGroup = [CAAnimationGroup animation];
animGroup.animations = [NSArray arrayWithObjects:moveAnim, TransformAnim,nil];
animGroup.duration = duration;
button.center = to;
return animGroup;
}
细节请参考demo:http://download.csdn.net/download/u011918080/7632701