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

来源:互联网 发布:雪迪龙网络报销系统 编辑:程序博客网 时间:2024/05/22 17:39

//

//  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) IBOutletUITableView *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 {

    [superviewDidLoad];

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

    

    self.headView = [[UIViewalloc] initWithFrame:CGRectMake(0,0, [UIScreenmainScreen].bounds.size.width,100)];

    self.headView.backgroundColor = [UIColorpurpleColor];

    

    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 = [[UIViewalloc] initWithFrame:CGRectMake(20,100, 100, 44)];

    self.testView.backgroundColor = [UIColorredColor];

    [self.viewaddSubview:self.testView];

    [superviewDidLoad];

    // 指定图像的位置

    //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];

//    // 实现平移的动画

   [selftranslationAnimation:point];

   //实现渐变的动画

   [selfalphaChangedAnimation];

    //实现缩放的动画

   [selfscaleAnimation];

    //实现旋转的动画

    [selfrotateAnimation];

    

}

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

- (void)translationAnimation:(CGPoint)point

{

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

        CABasicAnimation *basicAnimation = [CABasicAnimation

                                                                      animationWithKeyPath:@"position"];

       // 设置目标值

        basicAnimation.toValue = [NSValuevalueWithCGPoint:point];

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

        basicAnimation.removedOnCompletion =NO;

        basicAnimation.fillMode =kCAFillModeForwards;

        // 设置动画的时间函数

        basicAnimation.timingFunction = [CAMediaTimingFunction

                                                    functionWithName:kCAMediaTimingFunctionEaseOut];

        // 设置动画的时长

        basicAnimation.duration =2.0f;

        // 添加到iconView的图层

        [self.testView.layeraddAnimation: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.layeraddAnimation:basicAnimation forKey:nil];

}



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

- (void)scaleAnimation

{

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

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

        // 设置初始值和目标值

        basicAnimation.fromValue =@1.0;

        basicAnimation.toValue =@0.5;

        // 设置重复次数

        basicAnimation.repeatCount =MAXFLOAT;

        // 设置动画的时长

        basicAnimation.duration =2.0f;

        // 添加到iconView的图层

        [self.testView.layeraddAnimation: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.layeraddAnimation:basicAnimation forKey:nil];

    }



- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // 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 = [tableViewdequeueReusableCellWithIdentifier:@"Cell"forIndexPath:indexPath];

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

    

   return cell;

}


- (IBAction)change:(id)sender {

    NSLog(@"change");

    [self.testViewsetBackgroundColor:[UIColoryellowColor]];

    

   CGRect rect = self.testView.frame;

    rect.origin.x =100;

    rect.size.width =200;

    rect.size.height =150;

   self.testView.frame =rect;

    

}


@end



0 0
原创粉丝点击