ios扇形动画菜单

计算扇形点的坐标:

//计算扇形坐标
- (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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值