讲述属性动画的使用 —使用动画旋转、平移、渐变和缩放

//

//  ViewController.h

//  Demo

//

//  Created by yixia on 15/7/14.

//  Copyright (c) 2015 yixia. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ViewController : UIViewController


@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end

//

//  ViewController.m

//  Demo

//

//  Created by yixia on 15/7/14.

//  Copyright (c) 2015 yixia. All rights reserved.

//


#import "ViewController.h"


@interface ViewController () <UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, strong) UIView *headView;

@property (nonatomic, strong) UIView *testView;


- (IBAction)change:(id)sender;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

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

    

    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100)];

    self.headView.backgroundColor = [UIColor purpleColor];

    

    self.tableView.tableHeaderView = self.headView;

    

//    UIView *view =self.tableView.tableHeaderView;

//    view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200);

//    [self.tableView setTableHeaderView:view];

    

    self.testView = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 100, 44)];

    self.testView.backgroundColor = [UIColor redColor];

    [self.view addSubview:self.testView];

    [super viewDidLoad];

    // 指定图像的位置

    //self.testView.center = self.view.center;

}

//#pragma mark - 懒加载

//- (UIImageView *)testView

//{

//        if (!_testView) {

//               _testView = [[UIImageView alloc] initWithImage:

//                                  [UIImage imageNamed:@"heart"]];

//               [self.view addSubview:_testView];

//            }

//return _testView;

//  }


/** 触摸开始的方法*/

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    // 获取触摸点

    CGPoint point = [[touches anyObject] locationInView:self.view];

//    // 实现平移的动画

   [self translationAnimation:point];

   // 实现渐变的动画

   [self alphaChangedAnimation];

    //实现缩放的动画

   [self scaleAnimation];

    //实现旋转的动画

    [self rotateAnimation];

    

}

/** 平移动画的方法*/

- (void)translationAnimation:(CGPoint)point

{

        // 创建基本动画,并指定可动画属性

        CABasicAnimation *basicAnimation = [CABasicAnimation

                                                                       animationWithKeyPath:@"position"];

        // 设置目标值

        basicAnimation.toValue = [NSValue valueWithCGPoint:point];

        // 动画播放完成后,停留在目标位置

        basicAnimation.removedOnCompletion = NO;

        basicAnimation.fillMode = kCAFillModeForwards;

        // 设置动画的时间函数

        basicAnimation.timingFunction = [CAMediaTimingFunction

                                                     functionWithName:kCAMediaTimingFunctionEaseOut];

        // 设置动画的时长

        basicAnimation.duration = 2.0f;

        // 添加到iconView的图层

        [self.testView.layer addAnimation:basicAnimation forKey:nil];

    }

/** 渐变动画的方法*/

- (void)alphaChangedAnimation

{

        // 创建一个基本动画,并指定可动画属性

        CABasicAnimation *basicAnimation = [CABasicAnimation

                           animationWithKeyPath:@"opacity"];

    // 设置起始值

        basicAnimation.fromValue = @1.0;

    // 设置目标值

    basicAnimation.toValue = @0.0;

    // 设置重复次数

        basicAnimation.repeatCount = MAXFLOAT;

        // 设置动画的时长

        basicAnimation.duration = 2.0f;

        // 添加到iconView的图层

    [self.testView.layer addAnimation:basicAnimation forKey:nil];

}



/** 缩放动画的方法*/

- (void)scaleAnimation

{

        // 创建一个基本动画,并指定可动画属性

        CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

        // 设置初始值和目标值

        basicAnimation.fromValue = @1.0;

        basicAnimation.toValue = @0.5;

        // 设置重复次数

        basicAnimation.repeatCount = MAXFLOAT;

        // 设置动画的时长

        basicAnimation.duration = 2.0f;

        // 添加到iconView的图层

        [self.testView.layer addAnimation:basicAnimation forKey:nil];

    }

/** 旋转动画的方法*/

- (void)rotateAnimation

{

        // 创建一个基本动画,并指定可动画属性

        CABasicAnimation *basicAnimation = [CABasicAnimation

                                                            animationWithKeyPath:@"transform.rotation.y"];

        // 设置初始值和目标值

        basicAnimation.fromValue = @0;

        basicAnimation.toValue = @(M_PI);

        // 设置动画的时长

        basicAnimation.duration = 2.0f;

        // 添加到的图层

    [self.testView.layer addAnimation:basicAnimation forKey:nil];

    }



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 40;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    cell.textLabel.text = [@"row: " stringByAppendingFormat:@"%@", @(indexPath.row)];

    

    return cell;

}


- (IBAction)change:(id)sender {

    NSLog(@"change");

    [self.testView setBackgroundColor:[UIColor yellowColor]];

    

    CGRect rect = self.testView.frame;

    rect.origin.x = 100;

    rect.size.width = 200;

    rect.size.height = 150;

    self.testView.frame =rect;

    

}


@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值