美文网首页iOS开发知识小集
iOS UIImageView实现一个呼吸灯、仿华为banner

iOS UIImageView实现一个呼吸灯、仿华为banner

作者: 邓布利多教授 | 来源:发表于2019-06-20 11:23 被阅读61次

    首先感谢华为提供的两张图片

    https://www.huaweicloud.com/content/dam/cloudbu-site/archive/china/zh-cn/banner/index_banner/2019/0619-modelartsshiyanban/liang-0619.jpg
    https://www.huaweicloud.com/content/dam/cloudbu-site/archive/china/zh-cn/banner/index_banner/2019/0619-modelartsshiyanban/an-0619.jpg

    如果冒犯,请联系我删除,谢谢!

    • 开始表演

    .h文件

    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface AnimationViewController : UIViewController
    
    @end
    
    NS_ASSUME_NONNULL_END
    

    .m文件

    #import "AnimationViewController.h"
    
    @interface AnimationViewController ()
    
    @property (nonatomic, strong) UIImageView *img2;
    @property (nonatomic) NSInteger time;
    
    @end
    
    @implementation AnimationViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        self.view.backgroundColor = [UIColor whiteColor];
        _time = 2;
        
        UIImageView *img1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100, 1980 * 0.2, 1080 * 0.2)];
        img1.image = [UIImage imageNamed:@"img0"];
        [self.view addSubview:img1];
        
        _img2 = [[UIImageView alloc]initWithFrame:img1.frame];
        _img2.image = [UIImage imageNamed:@"img1"];
        [self.view addSubview:_img2];
        
        //开始动画
        [self GraduallyDarken];
        
    }
    
    #pragma mark - 变暗
    -(void)GraduallyDarken{
        
        [UIView animateWithDuration:_time animations:^{
            
            self.img2.alpha = 1;
            self.img2.alpha = 0.1;
            
        } completion:^(BOOL finished) {
            [self GraduallyBrighten];
        }];
        
    }
    
    #pragma mark - 变亮
    -(void)GraduallyBrighten{
        
        [UIView animateWithDuration:_time animations:^{
            
            self.img2.alpha = 0.1;
            self.img2.alpha = 1;
            
        } completion:^(BOOL finished) {
            [self GraduallyDarken];
        }];
        
    }
    
    @end
    

    效果图

    呼吸灯.gif
    • 表演结束

    全剧终

    相关文章

      网友评论

        本文标题:iOS UIImageView实现一个呼吸灯、仿华为banner

        本文链接:https://www.haomeiwen.com/subject/iifdqctx.html