車小弟

Main.storyboardide

ViewController.m動畫

//atom

//  ViewController.mspa

//  8A06.車小弟orm

//blog

//  Created by huan on 16/2/5.事件

//  Copyright © 2016 huanxi. All rights reserved.圖片

//ci

 

#import "ViewController.h"get

#import "OBShapedButton.h"//這個類能夠解決btn.tag;

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *circleImageView;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    //往圖片添加三個扇形按鈕

    for (NSInteger i = 0; i < 3; i++) {

        //獲取按鈕圖片的名稱

        NSString *imageName = [NSString stringWithFormat:@"circle%ld",i+1];

        //添加按鈕

        UIButton *btn = [OBShapedButton buttonWithType:UIButtonTypeCustom];

        btn.frame = self.circleImageView.bounds;

        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];

        btn.tag = i;

        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

       

        //往圖片添加按鈕

        [self.circleImageView addSubview:btn];

    }

    

    //添加中心的按鈕

    UIButton *centerBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    centerBtn.bounds = CGRectMake(0, 0, 112, 112);

    [centerBtn setBackgroundImage:[UIImage imageNamed:@"home_btn_dealer_had_bind"] forState:UIControlStateNormal];

    

    //監聽按鈕的事件

    [centerBtn addTarget:self action:@selector(centerBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    centerBtn.center = self.circleImageView.center;

    [self.view addSubview:centerBtn];

    

}

 

-(void)centerBtnClick:(UIButton *)btn{

    

    CGFloat currentAlpha = self.circleImageView.alpha;

    //1.先實現隱藏和顯示

    //hidden    alpha

    if (currentAlpha == 1) {//隱藏

        self.circleImageView.alpha = 0;

    }else{//顯示

        self.circleImageView.alpha = 1;

    }

    //2.再添加動畫 透明度 、縮放、旋轉效果

    //2.1 建立組動畫

    CAAnimationGroup *group = [CAAnimationGroup animation];

    //2.2透明度動畫

#warning 這裏設置的透明度是圖層opacity

    CABasicAnimation *opacityAni = [CABasicAnimation animation];

    opacityAni.keyPath = @"opacity";

    

    //2.2 縮放動畫

    CAKeyframeAnimation *scaleAni = [CAKeyframeAnimation animation];

    scaleAni.keyPath = @"transform.scale";

    

    //2.3 旋轉的動畫

    CABasicAnimation *rotationAni = [CABasicAnimation animation];

    rotationAni.keyPath = @"transform.rotation";

    //若是是要隱藏,透明度是由顯示"看不見"

    if (currentAlpha == 1) {

        opacityAni.fromValue = @1;

        opacityAni.toValue = @0;

        scaleAni.values = @[@1, @1.2, @0];

        

        //旋轉的時候從原來的位置 逆時針 旋轉45

        rotationAni.fromValue = @0;

        rotationAni.toValue = @(-M_PI_4);

    }else{

        opacityAni.fromValue = @0;

        opacityAni.toValue = @1;

        scaleAni.values = @[@0, @1.2, @1];

        //顯示的時候,旋轉是從 -M_PI_4開始

        rotationAni.fromValue = @(-M_PI_4);

        rotationAni.toValue = @0;

    }

    

    

    

    group.animations = @[opacityAni, scaleAni, rotationAni];

    group.duration = 3;

    [self.circleImageView.layer addAnimation:group forKey:nil];

    

}

 

-(void)btnClick:(UIButton *)btn{

    NSLog(@"%ld",btn.tag);

}

 

@end

結果

相關文章
相關標籤/搜索