zoukankan      html  css  js  c++  java
  • 通过layer的contents属性来实现uiimageview的淡入切换

    #import "ViewController.h"
    
    @interface ViewController ()
    @property(nonatomic,strong)CALayer *imageLayer;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        
        [super viewDidLoad];
        
        UIImage *image1=[UIImage imageNamed:@"1"];
        self.imageLayer=[CALayer layer];
        self.imageLayer.frame=CGRectMake(0, 0, 375, 300);
        self.imageLayer.contents=(__bridge id)image1.CGImage;
        [self.view.layer addSublayer:self.imageLayer];
        
        [self performSelector:@selector(layerAnimation) withObject:nil afterDelay:3];
    }
    -(void)layerAnimation
    {
        /*隐式动画
        UIImage *image2=[UIImage imageNamed:@"2"];
        self.imageLayer.contents=(__bridge id)image2.CGImage;
        */
        //显示动画,图片动画
        UIImage *image2=[UIImage imageNamed:@"2"];
        CABasicAnimation *basicAnimation=[CABasicAnimation animationWithKeyPath:@"contents"];
        basicAnimation.fromValue=self.imageLayer.contents;
        basicAnimation.toValue=(__bridge id)image2.CGImage;
        basicAnimation.duration=3.0f;
        
        //bounds动画
        CABasicAnimation *boundsAnimation=[CABasicAnimation animationWithKeyPath:@"bounds"];
        boundsAnimation.fromValue=[NSValue valueWithCGRect:self.imageLayer.bounds];
        boundsAnimation.toValue=[NSValue valueWithCGRect:CGRectMake(0, 0, 375/2.0f, 300/2.0f)];
        boundsAnimation.duration=3.0f;
        
        //组合动画
        CAAnimationGroup *groupAnimation=[CAAnimationGroup animation];
        groupAnimation.animations=@[basicAnimation,boundsAnimation];
        groupAnimation.duration=3.0f;
        
        //设置layer动画结束后的值必须设置,否则的话会恢复动画之前的状态
    
        self.imageLayer.contents=(__bridge id)image2.CGImage;
        self.imageLayer.bounds=CGRectMake(0, 0, 375/2.0f, 300/2.0f);
        [self.imageLayer addAnimation:basicAnimation forKey:nil];
        
    }
    
    @end
  • 相关阅读:
    程序员学习视频教程汇总
    分享一个单机软件试用期计时思路
    C# XML转Json Json转XML XML 转对象 对象转XML
    C# 数独 解法
    C# 炸弹人 winform版
    胃食管反流疾病认识总结
    C# Datagridview combox列 初始化颜色
    C# WPF 坦克大战
    高分辨率食道测压(HRM)
    C# wpf 使用 polyline 做一个贪吃蛇游戏的小蛇移动吃食部分功能
  • 原文地址:https://www.cnblogs.com/thbbsky/p/4379491.html
Copyright © 2011-2022 走看看