码迷,mamicode.com
首页 > 其他好文 > 详细

2016-1-10 组动画学习 底部旋转菜单的实现

时间:2016-01-11 23:35:40      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

#define detal 80;
// 按钮间距

#import "ZZMenuView.h"
@interface ZZMenuView()
@property (nonatomic, strong) NSMutableArray *items;
@property (weak, nonatomic) IBOutlet UIButton *mainBtn;


@end
@implementation ZZMenuView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (NSMutableArray *)items
{
    if (!_items) {
        _items = [NSMutableArray array];
    }
    return _items;
}
+ (instancetype)MenuView
{
    return [[[NSBundle mainBundle] loadNibNamed:@"ZZMenu" owner:nil options:nil] lastObject];
}
#warning 对象是从xib storybord中加载时会调用这个方法
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self initItems];
    }
    return self;
}
// 初始化三个隐藏的按钮
- (void)initItems
{
//    NSLog(@"!!!!");
    [self addBtnWithImgName:@"menu_btn_call" tag:1];
    [self addBtnWithImgName:@"menu_btn_cheyou" tag:2];
    [self addBtnWithImgName:@"menu_btn_tixing" tag:3];
    
    
}
- (void) addBtnWithImgName:(NSString *)imgName tag:(NSInteger)tag
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    btn.tag = tag;
    [self.items addObject:btn];
    [self addSubview:btn];
}
#pragma mark - 设置按钮尺寸位置
- (void)layoutSubviews
{
    [super layoutSubviews];
    CGRect btnRect = CGRectMake(0,0,30,30);
    for (UIButton *btn in self.items) {
        btn.bounds = btnRect;
        btn.center = self.mainBtn.center;
    }
//    [self bringSubviewToFront: self.mainBtn];
 
}
- (IBAction)mainBtnClick:(UIButton *)mainBtn {
    BOOL show = CGAffineTransformIsIdentity(self.mainBtn.transform);
    [UIView animateWithDuration:1 animations:^{
        if (show) {
            self.mainBtn.transform = CGAffineTransformMakeRotation(M_PI_4);
        }else{
            self.mainBtn.transform = CGAffineTransformIdentity;
        }
    }];
    [self showItems:show];
}
- (void)showItems:(BOOL)isShow
{
    
    if (isShow) {
    // 选项按钮位置调整
    for (UIButton *btn  in  self.items) {
        CGFloat centerX = self.mainBtn.center.x + btn.tag * detal;
        CGFloat centerY = self.mainBtn.center.y;
        CGPoint showPoint = CGPointMake(centerX, centerY);
        btn.center = showPoint;
        // 为每一个按钮都添加组动画
        CAAnimationGroup *groupAnimations = [CAAnimationGroup animation];
          // 创建平移动画
        CAKeyframeAnimation *transformAni =[CAKeyframeAnimation animation];
        transformAni.keyPath = @"position";
        NSValue *value1 = [NSValue valueWithCGPoint:self.mainBtn.center];
        NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(centerX*0.5, centerY)];
        NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(centerX*1.1,
                                                                centerY)];
        NSValue *value4 = [NSValue valueWithCGPoint:showPoint];
        // 设置平移路径
        transformAni.values = @[value1,value2,value3,value4];
         //创建旋转动画
        CAKeyframeAnimation *rotationAni = [CAKeyframeAnimation animation];
        rotationAni.keyPath = @"transform.rotation";
        rotationAni.values = @[@0,@(M_PI * 2),@(M_PI * 4),@(M_PI *2)];
        // 设置旋转路径
        groupAnimations.animations = @[transformAni,rotationAni];
        groupAnimations.duration =1.5;
        //给每一个按钮的图层都添加组动画
        [btn.layer addAnimation:groupAnimations forKey:nil];
        }
}else{
    for (UIButton *btn  in  self.items) {
        //获取现在的位置
        CGPoint currentPositon = btn.center;
        btn.center = self.mainBtn.center;
        CGFloat centerY  = self.mainBtn.center.y;
        btn.transform = CGAffineTransformIdentity;
        // 为每一个按钮都添加组动画
        CAAnimationGroup *groupAnimations = [CAAnimationGroup animation];
        // 创建平移动画
        CAKeyframeAnimation *transformAni =[CAKeyframeAnimation animation];
        transformAni.keyPath = @"position";
        NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(currentPositon.x*1.1, centerY)];
        NSValue *value1 = [NSValue valueWithCGPoint:currentPositon];
        NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(currentPositon.x*0.5,centerY)];
        NSValue *value4 = [NSValue valueWithCGPoint:self.mainBtn.center];
        // 设置平移路径
        transformAni.values = @[value1,value2,value3,value4];
        //创建旋转动画
        CAKeyframeAnimation *rotationAni = [CAKeyframeAnimation animation];
        rotationAni.keyPath = @"transform.rotation";
        rotationAni.values = @[@0,@(M_PI * 2),@0,@(-M_PI * 2)];
        // 设置旋转路径
        groupAnimations.animations = @[transformAni,rotationAni];
        groupAnimations.duration = 5;
        //给每一个按钮的图层都添加组动画
        [btn.layer addAnimation:groupAnimations forKey:nil];

    }
}
    
}
@end

技术分享

2016-1-10 组动画学习 底部旋转菜单的实现

标签:

原文地址:http://www.cnblogs.com/BJTUzhengli/p/5122815.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!