闲来无事,封装了一个广告页,还算比较实用,首先我们要知道如何来添加这个广告,以及添加在哪里,启动页上都有什么,逻辑理通顺以后,在上代码就简单多了。
-(instancetype)initWithWindow:(UIWindow *)window adType:(AdType)adtype{
self = [super init];
if (self) {
_adTimer = 6;
[window makeKeyAndVisible];
//获取启动图片
CGSize viewSize = window.bounds.size;
NSString *viewOrientation = @"Portrait";
NSString *launchImageName = nil;
NSArray *imagesDict = [[[NSBundle mainBundle]infoDictionary]valueForKey:@"UILaunchImages"];
for (NSDictionary *dict in imagesDict) {
CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]]) {
}
}
UIImage *launchImage = [UIImage imageNamed:launchImageName];
self.backgroundColor = [UIColor colorWithPatternImage:launchImage];
self.frame = CGRectMake(0, 0, SWidth, SHeight);
if (adtype == MainScreenAdType) {
self.AdImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SWidth, SHeight)];
}
else{
self.AdImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SWidth, SHeight - SWidth/3)];
}
self.skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.skipButton.frame = CGRectMake(SWidth-70, 20, 60, 30);
self.skipButton.backgroundColor = [UIColor brownColor];
self.skipButton.titleLabel.font = [UIFont systemFontOfSize:14];
[self.skipButton addTarget:self action:@selector(skipClick) forControlEvents:UIControlEventTouchUpInside];
[self.AdImageView addSubview:self.skipButton];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.skipButton.bounds byRoundingCorners:UIRectCornerBottomRight | UIRectCornerTopRight cornerRadii:CGSizeMake(15, 15)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
;
maskLayer.frame = self.skipButton.bounds;
maskLayer.path = maskPath.CGPath;
self.skipButton.layer.mask = maskLayer;
self.AdImageView.tag = 1001;
[self addSubview:self.AdImageView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(avtiveTap:)];
self.AdImageView.userInteractionEnabled = YES;
[self.AdImageView addGestureRecognizer:tap];
CABasicAnimation *opactyAnimation = [CABasicAnimation animationWithKeyPath:@"opactiy"];
opactyAnimation.duration = 0.8;
opactyAnimation.fromValue = [NSNumber numberWithFloat:0.0];
opactyAnimation.toValue = [NSNumber numberWithFloat:0.8];
opactyAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[self.AdImageView.layer addAnimation:opactyAnimation forKey:@"animateOpacity"];
countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
[window addSubview:self];
}
return self;
}
当倒计时结束或者用户点击跳过时的动画
-
(void)startCloseAnimation{
CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.duration = 0.5;
opacityAnimation.fromValue = [NSNumber numberWithFloat:1.0];
opacityAnimation.toValue = [NSNumber numberWithFloat:0.3];
opacityAnimation.removedOnCompletion = NO;
opacityAnimation.fillMode = kCAFillModeForwards;
[self.AdImageView.layer addAnimation:opacityAnimation forKey:@"animateOpacity"];
[NSTimer scheduledTimerWithTimeInterval:opacityAnimation.duration target:self selector:@selector(closeAddImgAnmation) userInfo:nil repeats:NO];
}
加载需要弹出的广告的URL- (void)setImageUrl:(NSString *)imageUrl{
_imageUrl = imageUrl;
if (_imageUrl) {
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:[NSURL URLWithString:_imageUrl] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { if (image) { [self.AdImageView setImage:[self imageCompressForWidth:image targetWidth:SWidth]]; } }];
}
}