Minutes to solve the iOS development in App start advertising function

Minutes to solve the iOS development in App start advertising function

Not long ago, there is a need for a friend to start advertising features, I said there are a lot of online, he said, not very ideal to see. I want to write one, then, to write a code, user-friendly, simple packaging, various events with the block callback, has two kinds of styles of advertisements, a full screen advertising, the other one is below the dew logo, similar to “news advertising. SDWebImage is mainly used to download the network advertising images, the general picture of the project which is used within the framework of the network, so do not elaborate on this. Let us take a look at the process of packaging, for beginners, you can learn about the idea of packaging.

1 the first to build a successor to the View LBLaunchImageAdView

.h file code is as follows:

/ / / / LBLaunchImageAdView.h / / LBLaunchImageAd / / technical exchange group: 534926022 (free) 511040024 (0.8/ Created by gold on pay) / / 16/6/8. / / Copyright / / reserved. rights All 2016 Bison. iOS development learning app download https://itunes.apple.com/cn/app/ it-blog-for-ios-developers/id1067787090? Mt=8 typedef enum {FullScreenAdType = 1, LogoAdType = 0 / / full screen advertising, advertising / 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 NSInteger adTime (assign, nonatomic); / / countdown to the total length of 6 seconds, the default @property (strong, nonatomic) UIButton *skipBtn; @property (nonatomic, copy) LBClick clickBlock; (instancetype) - initWithWindow: (UIWindow *) window andType: (NSInteger) type (andImgUrl: NSString * URL; @end)

Inside the main overrides the init method, init method is convenient for us to transfer some of the parameters, in the call package type is initialized here, I just passed three essential parameters, other parameters using the @property attribute to the deployment, in order to achieve the desired effect, there is a block callback function, the main processing of various events. Here we look at the contents of the.M file to achieve

/ / / / LBLaunchImageAdView.m / / LBLaunchImageAd / / technical exchange group: 534926022 (free) 511040024 (0.8/ Created by gold on pay) / / 16/6/8. / / Copyright / / reserved. rights All 2016 Bison. iOS development learning app download https://itunes.apple.com/cn/app/ it-blog-for-ios-developers/id1067787090? Mt=8 #import "LBLaunchImageAdView.h" @interface LBLaunchImageAdView (NSTimer) {*countDownTimer}; @property (strong, nonatomic) NSString *isClick; @property (assign, nonatomic) NSInteger secondsCountDown; / / countdown to the total length of @end @implementation LBLaunchImageAdView (instancetype) initWithWindow: (UIWindow *) window andType: (NSInteger) type andImgUrl: (NSString * URL) {self = [super init]; if (self) {self.window = window _secondsCountDown = 0; [window; makeKeyAndVisible]; / / CGSize = window.bounds.size viewSize to obtain images; / / cross screen set to "Landscape" = *viewOrientation @ NSString @ "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" dict[@ ") {launchImageName =" UILaunchImageName "];} UIImage * launchImage imageNamed:launchImageName]} = [UIImage; self.backgroundColor = [UIColor colorWithPatternImage:launchImage]; self.frame = CGRectMake (0, 0, mainWidth, mainHeight); if (type = = FullScreenAdType) {self.aDImgView [[UIImageView = alloc]initWithFrame:CGRectMake (0, 0, mainWidth, mainHeight);}else{self.aDImgView = [[UIImageView alloc]initWithFrame:CGRectMake (0, 0, mainWidth, mainHeight - mainWidth/3) self.skipBtn [UIButton buttonWithType;} =:UIButtonTypeCustom]; self.skipBtn.frame = CGRectMake (mainWidth - 70, 20, 60, 30); self.skipBtn.backgroundColor [UIColor = brownColor]; self.skipBtn.titleLabel.font = [UIFont s YstemFontOfSize:14]; [self.skipBtn addTarget:self action:@selector (skipBtnClick) forControlEvents:UIControlEventTouchUpInside]; [self.aDImgView addSubview:self.skipBtn]; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: self.skipBtn.bounds byRoundingCorners:UIRectCornerBottomRight UIRectCornerTopRight cornerRadii:CGSizeMake | (15, 15)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.skipBtn.bounds; maskLayer.path = maskPath.CGPath; self.skipBtn.layer.mask = maskLayer; SDWebImageManager = *manager [SDWebImageManager sharedManager]; [manager downloadImageWithURL:[NSURL URLWithString:url] (NSInteger received options:0 progress:^ Size, NSInteger expectedSize) {completed:^} (UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {if (image) setImage:[self imageCompressForWidth:image {[self.aDImgView targetWidth:mainWidth]];}}]; self.aDImgView.tag = 1101; self.aDImgView.backgroundColor = [UIColor redColor]; [self addSubview:self.aDImgView]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector (activiTap:)]; / / allow user interaction self.aDImgView.userInteractionEnabled = YES; [self.aDImgView addGestureRecognizer:tap]; CABasicAnimation *opacityAnimation = [CABasicAn Imation animationWithKeyPath:@ "Opacity"]; opacityAnimation.duration = 0.8; opacityAnimation.fromValue = [NSNumber numberWithFloat:0.0]; opacityAnimation.toValue [NSNumber = numberWithFloat:0.8]; opacityAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn] [self.aDImgView.layer addAnimation:opacityAnimation; forKey:@ "animateOpacity"]; countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector (onTimer) userInfo:nil repeats:YES]; [self.window addSubview:self];} return self;} #pragma mark - (void) - click on Ads activiTap: (UITapGestureRecognizer*) recognizer{_isClick = "@ 1"; [self startcloseAnima Tion] #pragma mark;} - turn off animation - (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.aDImgView.layer addAnimation:opacityAnimation forKey:@ [NSTimer scheduledTimerWithTimeInterval:opacityAnimation.duration "animateOpacity"]; target:self selector:@selector (closeAddImgAnimation) user Info:nil repeats:NO];} - (void) skipBtnClick{_isClick = "@ 2"; [self startcloseAnimation];} #pragma mark - complete closed animation processing events - (void) closeAddImgAnimation {[countDownTimer invalidate]; countDownTimer = nil; self.hidden = YES; self.aDImgView.hidden = YES; self.hidden = YES; if ([_isClick = = 1 {if (integerValue]) self.clickBlock) {// click advertising (1100); self.clickBlock}else if} ([_isClick integerValue] = = 2) {if (self.clickBlock) {// click skip self.clickBlock (1101);}}else{if (self.clickBlock) {// click skip self.clickBlock (1102);}}} - (void) onTime R {if (_adTime = = 0) {_adTime} (_secondsCountDown = 6; if < _adTime) {_secondsCountDown++ [self.skipBtn; setTitle:[NSString stringWithFormat:@%ld, _secondsCountDown] forState:UIControlStateNormal]; | skip "}else{[countDownTimer invalidate]; countDownTimer = nil; [self startcloseAnimation];}} #pragma mark - specifies the width according to the proportion of zoom - (UIImage *) imageCompressForWidth: (UIImage *) sourceImage targetWidth: (CGFloat) defineWidth *newImage CGSize {UIImage = nil; imageSize = sourceImage.size; CGFloat = width imageSize.width; CGFloat height CGFloat = imageSize.height; targetWidth = defineWidth; CGFloat = height (targetHeight / width / target Width); CGSize size = CGSizeMake (targetWidth, targetHeight); CGFloat scaleFactor = 0; CGFloat = scaledWidth targetWidth; CGFloat scaledHeight CGPoint = targetHeight; thumbnailPoint = CGPointMake (0, 0); if (CGSizeEqualToSize (imageSize, size) = = NO) {CGFloat widthFactor = targetWidth / width; CGFloat heightFactor = targetHeight / height; if (widthFactor > heightFactor) {scaleFactor = widthFactor;} else{scaleFactor = heightFactor;} scaledWidth = width * scaleFactor * height; scaledHeight = scaleFactor; if (widthFactor > heightFactor) {thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; }else if (widthFactor < heightFactor) {thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;}} / / UIGraphicsBeginImageContext (size); UIGraphicsBeginImageContextWithOptions (size, NO, [UIScreen, mainScreen].scale); CGRect thumbnailRect = CGRectZero; thumbnailRect.origin = thumbnailPoint; thumbnailRect.size.width = scaledWidth; thumbnailRect.size.height = scaledHeight; [sourceImage = drawInRect:thumbnailRect]; newImage UIGraphicsGetImageFromCurrentImageContext (if); (newImage = = Nil) {NSLog (@ "scale image fail");} UIGraphicsEndImageContext (return); newImage @end;}

UI part because there is no need to reuse the place, so there is no other way out, all in the init method inside, a little bloated. UI do not do too much exposition, it mainly uses a gradient of animation, the use of CABasicAnimation in the opacity, interested friends can look at the source, there is a picture of the reconstruction, to prevent the image deformation.

Below we say how to integrate the function of my package it, very simple, first look at the code:

- (BOOL) application: (UIApplication * application) didFinishLaunchingWithOptions: (NSDictionary * launchOptions) {/ * FullScreenAdType * LogoAdType logo with full screen advertising ads like NetEase advertising, it is worth noting that the picture must start with logo * ImgUrl URL * / LBLaunchImageAdView map picture * adView = [[LBLaunchImageAdView alloc]initWithWindow:self.window andType:LogoAdType andImgUrl:@ "http://www.uisheji.com/wp-content/uploads/2013/04/19/app-design-uisheji-ui-icon20121_55.jpg"]; / / adView.clickBlock = all callback ^ (NSInteger tag) {switch (tag) {case 1100:{(@ NSLog "click callback"); TestViewController *vc = [[TestViewController alloc]init]; vc. View.backgroundColor = [UIColor whiteColor]; [self.window.rootViewController presentViewController:vc animated:YES completion:^{break case 1101:}];}; NSLog (@ "click skip callback"); break case; 1102: NSLog (@ "after the completion of the countdown back"); break; default: break; return YES;}}};

First of all, the first file in the AppDelegate.m #import “LBLaunchImageAdView.h”, and then in the didFinishLaunchingWithOptions method inside the initialization, and finally some click callback event. To this end, explain the end, and finally lost the effect and download the address.

Minutes to solve the iOS development in App start advertising function

Download the address

If you help, please do not begrudge your star and like oh!

Technical exchange group: 534926022 (free) (0.8/)

A recommendation of the development of learning iOS app_____|__| | portal

Copyright Bison all for reprint please retain the original hyperlink address! Otherwise the consequences!