Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // CircleJump
- //
- // Created by Aleksandar Angelov on 10/27/14.
- // Copyright (c) 2014 Aleksandar Angelov. All rights reserved.
- //
- import UIKit
- import iAd
- class ViewController: UIViewController,ADBannerViewDelegate,ADInterstitialAdDelegate,GADBannerViewDelegate, GADInterstitialDelegate,UIActionSheetDelegate {
- //MARK: - Variables -
- @IBOutlet var _playButton:UIButton!;
- @IBOutlet var _frontCircleImg:UIImageView!;
- @IBOutlet var _backCircleImg:UIImageView!;
- @IBOutlet var _firstLineImg:UIImageView!;
- @IBOutlet var _scoreBackgroundImg:UIImageView!;
- @IBOutlet var _leaderboardButton:UIButton!;
- @IBOutlet var _rateButton:UIButton!;
- @IBOutlet var _musicButton:UIButton!;
- @IBOutlet var _replayButton:UIButton!;
- @IBOutlet var _shareButton:UIButton!;
- @IBOutlet var noAds:UIButton!;
- @IBOutlet var _titleLbl:UILabel!;
- @IBOutlet var _tapLbl:UILabel!;
- @IBOutlet var _scoreLbl:UILabel!;
- @IBOutlet var _bestScoreTextLbl:UILabel!;
- @IBOutlet var _bestScoreLbl:UILabel!;
- @IBOutlet var _gameoverLbl:UILabel!;
- @IBOutlet var _gameoverScoreLbl:UILabel!;
- var _iAdView:UIView!;
- var _iAdInterstitial:ADInterstitialAd!;
- var _googleInterstitial:GADInterstitial!;
- var _iAdBannerView:ADBannerView!;
- var _winSize:CGSize!;
- var _isGoingUp:Bool=false;
- var _isFirstTap:Bool=true;
- var _isInitialJump:Bool=true;
- var _isGameOver:Bool=false;
- var _jumpNo:Int=0;
- var _colorIndex:Int=0;
- var _fallVelocity:CGPoint!;
- var _jumpVelocity:CGPoint!;
- var _lineMoveSpeed:CGPoint!;
- var _fallAcceleration:CGPoint!;
- var _initialCirclePosition:CGPoint!;
- var _updateTimer:NSTimer!;
- var _lineBoundaries:LineBoundaries!;
- var _lineList:NSMutableArray!;
- var _pointList:NSMutableArray!;
- //MARK:- Init -
- override func viewDidLoad() {
- super.viewDidLoad();
- NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseCircleBounceAnimation"), name: ADBackgroundStateChangeNotification, object: nil);
- NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("resumeCircleBounceAnimation"), name: ADForegroundStateChangeNotification, object: nil);
- GamecenterUtils.sharedGamecenterUtils.authenticateLocalUserOnViewController(self);
- _winSize = self.view.frame.size;
- _frontCircleImg.layer.zPosition = 10;
- _backCircleImg.layer.zPosition = -10;
- _firstLineImg.layer.zPosition = -5;
- var circleImgPosition = _frontCircleImg.layer.position;
- _lineBoundaries = LineBoundaries(topLimit: kTopLimit, bottomLimit:kBottomLimit, minimimLength: kMinimumLength, maximumLength: kMaximumLength);
- _initialCirclePosition = circleImgPosition;
- _fallVelocity = isIpad() ? CGPointMake(0, 0.4): CGPointMake(0, 0.2);
- _jumpVelocity = isIpad() ? CGPointMake(0, -1) : CGPointMake(0, -0.5);
- _lineMoveSpeed = kLineMoveSpeed;
- _fallAcceleration = isIpad() ? CGPointMake(0, 0.04): CGPointMake(0, 0.03);
- _bestScoreLbl.text = String(UserDefaultsUtils.sharedUserDefaultsUtils.highscore());
- if(UserDefaultsUtils.sharedUserDefaultsUtils.musicOn()) {
- _musicButton.setImage(UIImage(named: "musicButtonOn.png"), forState: UIControlState.Normal);
- }
- else {
- _musicButton.setImage(UIImage(named: "musicButtonOff.png"), forState: UIControlState.Normal);
- }
- var children = self.view.subviews;
- for child in children {
- if let subview = child as? UIView {
- if (subview.tag == 1) {
- subview.layer.opacity = 0;
- }
- }
- }
- // Remove Ads
- if NSUserDefaults.standardUserDefaults().boolForKey("noAds") {
- admobBannerView.removeFromSuperview()
- }
- UIView.animateWithDuration(0.5, animations: {
- for child in children {
- if let subview = child as? UIView {
- if (subview.tag == 1) {
- subview.layer.opacity = 1;
- }
- }
- }
- });
- self.initLines();
- self.initialCircleBounce();
- if (kBanner != .NoBanner) {
- self.createBanner();
- }
- if (kInterstitial != .NoInterstitial) {
- self.createInterstitial();
- }
- }
- func initLines(){
- let lineHeight:CGFloat = _firstLineImg.frame.size.height;
- var startPosition:CGPoint = CGPointMake(_firstLineImg.layer.position.x+_firstLineImg.frame.size.width/2, _firstLineImg.layer.position.y+lineHeight/2);
- var circleDotImg:UIImageView = UIImageView(image: UIImage(named: "circleDot.png"));
- circleDotImg.frame.size = kCircleSize;
- circleDotImg.layer.position = CGPointMake(startPosition.x, startPosition.y-circleDotImg.frame.size.height/2);
- circleDotImg.layer.zPosition = -5;
- self.view.addSubview(circleDotImg);
- _lineList = NSMutableArray(objects: _firstLineImg);
- _pointList = NSMutableArray(object: circleDotImg);
- for (var i=0;i<kLinesCount;i++) {
- var positionOffset_x = Int(arc4random()%UInt32(_lineBoundaries.maximumLength-_lineBoundaries.minimimLength+1))+Int(_lineBoundaries.minimimLength);
- var positionOffset_y = self.verticalOffset();
- var nextPosition:CGPoint = CGPointMake(startPosition.x+CGFloat(positionOffset_x), startPosition.y+positionOffset_y);
- circleDotImg = UIImageView(image: UIImage(named: "circleDot.png"));
- circleDotImg.frame.size = kCircleSize;
- circleDotImg.layer.position = CGPointMake(nextPosition.x, nextPosition.y-circleDotImg.frame.size.height/2);
- circleDotImg.layer.zPosition = -5;
- self.view.addSubview(circleDotImg);
- var diff:CGPoint = CGPointMake(startPosition.x-nextPosition.x, startPosition.y-nextPosition.y);
- var rads:Float = Float(M_PI)-atan2(Float(diff.y), Float(diff.x));
- //var deg:Float=rads*(Float(180)/Float(M_PI));
- var lineWidth = pointsDistance(startPosition, pointB: nextPosition);
- var newLineImg:UIImageView = UIImageView(frame: CGRectMake(startPosition.x-lineWidth/2,startPosition.y-lineHeight, lineWidth, lineHeight));
- newLineImg.layer.anchorPoint = CGPointMake(0, 0.5);
- newLineImg.image = UIImage(named: "line.png");
- if(diff.y != 0) {
- newLineImg.transform = CGAffineTransformMakeRotation(CGFloat(-rads));
- }
- startPosition = nextPosition;
- newLineImg.layer.zPosition = -5;
- self.view.addSubview(newLineImg);
- _lineList.addObject(newLineImg);
- _pointList.addObject(circleDotImg);
- }
- }
- //MARK:- Touch handler -
- override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
- var touch = touches.anyObject() as UITouch;
- var touchPoint = touch.locationInView(self.view);
- if (_isGameOver) {
- return;
- }
- if (_iAdBannerView != nil) {
- if (CGRectContainsPoint(_iAdBannerView.frame, touchPoint)) {
- return;
- }
- }
- if (_iAdView != nil) {
- if(_iAdView.alpha != 0) {
- return;
- }
- }
- AudioUtils.sharedAudioUtils.playJumpSound();
- if(_isFirstTap){
- self.fadeOutAnimation();
- var frontCircleLayer:CALayer = _frontCircleImg.layer.presentationLayer() as CALayer;
- _frontCircleImg.frame = frontCircleLayer.frame;
- var backCircleLayer:CALayer = _backCircleImg.layer.presentationLayer() as CALayer;
- _backCircleImg.frame = backCircleLayer.frame;
- _frontCircleImg.layer.removeAllAnimations();
- _backCircleImg.layer.removeAllAnimations();
- _isFirstTap = false;
- _updateTimer = NSTimer.scheduledTimerWithTimeInterval( 0.01, target: self, selector: Selector("update"), userInfo: nil, repeats: true);
- _scoreLbl.hidden = false;
- _isGoingUp = true;
- }
- else {
- if(!_isGoingUp) {
- _isGoingUp = true;
- }
- _jumpNo = 0;
- }
- var score:Int = _scoreLbl.text!.toInt()!;
- score+=1;
- if ((score%kColorChangeClicks)==0 && score > 0) {
- var rgbColor = kColorList[_colorIndex];
- _colorIndex++;
- if (_colorIndex == kColorList.count) {
- _colorIndex = 0;
- }
- var red:CGFloat = CGFloat(rgbColor.Red/255);
- var green:CGFloat = CGFloat(rgbColor.Green/255);
- var blue:CGFloat = CGFloat(rgbColor.Blue/255);
- UIView.animateWithDuration(0.5, animations: { () -> Void in
- self.view.backgroundColor = UIColor(red: red, green: green, blue: blue, alpha: 1);
- });
- }
- _scoreLbl.text = String(score);
- }
- //MARK:- Animations -
- func initialCircleBounce(){
- var offset:CGFloat = isIpad() ? 40 : 20;
- _frontCircleImg.layer.position = _initialCirclePosition;
- _backCircleImg.layer.position = _initialCirclePosition;
- UIView.animateWithDuration(1.3,
- delay: 0,
- options: .CurveEaseInOut | .Repeat | .Autoreverse,
- animations: {
- self._frontCircleImg.center = CGPointMake(self._frontCircleImg.center.x, self._frontCircleImg.center.y+offset);
- self._backCircleImg.center = CGPointMake(self._backCircleImg.center.x, self._backCircleImg.center.y+offset);
- }, completion: nil);
- }
- func resumeCircleBounceAnimation(){
- _frontCircleImg.layer.position = _initialCirclePosition;
- _backCircleImg.layer.position = _initialCirclePosition;
- self.initialCircleBounce();
- }
- func pauseCircleBounceAnimation(){
- var frontCircleImgLayer = _frontCircleImg.layer.presentationLayer() as CALayer;
- var backCircleImgLayer = _backCircleImg.layer.presentationLayer() as CALayer;
- _frontCircleImg.frame = frontCircleImgLayer.frame;
- _backCircleImg.frame = backCircleImgLayer.frame;
- frontCircleImgLayer.removeAllAnimations();
- backCircleImgLayer.removeAllAnimations();
- }
- func fadeOutAnimation(){
- UIView.animateWithDuration(0.5, animations:{
- self._titleLbl.layer.opacity = 0;
- self._tapLbl.layer.opacity = 0;
- self._leaderboardButton.layer.opacity = 0;
- self._rateButton.layer.opacity = 0;
- self._musicButton.layer.opacity = 0;
- self._bestScoreTextLbl.layer.opacity = 0;
- self._bestScoreLbl.layer.opacity = 0;
- self.noAds.layer.opacity = 0;
- });
- }
- func fadeInAnimation(){
- UIView.animateWithDuration(0.5, animations:{
- self._frontCircleImg.layer.opacity = 0;
- self._backCircleImg.layer.opacity = 0;
- self._scoreLbl.layer.opacity = 0;
- self._titleLbl.layer.opacity = 1;
- self._gameoverLbl.layer.opacity = 1;
- self._scoreBackgroundImg.layer.opacity = 1;
- self._gameoverScoreLbl.layer.opacity = 1;
- self._replayButton.layer.opacity = 1;
- self._shareButton.layer.opacity = 1;
- self._leaderboardButton.layer.opacity = 1;
- self._rateButton.layer.opacity = 1;
- self._musicButton.layer.opacity = 1;
- self._bestScoreTextLbl.layer.opacity = 1;
- self._bestScoreLbl.layer.opacity = 1;
- self.noAds.layer.opacity = 1;
- });
- }
- func errorAnimation(){
- var errorBackCircle:UIImageView = UIImageView(frame: _backCircleImg.frame);
- errorBackCircle.layer.zPosition = -10;
- errorBackCircle.image = UIImage(named: "errorBackCircle.png");
- errorBackCircle.alpha = 0;
- var errorFrontCircle:UIImageView = UIImageView(frame: _frontCircleImg.frame);
- errorFrontCircle.layer.zPosition = 10;
- errorFrontCircle.image = UIImage(named: "errorFrontCircle.png");
- errorFrontCircle.alpha = 0;
- self.view.addSubview(errorBackCircle);
- self.view.addSubview(errorFrontCircle);
- UIView.animateWithDuration(kBlinkAnimationDuration, delay:0, options:UIViewAnimationOptions.Repeat | UIViewAnimationOptions.Autoreverse, animations: { () -> Void in
- UIView.setAnimationRepeatCount(3);
- errorBackCircle.alpha = 1;
- errorFrontCircle.alpha = 1;
- }, completion: { finished in
- errorBackCircle.alpha = 0;
- errorFrontCircle.alpha = 0;
- self._frontCircleImg.alpha = 0;
- self._backCircleImg.alpha = 0;
- self.fadeInAnimation();
- });
- }
- //MARK:- Button handlers -
- @IBAction func repeatButtonClicked(x:UIButton) {
- if (_iAdBannerView != nil) {
- _iAdBannerView.delegate = nil;
- }
- if (_iAdInterstitial != nil) {
- _iAdInterstitial.delegate = nil;
- }
- if (_googleInterstitial != nil) {
- _googleInterstitial.delegate = nil;
- }
- NSNotificationCenter.defaultCenter().removeObserver(self);
- AudioUtils.sharedAudioUtils.playClickSound();
- UIView.animateWithDuration(0.5, animations: {
- var children = self.view.subviews;
- for child in children {
- if let subview = child as? UIView {
- subview.layer.opacity = 0;
- }
- }
- }, completion: { finished in
- AppDelegate.reset();
- })
- }
- @IBAction func leaderboardButtonClicked(x:UIButton) {
- AudioUtils.sharedAudioUtils.playClickSound();
- GamecenterUtils.sharedGamecenterUtils.showLeaderboardOnViewController(self,leaderboardID:kLeaderboardID);
- }
- @IBAction func musicButtonClicked(x:UIButton) {
- AudioUtils.sharedAudioUtils.playClickSound();
- var musicState:Bool?;
- if(UserDefaultsUtils.sharedUserDefaultsUtils.musicOn()) {
- _musicButton.setImage(UIImage(named: "musicButtonOff.png"), forState: UIControlState.Normal);
- musicState = false;
- }
- else {
- _musicButton.setImage(UIImage(named: "musicButtonOn.png"), forState: UIControlState.Normal);
- musicState = true;
- }
- UserDefaultsUtils.sharedUserDefaultsUtils.setMusic(musicState!);
- }
- @IBAction func noAdsClicked(x:UIButton) {
- /*var alert = UIAlertController(title: "Coming Soon!", message: "In-App Purchases to remove ads are coming soon!", preferredStyle: UIAlertControllerStyle.Alert)
- alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
- self.presentViewController(alert, animated: true, completion: nil)*/
- PFPurchase.buyProduct("com.develop.ring.noads", block: { (error:NSError?) -> Void in
- if error != nil{
- let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)
- alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
- self.presentViewController(alert, animated: true, completion: nil)
- }
- })
- }
- @IBAction func rateButtonClicked(x:UIButton) {
- AudioUtils.sharedAudioUtils.playClickSound();
- var iTunesLink:NSString = "itms-apps://itunes.apple.com/app/id\(kRateAppID)";
- var url = NSURL(string: iTunesLink);
- UIApplication.sharedApplication().openURL(url!);
- }
- @IBAction func shareButtonClicked(x:UIButton) {
- AudioUtils.sharedAudioUtils.playClickSound();
- var message = kSocialMessage;
- var url = NSURL(string: kItunesLink);
- var image = UIImage(named: kSocialImage);
- var activityItems:NSArray = [message,url!,image!];
- var contr = UIActivityViewController(activityItems: activityItems, applicationActivities: nil);
- if (isIpad()){
- contr.popoverPresentationController?.sourceView = self.view;
- var sourceRect = contr.popoverPresentationController?.sourceRect;
- contr.popoverPresentationController?.sourceRect = CGRectMake(0, _winSize.height, _winSize.width, sourceRect!.size.height);
- }
- self.presentViewController(contr, animated: true, completion: nil);
- }
- @IBAction func closeAd(x:UIButton) {
- AudioUtils.sharedAudioUtils.playClickSound();
- UIView.animateWithDuration(0.5, animations: { () -> Void in
- self._iAdView.alpha = 0;
- }, completion: { finished in
- self._iAdView.removeFromSuperview();
- self._iAdInterstitial.delegate = nil;
- self._iAdInterstitial = nil;
- });
- }
- //MARK:- Timers -
- func update(){
- self.jump();
- self.fall();
- self.moveLines();
- self.movePoints();
- self.checkForCollision();
- }
- func checkForCollision() {
- for imgView in _lineList{
- let lineImgView = imgView as UIImageView;
- var touchPoint = CGPointZero;//touch.locationInView(imgView as UIImageView);
- var bottomCollidePoint:CGPoint = CGPointMake(_backCircleImg.layer.position.x, _backCircleImg.layer.position.y+_backCircleImg.frame.size.height/2-(isIpad() ? 16 : 7.5));
- var topCollidePoint:CGPoint = CGPointMake(_backCircleImg.layer.position.x, _backCircleImg.layer.position.y-_backCircleImg.frame.size.height/2+(isIpad() ? 15 : 9));
- if(CGRectContainsPoint(lineImgView.bounds, lineImgView.convertPoint(topCollidePoint, fromView: self.view)) || CGRectContainsPoint(lineImgView.bounds, lineImgView.convertPoint(bottomCollidePoint, fromView: self.view))){
- _updateTimer.invalidate();
- UserDefaultsUtils.sharedUserDefaultsUtils.setHighscore(_scoreLbl.text!.toInt()!);
- GamecenterUtils.sharedGamecenterUtils.reportScore(UserDefaultsUtils.sharedUserDefaultsUtils.highscore(),leaderboardID:kLeaderboardID);
- _gameoverScoreLbl.text = _scoreLbl?.text;
- _bestScoreLbl.text = String(UserDefaultsUtils.sharedUserDefaultsUtils.highscore());
- AudioUtils.sharedAudioUtils.playErrorSound();
- self.errorAnimation();
- _isGameOver = true;
- }
- }
- }
- func moveLines(){
- for line in _lineList{
- let lineImgView = line as UIImageView;
- lineImgView.layer.position = self.addPoints(lineImgView.layer.position, pointB: _lineMoveSpeed);
- let lineImgPos:CGPoint = lineImgView.frame.origin;
- if((lineImgPos.x+lineImgView.frame.width) < 0) {
- if (lineImgView == _firstLineImg) {
- _lineList.removeObject(_firstLineImg);
- }
- else {
- var lastLineImg = _lineList.lastObject as UIImageView;
- var lastPointImg = _pointList.lastObject as UIImageView;
- var preLastPointImg = _pointList.objectAtIndex(_pointList.count-2) as UIImageView;
- _lineList.removeObjectAtIndex(0);
- _lineList.addObject(lineImgView);
- lineImgView.transform = CGAffineTransformMakeRotation(0);
- var nextPosition:CGPoint = lastPointImg.layer.position;
- var diff:CGPoint = CGPointMake(preLastPointImg.layer.position.x-nextPosition.x, preLastPointImg.layer.position.y-nextPosition.y);
- var rads:Float = Float(M_PI)-atan2(Float(diff.y), Float(diff.x));
- var lineWidth = pointsDistance(preLastPointImg.layer.position, pointB: nextPosition);
- var lineHeight = lineImgView.frame.size.height;
- lineImgView.frame = CGRectMake(preLastPointImg.layer.position.x,preLastPointImg.layer.position.y-lineHeight/2, lineWidth, lineHeight);
- if(diff.y != 0) {
- lineImgView.transform = CGAffineTransformMakeRotation(CGFloat(-rads));
- }
- }
- }
- }
- }
- func movePoints(){
- for line in _pointList{
- let pointImgView = line as UIImageView;
- pointImgView.layer.position = self.addPoints(pointImgView.layer.position, pointB: _lineMoveSpeed);
- let pointImgPos:CGPoint = pointImgView.layer.position;
- if((pointImgPos.x+pointImgView.frame.width/2) < 0) {
- var lastPointImg = _pointList.lastObject as UIImageView;
- var lastPointPosition = lastPointImg.layer.position;
- var positionOffset_x = Int(arc4random()%UInt32(_lineBoundaries.maximumLength-_lineBoundaries.minimimLength+1))+Int(_lineBoundaries.minimimLength);
- var positionOffset_y = self.verticalOffset();
- pointImgView.layer.position = CGPointMake(lastPointPosition.x+CGFloat(positionOffset_x), lastPointPosition.y+positionOffset_y);
- _pointList.addObject(pointImgView);
- _pointList.removeObjectAtIndex(0);
- }
- }
- }
- func jump(){
- if(_isGoingUp) {
- _frontCircleImg.layer.position = self.addPoints(_frontCircleImg.layer.position, pointB: _jumpVelocity);
- _backCircleImg.layer.position = self.addPoints(_backCircleImg.layer.position, pointB: _jumpVelocity);
- //_jumpVelocity = self.addPoints(_jumpVelocity, pointB: CGPointMake(0, 0));
- _jumpNo++;
- if(_jumpNo == 30) {
- _jumpNo = 0;
- _isGoingUp = false;
- //_jumpVelocity = CGPointMake(0, -0.7);
- _fallVelocity = isIpad() ? CGPointMake(0, 0.4): CGPointMake(0, 0.2);
- if(_isInitialJump) {
- _isInitialJump = false;
- _jumpVelocity = isIpad() ? CGPointMake(0, -2.3) : CGPointMake(0, -1.2);
- }
- }
- }
- }
- func fall(){
- if(!_isGoingUp) {
- _frontCircleImg.layer.position = self.addPoints(_frontCircleImg.layer.position, pointB: _fallVelocity);
- _backCircleImg.layer.position = self.addPoints(_backCircleImg.layer.position, pointB: _fallVelocity);
- _fallVelocity = self.addPoints(_fallVelocity, pointB:_fallAcceleration);
- }
- }
- //MARK:- Lines Manipulation
- func verticalOffset()->CGFloat{
- var randomSideIndex = arc4random()%101;
- var offset = isIpad() ? Int((arc4random()%40)+50) : Int((arc4random()%20)+30);
- if(randomSideIndex < 50) {
- if(_lineBoundaries.topLimit > offset){
- _lineBoundaries.topLimit -= offset;
- if((_lineBoundaries.bottomLimit+offset)>kBottomLimit){
- _lineBoundaries.bottomLimit = kBottomLimit;
- }
- else {
- _lineBoundaries.bottomLimit += offset;
- }
- offset *= -1;
- }
- else {
- _lineBoundaries.bottomLimit -= offset;
- if((_lineBoundaries.topLimit+offset)>kTopLimit){
- _lineBoundaries.topLimit = kTopLimit;
- }
- else {
- _lineBoundaries.topLimit += offset;
- }
- }
- }
- else {
- if(_lineBoundaries.bottomLimit > offset){
- _lineBoundaries.bottomLimit -= offset;
- _lineBoundaries.topLimit += offset;
- if((_lineBoundaries.topLimit+offset)>kTopLimit){
- _lineBoundaries.topLimit = kTopLimit;
- }
- else {
- _lineBoundaries.topLimit += offset;
- }
- }
- else {
- _lineBoundaries.topLimit -= offset;
- _lineBoundaries.bottomLimit += offset;
- if((_lineBoundaries.bottomLimit+offset)>kBottomLimit){
- _lineBoundaries.bottomLimit = kBottomLimit;
- }
- else {
- _lineBoundaries.bottomLimit += offset;
- }
- offset *= -1;
- }
- }
- return CGFloat(offset);
- }
- //MARK:- ADS -
- //MARK:Interstitial
- func createInterstitial() {
- if(UserDefaultsUtils.sharedUserDefaultsUtils.adsCounter()==0){
- if (kInterstitial == .GoogleInterstitial) {
- _googleInterstitial = GADInterstitial();
- _googleInterstitial.delegate = self;
- _googleInterstitial.adUnitID = kGoogleInterstitialAppUnitID;
- var request = GADRequest();
- //request.testDevices = [GAD_SIMULATOR_ID];
- _googleInterstitial.loadRequest(request);
- }
- else if (kInterstitial == .iAdInterstitial) {
- _iAdInterstitial = ADInterstitialAd();
- _iAdInterstitial.delegate = self;
- }
- }
- UserDefaultsUtils.sharedUserDefaultsUtils.incrementAdsCounter(2);
- }
- func interstitialDidReceiveAd(ad: GADInterstitial!) {
- self.pauseCircleBounceAnimation();
- ad.presentFromRootViewController(self);
- }
- func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
- ad.delegate = nil;
- }
- func interstitialDidDismissScreen(ad: GADInterstitial!){
- self.resumeCircleBounceAnimation();
- }
- func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
- _iAdView = UIView(frame: CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height));
- _iAdView.layer.zPosition = 20;
- _iAdView.alpha = 0;
- self.view.addSubview(_iAdView);
- interstitialAd.presentInView(_iAdView);
- var closeButton = UIButton(frame: CGRectMake(0, 0, 40, 40));
- closeButton.addTarget(self, action: Selector("closeAd:"), forControlEvents: UIControlEvents.TouchDown);
- closeButton.setBackgroundImage(UIImage(named: "closeButton.png"), forState: UIControlState.Normal);
- _iAdView.addSubview(closeButton);
- UIView.animateWithDuration(0.5, animations: { () -> Void in
- self._iAdView.alpha = 1;
- })
- }
- func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
- UIView.animateWithDuration(0.5, animations: { () -> Void in
- self._iAdView.alpha = 0;
- }, completion: { finished in
- self._iAdView.removeFromSuperview();
- self._iAdInterstitial.delegate = nil;
- self._iAdInterstitial = nil;
- });
- }
- func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
- interstitialAd.cancelAction();
- }
- //MARK:Banner
- func createBanner() {
- if (kBanner == .GoogleBanner) {
- var admobBannerView = GADBannerView(adSize: isIpad() ? kGADAdSizeSmartBannerPortrait : kGADAdSizeBanner);
- admobBannerView.rootViewController = self;
- admobBannerView.adUnitID = kGoogleBannerAppUnitID;
- admobBannerView.delegate = self;
- self.view.addSubview(admobBannerView);
- var request = GADRequest();
- //request.testDevices = ["600ecccd6a1c2a4750952facf253ea2f"];
- admobBannerView.loadRequest(request);
- admobBannerView.frame = CGRectMake(0, _winSize.height, _winSize.width, admobBannerView.frame.size.height);
- }
- else if (kBanner == .iAdBanner) {
- _iAdBannerView = ADBannerView(adType: ADAdType.Banner);
- _iAdBannerView.frame = CGRectMake(0, _winSize.height, _iAdBannerView.frame.width, _iAdBannerView.frame.height);
- _iAdBannerView.delegate = self;
- self.view.addSubview(_iAdBannerView);
- }
- }
- func bannerViewDidLoadAd(banner: ADBannerView!) {
- UIView.animateWithDuration(1, animations: { () -> Void in
- banner.frame = CGRectMake(0, self._winSize.height-banner.frame.height, banner.frame.width, banner.frame.height);
- });
- }
- func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
- UIView.animateWithDuration(1, animations: { () -> Void in
- banner.frame = CGRectMake(0, self._winSize.height, banner.frame.width, banner.frame.height);
- })
- }
- func adViewDidReceiveAd(view: GADBannerView!) {
- UIView.animateWithDuration(1, animations: { () -> Void in
- view.frame = CGRectMake(0, self._winSize.height-view.frame.size.height, view.frame.size.width, view.frame.size.height);
- });
- }
- func adView(view: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
- UIView.animateWithDuration(1, animations: { () -> Void in
- view.frame = CGRectMake(0, self._winSize.height, view.frame.size.width, view.frame.size.height);
- });
- }
- //MARK:- Other -
- override func prefersStatusBarHidden() -> Bool {
- return true;
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- }
- func pointsDistance(pointA:CGPoint,pointB:CGPoint)->CGFloat{
- var xDist = (pointB.x - pointA.x);
- var yDist = (pointB.y - pointA.y);
- var distance = sqrt((xDist * xDist) + (yDist * yDist));
- return distance;
- }
- func addPoints(pointA:CGPoint,pointB:CGPoint)->CGPoint{
- return CGPointMake(pointA.x+pointB.x, pointA.y+pointB.y);
- }
- }
- func isIpad()->Bool{
- return UIDevice.currentDevice().userInterfaceIdiom == .Pad;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement