前不久有朋友需要一个启动广告的功能,我说网上有挺多的,他说,看的不是很理想。想让我写一个,于是乎,抽空写了一个,代码通俗易懂,简单的封装了一下,各种事件用block回调的,有俩种样式的广告,一种是全屏广告,另一种是下面露logo的,类似网页新闻的启动广告。依赖SDWebImage
主要用来下载网络的广告图片,一般项目里面网络图片都用的这个框架,所以在此不做过多的阐述。下面让我们来看看我封装的过程,对于新手来说,可以学习一下这种封装的思想。
1.首先建一个继承View的LBLaunchImageAdView
.H文件 代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// // LBLaunchImageAdView.h // LBLaunchImageAd // 技术交流群:534926022(免费) 511040024(0.8/人付费) // Created by gold on 16/6/8. // Copyright © 2016年 Bison. All rights reserved. // iOS开发学习app下载https://itunes.apple.com/cn/app/it-blog-for-ios-developers/id1067787090?mt=8 typedef enum { FullScreenAdType = 1,//全屏的广告 LogoAdType = 0,//带logo的广告 }AdType; #import <UIKit/UIKit.h> #import "UIImageView+WebCache.h" #define mainHeight [[UIScreen mainScreen] bounds].size.height #define mainWidth [[UIScreen mainScreen] bounds].size.width typedef void (^LBClick) (NSInteger tag); @interface LBLaunchImageAdView : UIView @property (strong, nonatomic) UIImageView *aDImgView; @property (strong, nonatomic) UIWindow *window; @property (assign, nonatomic) NSInteger adTime; //倒计时总时长,默认6秒 @property (strong, nonatomic) UIButton *skipBtn; @property (nonatomic, copy)LBClick clickBlock; - (instancetype)initWithWindow:(UIWindow *)window andType:(NSInteger)type andImgUrl:(NSString *)url; @end |
里面主要重写了init方法,init方法方便我们在调用封装的类初始化时传递一些参数,在此,我只传递了三个必要的参数,其他参数都用@property属性来调配,达到自己想要的效果,再有就是一个block的回调函数,主要处理各种事件。下面我们看看.m文件里面实现的部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 n-striped-num" data-line="crayon-5812a3c95a6d3351149909-76">76 77 78 79 80
|