ios开发——显示动画



#import "ViewController.h"

@interface ViewController ()

@property(strong, nonatomic) UIImageView *plane;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //界面初始化
    CGRect screen = [[UIScreen mainScreen] bounds];

    CGFloat imageWidth = 100;
    CGFloat imageHeight = 100;
    CGFloat imageTopView = 25;
    CGFloat imageLeftView = 20;
    CGRect imageFrame = CGRectMake(imageLeftView, imageTopView, imageWidth, imageHeight);
    //创建Image View对象plane
    self.plane = [[UIImageView alloc] initWithFrame:imageFrame];
    //设置plane的图片属性
    self.plane.image = [UIImage imageNamed:@"clipartPlane.png"];
    //设置plane视图上的层opacity属性
    self.plane.layer.opacity = 0.25;
    //添加plane到当前视图
    [self.view addSubview:self.plane];

    //创建按钮对象
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置按钮正常状态时显示图片
    [button setImage:[UIImage imageNamed:@"ButtonOutline.png"] forState:UIControlStateNormal];
    //设置按钮高亮状态时显示图片
    [button setImage:[UIImage imageNamed:@"ButtonOutlineHighlighted.png"] forState:UIControlStateHighlighted];
    //设置按钮触摸动作输出口
    [button addTarget:self action:@selector(movePlane:) forControlEvents:UIControlEventTouchUpInside];

    CGFloat buttonWidth = 130;
    CGFloat buttonHeight = 50;
    CGFloat buttonTopView = 500;
    button.frame = CGRectMake((screen.size.width - buttonWidth) / 2, buttonTopView, buttonWidth, buttonHeight);
    //添加按钮到当前视图
    [self.view addSubview:button];

}

- (void)movePlane:(id)sender {
    
    //创建opacity动画
    CABasicAnimation *opAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
    //设置动画持续时间
    opAnim.duration = 3.0;
    //设置opacity开始值
    opAnim.fromValue = @0.25;   //数值为0.25的NSNumber对象
    //设置opacity结束值
    opAnim.toValue= @1.0;       //数值为1.0的SNumber对象
    //设置累计上次值
    opAnim.cumulative = YES;
    //设置动画重复2次
    opAnim.repeatCount = 2;
    //设置动画结束时候处理方式
    opAnim.fillMode = kCAFillModeForwards;
    //设置动画结束时是否停止
    opAnim.removedOnCompletion = NO;
    //添加动画到层
    [self.plane.layer addAnimation:opAnim forKey:@"animateOpacity"];
    
    //创建平移仿射变换
    CGAffineTransform moveTransform = CGAffineTransformMakeTranslation(200, 300);
    //创建平移动画
    CABasicAnimation *moveAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
    moveAnim.duration = 6.0;
    //设置结束位置
    moveAnim.toValue= [NSValue valueWithCATransform3D:
                       CATransform3DMakeAffineTransform(moveTransform)];
    moveAnim.fillMode = kCAFillModeForwards;
    moveAnim.removedOnCompletion = NO;
    [self.plane.layer addAnimation:moveAnim forKey:@"animateTransform"];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值