1. 程式人生 > >ios激情詳解之動畫3D旋轉晃動

ios激情詳解之動畫3D旋轉晃動

//
//  Created by WDX on 15/9/23.
//  Copyright (c) 2015年 WDongXu. All rights reserved.
//
#import "RootViewController.h"

@interface RootViewController ()
@property (nonatomic, retain)UIView *myView;


@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addSubViews];
   
}
// 初始化 建立檢視
- (void)addSubViews
{
    self.myView = [[UIView alloc]initWithFrame:(CGRectMake(100, 100, 100, 100))];
    self.myView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.myView];
    [_myView release];
    
    // layer是負責顯示圖層的
    // 要更改 咱們看到的圖形的形狀 需要更該layer
    
    // 設定圓角
    // 先決條件 變圓 必須是長寬相同
    self.myView.layer.cornerRadius = self.myView.frame.size.width / 2;
    // 設定陰影的顏色
    // CGColorRef 圖層繪製的顏色
    self.myView.layer.shadowColor = [UIColor blackColor].CGColor;
    // 顯示陰影範圍
    self.myView.layer.shadowOffset = CGSizeMake(10, 10);
    // 陰影透明度
    self.myView.layer.shadowOpacity = 1;
    // 模糊程度
    self.myView.layer.shadowRadius = 50;
    // 設定邊框
    self.myView.layer.borderWidth = 10;
    // 設定邊框的顏色
    self.myView.layer.borderColor = [UIColor blueColor].CGColor;
    // layer層動畫
    
    // CAKeyframeAnimation抽象類
    // CABasicAnimation 基礎動畫 可以更改大小,旋轉等
    // CAPropertyAnimation 主要有按照軌跡移動,位置 比如 執行一組動畫是使用背景顏色
    
// 建立一個button
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(100, 300, 100, 100);
                    button.backgroundColor = [UIColor blueColor];
                    [self.view addSubview:button];
                    [button setTitle:@"點我" forState:(UIControlStateNormal)];
                    [button addTarget:self action:@selector(actionButton:) forControlEvents:(UIControlEventTouchUpInside)];
}
- (void)actionButton:(UIButton *)button
 {
     //  [self xyAnimation];
     //[self sizeAnimation];
     //[self changeBackgroundColor];
     // [self positionPoint];
      [self huangDongPoint];
     [self transform3D];
     // [self groupAnimation];
}
// 實現晃動
- (void)huangDongPoint
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
    CGFloat center = self.myView.layer.position.x;
    CGFloat left = center - 10;
    CGFloat right = center + 10;
    NSNumber *l = [NSNumber numberWithFloat:left];
    NSNumber *c = [NSNumber numberWithFloat:center];
    NSNumber *r = [NSNumber numberWithFloat:right];
    animation.values = @[l,c,r,l,c,r,l,c,r,l,c,r,l,c,r,l,c,r,l,c,r,l,c,r,l,c,r,l,c,r,l,c];
    animation.duration = 1;

    [self.myView.layer addAnimation:animation forKey:@"huangDong.x"];
}
// 3d旋轉
- (void)transform3D
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    // 結束值
    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.myView.layer.transform, M_PI, 10, 10, 10)];
    // 設定時間
    animation.duration = 2;
    [self.myView.layer addAnimation:animation forKey:@"transform"];
}