zoukankan      html  css  js  c++  java
  • iOS-CALayer图片淡入淡出动画

    #import "ViewController.h"

    @interface ViewController ()
    @property (nonatomic,strong)CALayer *imageLayer;

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        UIImage *image1 = [UIImage imageNamed:@"test1.jpg"];
       
        //可以显示图片类容,layer是view的载体
    //    self.view.layer.contents = (__bridge id)(image.CGImage);
        
        
    //创建出图片layer
        self.imageLayer = [CALayer layer];
        self.imageLayer.frame = self.view.bounds;
        [self.view.layer addSublayer:self.imageLayer];
        
        self.imageLayer.contents = (__bridge id)(image1.CGImage);
        [self performSelector:@selector(imageAnimation) withObject:nil afterDelay:3];
    }

    - (void)imageAnimation {
        
        //隐式动画
         UIImage *image2 = [UIImage imageNamed:@"test2.jpg"];
        // self.imageLayer.contents = (__bridge id)(image2.CGImage);
        CABasicAnimation *contentsAnimation = [CABasicAnimation animationWithKeyPath:@"contens"];
        contentsAnimation.fromValue = self.imageLayer.contents;//原始图片
        contentsAnimation.toValue = (__bridge id)(image2.CGImage);//切换后图片
        contentsAnimation.duration = 3.f;
        
        CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
        boundsAnimation.fromValue = [NSValue valueWithCGRect:self.imageLayer.bounds];
        boundsAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(10100200200)];
        boundsAnimation.duration = 3.f;
        
        CAAnimationGroup *group = [CAAnimationGroup animation];
        group.animations = @[contentsAnimation,boundsAnimation];
        group.duration = 3.f;
        //设置layer动画结束后的值否则会回复东湖钱的状态
        self.imageLayer.contents = (__bridge id)(image2.CGImage);
        self.imageLayer.bounds = CGRectMake(10100200200);
        [self.imageLayer addAnimation:group forKey:nil];
        
    }
  • 相关阅读:
    .NET 6.0 —— 网络监视器 (TODO)
    Google adwords api —— report & AWQL
    Linux 镜像更新 为国内镜像源 for debian
    优化代码 —— 二八法则 & 编完代码,再优化
    鳥哥的 Linux 私房菜 ——— 第十八章、 服务的防火墙管理 xinetd, TCP Wrappers(3)
    端口号port 是什么
    aptget的install、update、upgrade的区别(转发)
    Google ads api —— github
    .net 6.00 —— record 类型 (TODO)
    Compiled models —— .NET Core 6.0
  • 原文地址:https://www.cnblogs.com/hxwj/p/4666036.html
Copyright © 2011-2022 走看看