iOS开发知识小集

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

2019-06-20  本文已影响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

全剧终

上一篇下一篇

猜你喜欢

热点阅读