Positioning UIWindow in UIScreen in iOS - ios

I need to add additional UIWindow to my App. This UIWindow should always be positioned in the bottom right corner of the screen despite device's orientation, here's the sketch:
I've tried to subclass UIWindow like this to be able to set the size and the margins of window:
#interface MyWindow : UIWindow
#property (nonatomic) CGSize size;
#property (nonatomic) CGFloat margin;
#end
#implementation MyWindow
- (id)initWithSize:(CGSize)size andMargin:(CGFloat)margin {
self.size = size;
self.margin = margin;
return [self initWithFrame:[self calculateFrame]];
}
- (CGRect)calculateFrame {
return CGRectMake([[UIScreen mainScreen] bounds].size.width-self.size.width-self.margin, [[UIScreen mainScreen] bounds].size.height-self.size.height-self.margin, self.size.width, self.size.height);
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self assignObservers];
}
return self;
}
-(void)assignObservers {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(statusBarDidChangeFrame:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
- (void)statusBarDidChangeFrame:(NSNotification *)notification {
[self setFrame:[self calculateFrame]];
}
#end
Everything is great on startup! No matter what orientation is on initial start new UIWindow position is correct. But when I rotate device - my window goes crazy, it jumps to unexpected positions and I can't figure out why.
Please help!

Everything works fine if:
dispatch_async(dispatch_get_main_queue(), ^{
[self setFrame:[self calculateFrame]];
});
So full working code looks like this:
#interface MyWindow : UIWindow
#property (nonatomic) CGSize size;
#property (nonatomic) CGFloat margin;
#end
#implementation MyWindow
- (id)initWithSize:(CGSize)size andMargin:(CGFloat)margin {
self.size = size;
self.margin = margin;
return [self initWithFrame:[self calculateFrame]];
}
- (CGRect)calculateFrame {
return CGRectMake([[UIScreen mainScreen] bounds].size.width-self.size.width-self.margin, [[UIScreen mainScreen] bounds].size.height-self.size.height-self.margin, self.size.width, self.size.height);
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self assignObservers];
}
return self;
}
-(void)assignObservers {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(statusBarDidChangeFrame:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
}
- (void)statusBarDidChangeFrame:(NSNotification *)notification {
dispatch_async(dispatch_get_main_queue(), ^{
[self setFrame:[self calculateFrame]];
});
}
#end
Many thanks to Cocoa-Chat and max.lunin :)

Related

UICollectionView Will Not Update Its Layout On Screen Rotation

I have a custom UIView with a UICollectionView.
On screen rotation I am trying to get the UICollectionView to stretch across the screen, and then redraw its cells.
After I had the data downloaded I tried both [grid setNeedsLayout] and [grid setNeedsDisplay] but that didn't work.
This is what I want to happen:
Portrait
Landscape
(This is also how it appears when the app is started in landscape, but if you change to portrait it doens't update.)
But this is what I get if I start in Portrait mode and switch to Landscape.
I am creating these views programmatically. I am not using any Storyboards.
I have tried:
-(void)viewDidLayoutSubviews {
grid = [[MyThumbnailGridView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2)];
}
I have also tried toying with:
- (void) viewWillLayoutSubviews {
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
//LANDSCAPE
if(grid){
NSLog(#"Grid Needs Landscape Layout");
grid.frame =CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2);
[grid refreshData];
}
}else {
//PORTRIAT
if(grid){
NSLog(#"Grid Needs Portrait Layout");
grid.frame =CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2);
[grid refreshData];
}
}
}
But I can't get it to stretch.
Any help?
MyThumbnailGridView
#interface ViewController () <UINavigationControllerDelegate> {
MyThumbnailGridView *grid;
NSMutableArray * arrImages;
}
- (void)viewDidLoad {
[super viewDidLoad];
arrImages = [NSMutableArray new];
grid = [[MyThumbnailGridView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2)];
//grid = [[MyThumbnailGridView alloc] initWithFrame:CGRectZero];
NSLog(#"showThumbnailGrid Grid View Size: %#", NSStringFromCGRect(grid.frame));
[self.view addSubview:grid];
[self getListOfImages];
}
-(void) getListOfImages {
//Do background task to get images and fill arrImages
[self onDownloadImageDataComplete];
}
- (void) onDownloadImageDataComplete{
grid.imageDataSource = arrImages;
// [grid setNeedsLayout];
// [grid setNeedsDisplay];
}
//...
#end
*MyThumbnailGridView.h
#interface MyThumbnailGridView : UIView
-(id)initWithFrame:(CGRect)frame;
-(void) refreshData;
#property (nonatomic,strong) NSArray *imageDataSource;
#end
*MyThumbnailGridView.m
#interface MyThumbnailGridView () <UICollectionViewDelegate, UICollectionViewDataSource>{
UICollectionView *collectionView;
}
#end
#implementation MyThumbnailGridView
- (instancetype) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(self){
[self customInit];
}
return self;
}
- (void) customInit {
collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:[[MyFlowLayout alloc] init]];
collectionView.delegate = self;
collectionView.dataSource = self;
collectionView.allowsMultipleSelection = NO;
collectionView.showsVerticalScrollIndicator = YES;
[collectionView setBackgroundColor:[UIColor darkGrayColor]];
[collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:#"MyId"];
[self addSubview:collectionView];
}
- (void) refreshData {
NSLog(#"Refresh Grid Data");
[collectionView reloadData];
}
////other code
#end
MyFlowLayout
#interface MyFlowLayout : UICollectionViewFlowLayout
#end
#implementation MyFlowLayout
- (instancetype)init{
self = [super init];
if (self)
{
self.minimumLineSpacing = 1.0;
self.minimumInteritemSpacing = 1.0;
self.scrollDirection = UICollectionViewScrollDirectionVertical;
}
return self;
}
- (CGSize)itemSize {
NSInteger numberOfColumns = 3;
CGFloat itemWidth = (CGRectGetWidth(self.collectionView.frame) - (numberOfColumns - 1)) / numberOfColumns;
return CGSizeMake(itemWidth, itemWidth);
}
#end
MyCollectionViewCell
#interface MyCollectionViewCell : UICollectionViewCell
#property (strong, nonatomic) UIImageView *imageView;
#end
#implementation MyCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.imageView = [UIImageView new];
[self.imageView setContentMode:UIViewContentModeScaleAspectFill];
[self.imageView setClipsToBounds:YES];
[self.imageView setBackgroundColor:[UIColor darkGrayColor]];
[self.contentView addSubview:self.imageView];
}
return self;
}
- (void)prepareForReuse {
[super prepareForReuse];
self.imageView.image = nil;
[self.imageView setHidden:NO];
}
- (void)layoutSubviews {
[super layoutSubviews];
[self.imageView setFrame:self.contentView.bounds];
}
#end
This can be solved with
either you can change the frame of MyThumbnailGridView view in delegate function of orientation or create the view with constraints like this
(void)viewDidLoad {
[super viewDidLoad];
arrImages = [NSMutableArray new];
grid = [[MyThumbnailGridView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2)];
[self.view addSubview:grid];
[self getListOfImages];
}
-(void)viewDidLayoutSubviews
{
if(Once){
Once = NO;
// adding constraints
MyThumbnailGridView.translatesAutoresizingMaskIntoConstraints = NO;
[self.MyThumbnailGridView.widthAnchor constraintEqualToConstant:self.view.frame.size.width].active = YES;
[self.MyThumbnailGridView.heightAnchor constraintEqualToConstant:self.view.frame.size.height/2].active = YES;
[self.MyThumbnailGridView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES;
[self.MyThumbnailGridView.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:self.view.frame.size.height/2].active = YES;
}
}
Also implement this
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[self.view layoutIfNeeded];
[MyThumbnailGridView.collectionView invalidate];
// Do view manipulation here.
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
The problem is that the collection view itself is not changing size when the app rotates. You have given the collection view a fixed frame and then just walked away. So it never changes. So never lays itself out again.
You should give your MyThumbnailGridView and its collection view subview autolayout contraints to their superviews, so that they change size correctly when the app rotates.

scrollViewDidScroll get called when back button pressed and make app crash

i got weird result on my app. when i pressed back button its always call scrollViewDidScroll and it makes my app crash.
error not happen if i'm not scrolling to the bottom (just load the view and then pressed back button).
but when i scrolled to the bottom and then press back button it force quit.
also no error if i'm scrolling to the bottom then scroll again back to the middle of tableview.
here is my code snippet.
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Securities Instructions";
currentPage = 1;
isReloading = NO;
totalPages = 1;
isScrollingEnable = YES;
...... bla bla bla....
[self.tableView reloadData];
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 90, 0); //values passed are - top, left, bottom, right
}
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
NSLog(#"back button pressed");
isScrollingEnable = NO;
}
// [super viewWillDisappear:animated];
}
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
NSLog(#"scroll view called");
if (isScrollingEnable) {
NSLog(#"scroll view get called: scroll enalbe");
CGPoint offset = aScrollView.contentOffset;
CGRect bounds = aScrollView.bounds;
CGSize size = aScrollView.contentSize;
UIEdgeInsets inset = aScrollView.contentInset;
float y = offset.y + bounds.size.height - inset.bottom;
float h = size.height;
float reload_distance = 20;
if(y > h + reload_distance) {
if (isReloading==NO) {
if (totalPages>1) {
if (currentPage<totalPages) {
NSLog(#"scroll view called");
currentPage++;
isReloading = YES;
[self getJsonOnLoad];
}
}
}
}
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"index:%d",buttonIndex);
if (buttonIndex==0) {
if (totalPages!=9999) {
[self.navigationController popViewControllerAnimated:YES];
}
}
}
- (void) getTotalPages{
NSArray *detailArray = [self.jsonResult objectForKey:#"detail"];
int detailCount = [detailArray count];
//server tidak konsisten,parameter totalpages di menu ini tidak ada (menu lain ada) -_-!
if (detailCount>=25) {
if ([jsonHeader objectForKey:#"totalRows"]) {
totalPages = [[jsonHeader objectForKey:#"totalRows"] floatValue]/25;
}else{
//buat unlimited
totalPages = 9999;
}
}
}
//orientation handling
- (void)canRotate{}
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void)viewDidDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationChanged:(NSNotification *)notification{
NSLog(#"orientation change...");
[self adjustViewsForOrientation:[[UIDevice currentDevice] orientation]];
}
- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {
NSLog(#"orientation:%d",orientation);
if (orientation == UIInterfaceOrientationPortrait)
// if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
landscape = NO;
//harus di tambahin ini karena parentnya uiviewcontroller bukan uitableviewcontroller jadi tidak otomatis
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, 320, 480);
[self.tableView layoutSubviews];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[self.tableView reloadData];
//load the portrait view
NSLog(#"portraid");
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[self.navigationController.navigationBar setHidden:NO];
}
// else if (orientation == UIInterfaceOrientationLandscapeLeft)
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
landscape = YES;
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
//harus di tambahin ini karena parentnya uiviewcontroller bukan uitableviewcontroller jadi tidak otomatis
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, 480, 320);
[self.tableView layoutSubviews];
[self.tableView reloadData];
//load the landscape view
NSLog(#"landscape");
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[self.navigationController.navigationBar setHidden:NO];
}
}
in .h
#import <UIKit/UIKit.h>
#interface SecuritiesInstructionsResultViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>{
NSDictionary *jsonHeader;
//untuk paging
NSInteger currentPage;
Boolean *isReloading;
NSInteger totalRows;
float totalPages;
BOOL landscape;
BOOL isScrollingEnable;
}
#property (strong, nonatomic) IBOutlet UITableView *tableView;
//#property NSMutableArray *rowData;
#property NSMutableArray *dataAccount;
#property NSMutableArray *dataCashAmount;
#property NSMutableArray *dataCode;
#property NSMutableArray *dataDate;
#property NSMutableArray *dataExtRef;
#property NSMutableArray *dataInsType;
#property NSMutableArray *dataSecuritiesAmmount;
#property NSMutableArray *dataStatus;
#property NSString *dateFrom;
#property NSString *dateTo;
#property NSDictionary *jsonResult;
#property NSString *selectedAccount;
#property NSString *selectedSecurityCode;
#property NSString *selectedSecuritySecCodeType;
#property NSString *selectedSecurityType;
#property NSString *selectedCurrencyCode;
#property NSString *selectedStatus;
#property NSString *selectedDate;
#property NSString *selectedToDate;
#end
i've prevent to not called scrollviewdidscroll method when back button pressed by implementing viewWillDisappear, but it doesn't help. the app is still crash and no error message.
Any one have a clue for this problem?
Set delegate of UIScrollView or inherited classes (UITableView, UICollectionView and etc) to nil
Objective-C:
- (void)dealloc {
[scrollView setDelegate:nil];
}
Swift:
deinit {
scrollView.delegate = nil
}

Custom Delegate Not Responding iOS

I've setup a delegate to take the old and new position of a gauge. However, I can't seem to get a response from the delegate. I could be missing something but I've done a bunch of research and it seems like everything is in order.
MeterView.h
#import <UIKit/UIKit.h>
#import "KTOneFingerRotationGestureRecognizer.h"
#protocol MeterViewDelegate;
#interface MeterView : UIView{
IBOutlet UIImageView *gauge;
IBOutlet UIImageView *needle;
float rotation, oldPos, newPos;
KTOneFingerRotationGestureRecognizer *rgest;
}
#property (nonatomic, weak) id<MeterViewDelegate> delegate;
- (IBAction)handleMovedNeedle;
#end
#protocol MeterViewDelegate <NSObject>
- (void)meterView:(MeterView*)view OldValue:(float)oldval NewValue:(float)newval;
#end
MeterView.m
#import "MeterView.h"
#import <QuartzCore/QuartzCore.h>
#implementation MeterView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
-(void)handleMovedNeedle{
if ([self.delegate respondsToSelector:#selector(meterView:OldValue:NewValue:)]) {
[self.delegate meterView:self OldValue:oldPos NewValue:newPos];
}
else{
NSLog(#"Delegate call FAIL, needle moved from %f, to %f", oldPos,newPos);
}
}
-(void)awakeFromNib{
gauge = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"gauge.png"]];
needle = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"needle.png"]];
needle.frame = CGRectMake(0,gauge.frame.size.height-(needle.frame.size.height/2), gauge.frame.size.width, needle.frame.size.height);
[self addSubview:gauge];
[self addSubview:needle];
rotation = 0;
rgest = [[KTOneFingerRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
rgest.center = CGPointMake(CGRectGetMidX([needle bounds]) + needle.frame.origin.x, CGRectGetMidY([needle bounds]) + needle.frame.origin.y);
[self addGestureRecognizer:rgest];
}
- (void)rotate:(UIRotationGestureRecognizer *)recognizer {
switch (recognizer.state) {
case UIGestureRecognizerStateBegan: {
oldPos = ([(NSNumber *)[needle.layer valueForKeyPath:#"transform.rotation.z"] floatValue]/3.14)*100;
}
break;
case UIGestureRecognizerStateChanged: {
CGFloat angle = [(NSNumber *)[needle.layer valueForKeyPath:#"transform.rotation.z"] floatValue];
angle += [recognizer rotation];
if (angle >= 0 && angle <= M_PI) {
[needle setTransform:CGAffineTransformRotate([needle transform], [recognizer rotation])];
rotation += [recognizer rotation];
}
}
break;
case UIGestureRecognizerStateEnded: {
newPos = ([(NSNumber *)[needle.layer valueForKeyPath:#"transform.rotation.z"] floatValue]/3.14)*100;
[self handleMovedNeedle];
}
break;
default:
break;
}
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "MeterView.h"
#import "KTOneFingerRotationGestureRecognizer.h"
#interface ViewController : UIViewController<MeterViewDelegate>{
IBOutlet MeterView *secondMeter;
IBOutlet MeterView *thirdMeter;
}
#end
ViewController.m
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#define DEG2RAD(degrees) (degrees * 0.01745327) // degrees * pi over 180
#define RAD2DEG(radians) (radians * 57.2957795) // radians * 180 over pi
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
secondMeter = [[MeterView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
secondMeter.delegate = self;
thirdMeter = [[MeterView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
thirdMeter.delegate =self;
}
-(void)meterView:(MeterView *)view OldValue:(float)oldval NewValue:(float)newval{
NSLog(#"Delegate call SUCCESS, need moved from %f, to %f", oldval,newval);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
This code:
secondMeter = [[MeterView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
secondMeter.delegate = self;
thirdMeter = [[MeterView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
thirdMeter.delegate =self;
creates 2 instances of your view, but it doesn't add them to a view, so they will never be seen.
So, you probably have some other view instances that are on view and don't have delegates, and these views which have delegates but aren't on display. Presumably from an XIB / storyboard as you have outlets.
Connect the views that are on display to the delegate using the outlets rather than creating new instances.
If you make the delegate property in the view an IBOutlet itself then you can connect the view controller as the delegate in the XIB / storyboard and you don't need to worry about the delegate in code at all...

How to release this static variable manually in non-ARC mode

There is a small toast utility source code (https://github.com/Joyfl/JLToast) which seems to use ARC. But I want to use it in manual retain-release (MRR) mode.
In particular, I'm not sure if the center = [[JLToastCenter alloc] init]; (for ARC mode) in +(id)defaultCenter of JLToastCenter.m should be re-writing to center = [[[JLToastCenter alloc] init] autorelease]; (for MRR mode), where the center is declared as static id center = nil;.
In this post, the answer given by #mipadi says that "If the variable is initialized only once, and should stay around for the lifetime of the application, then no, it shouldn't be released (its memory will essentially be freed when the application exits, anyway)". I guess this is the case when the static variable center in JLToastCenter.m but not sure about it.
My own version for MRR mode listed below added release/autorelease/dealloc things. I also changed all dot notation into messaging style.
Source code
Source code list:
JLToastCenter.h
JLToastCenter.m
JLToast.h
JLToast.m
JLToastView.h
JLToastView.m
The JLToastCenter.h file:
#import <Foundation/Foundation.h>
#class JLToast;
#interface JLToastCenter : NSObject
{
NSOperationQueue *_queue;
}
+ (id)defaultCenter;
- (void)addToast:(JLToast *)toast;
#end
The JLToastCenter.m file:
#import "JLToastCenter.h"
#import "JLToast.h"
#implementation JLToastCenter
+ (id)defaultCenter
{
static id center = nil;
static dispatch_once_t onceToken; // It makes singleton object thread-safe
dispatch_once(&onceToken, ^{
center = [[[JLToastCenter alloc] init] autorelease]; // Added autorelease by me, originally as center = [[JLToastCenter alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:center selector:#selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
});
return center;
}
// Added by me
- (void)dealloc
{
[_queue release];
[super dealloc];
}
- (id)init
{
self = [super init];
if( self )
{
_queue = [[NSOperationQueue alloc] init];
[_queue setMaxConcurrentOperationCount:1];
}
return self;
}
- (void)addToast:(JLToast *)toast
{
[_queue addOperation:toast];
}
- (void)deviceOrientationDidChange:(id)sender
{
if( [[_queue operations] count] )
{
[[[[_queue operations] objectAtIndex:0] view] layoutSubviews];
}
}
#end
The JLToast.h file:
#import <UIKit/UIKit.h>
#define JLToastShortDelay 2.0f
#define JLToastLongDelay 3.5f
#class JLToastView;
#interface JLToast : NSOperation
{
BOOL _isExecuting;
BOOL _isFinished;
}
#property (nonatomic, strong) JLToastView *view;
#property (nonatomic, copy) NSString *text; // added by me
#property (nonatomic) NSTimeInterval delay;
#property (nonatomic) NSTimeInterval duration;
+ (id)makeText:(NSString *)text;
+ (id)makeText:(NSString *)text duration:(NSTimeInterval)duration;
+ (id)makeText:(NSString *)text delay:(NSTimeInterval)delay duration:(NSTimeInterval)duration;
- (void)show;
- (void)cancel;
#end
The JLToast.m file:
#import "JLToast.h"
#import "JLToastView.h"
#import "JLToastCenter.h"
#import <dispatch/dispatch.h>
#implementation JLToast
#synthesize view = _view; // added by me
#synthesize text = _text; // added by me
+ (id)makeText:(NSString *)text
{
return [JLToast makeText:text delay:0 duration:JLToastShortDelay];
}
+ (id)makeText:(NSString *)text duration:(NSTimeInterval)duration
{
return [JLToast makeText:text delay:0 duration:duration];
}
+ (id)makeText:(NSString *)text delay:(NSTimeInterval)delay duration:(NSTimeInterval)duration
{
JLToast *toast = [[[JLToast alloc] init] autorelease]; // added autorelease by me
[toast setText:text];
[toast setDelay:delay];
[toast setDuration:duration];
return toast;
}
// added by me
- (void)dealloc
{
[_view release];
[_text release];
[super dealloc];
}
- (id)init
{
self = [super init];
if( self )
{
_view = [[JLToastView alloc] init];
}
return self;
}
- (void)show
{
[[JLToastCenter defaultCenter] addToast:self];
}
- (void)cancel
{
}
#pragma mark -
#pragma mark Getter/Setter
- (NSString *)text
{
return [[_view textLabel] text];
}
- (void)setText:(NSString *)text
{
[[_view textLabel] setText:text];
// [_view layoutSubviews];
}
#pragma mark -
#pragma mark NSOperation Overriding
- (BOOL)isConcurrent
{
return YES;
}
- (void)start
{
if( ![NSThread isMainThread] )
{
[self performSelectorOnMainThread:#selector(start) withObject:nil waitUntilDone:NO];
return;
}
[super start];
}
- (void)main{
[self willChangeValueForKey:#"isExecuting"];
_isExecuting = YES;
[self didChangeValueForKey:#"isExecuting"];
dispatch_async(dispatch_get_main_queue(), ^{ // Non-main thread cannot modify user interface
[_view layoutSubviews]; // Calls layoutSubviews before being-shown. added by the original creator devxoul at around 20131013
[_view setAlpha:0];
[[[UIApplication sharedApplication] keyWindow] addSubview:_view];
[UIView animateWithDuration:0.5 delay:_delay options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[_view setAlpha:1];
} completion:^(BOOL finished) {
[UIView animateWithDuration:_duration animations:^{
[_view setAlpha:1.0001];
} completion:^(BOOL finished) {
[self finish];
[UIView animateWithDuration:0.5 animations:^{
[_view setAlpha:0];
}];
}];
}];
});
}
- (void)finish
{
[self willChangeValueForKey:#"isExecuting"];
[self willChangeValueForKey:#"isFinished"];
_isExecuting = NO;
_isFinished = YES;
[self didChangeValueForKey:#"isExecuting"];
[self didChangeValueForKey:#"isFinished"];
}
- (BOOL)isExecuting
{
return _isExecuting;
}
- (BOOL)isFinished
{
return _isFinished;
}
#end
The JLToastView.h file:
#import <UIKit/UIKit.h>
#interface JLToastView : UIView
#property (nonatomic, strong) UIView *backgroundView;
#property (nonatomic, strong) UILabel *textLabel;
#property (nonatomic) UIEdgeInsets textInsets;
#end
The JLToastView.m file:
#import "JLToastView.h"
#import <QuartzCore/CALayer.h>
#define JLTOAST_LABEL_FONT_SIZE ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) ? 12 : 16)
#define JLTOAST_OFFSET_PORTRAIT_Y ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) ? 30 : 60)
#define JLTOAST_OFFSET_LANDSCAPE_Y ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) ? 20 : 40)
#implementation JLToastView
// added by congliu at 20131031Thu 1000am
- (void)dealloc
{
[_backgroundView release];
[_textLabel release];
[super dealloc];
}
- (id)init
{
self = [super init];
if( self )
{
_backgroundView = [[UIView alloc] initWithFrame:CGRectMake( 0, 0, 100, 100 )];
[_backgroundView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.7]];
[[_backgroundView layer] setCornerRadius:5];
[_backgroundView setClipsToBounds:YES];
[self addSubview:_backgroundView];
_textLabel = [[UILabel alloc] initWithFrame:CGRectMake( 0, 0, 100, 100 )];
[_textLabel setTextColor:[UIColor whiteColor]];
[_textLabel setBackgroundColor:[UIColor clearColor]];
[_textLabel setFont:[UIFont systemFontOfSize:JLTOAST_LABEL_FONT_SIZE]];
[_textLabel setNumberOfLines:0];
[self addSubview:_textLabel];
_textInsets = UIEdgeInsetsMake( 6, 10, 6, 10 );
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat deviceWidth = [[UIScreen mainScreen] bounds].size.width;
UIFont *font = [_textLabel font];
CGSize constraintSize = CGSizeMake( deviceWidth * (280.0f/320.0f), INT_MAX );
CGSize textLabelSize = [[_textLabel text] sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
[_textLabel setFrame:CGRectMake( _textInsets.left, _textInsets.top, textLabelSize.width, textLabelSize.height )];
[_backgroundView setFrame:CGRectMake( 0, 0,
[_textLabel frame].size.width + _textInsets.left + _textInsets.right,
[_textLabel frame].size.height + _textInsets.top + _textInsets.bottom )];
NSInteger x, y, width, height;
CGFloat angle;
switch( [[UIDevice currentDevice] orientation] )
{
case UIDeviceOrientationPortraitUpsideDown:
width = [_backgroundView frame].size.width;
height = [_backgroundView frame].size.height;
x = ([[UIScreen mainScreen] bounds].size.width - width) / 2;
y = JLTOAST_OFFSET_PORTRAIT_Y;
angle = M_PI;
break;
case UIDeviceOrientationLandscapeLeft:
width = [_backgroundView frame].size.height;
height = [_backgroundView frame].size.width;
x = JLTOAST_OFFSET_LANDSCAPE_Y;
y = ([[UIScreen mainScreen] bounds].size.height - height) / 2;
angle = M_PI_2;
break;
case UIDeviceOrientationLandscapeRight:
width = [_backgroundView frame].size.height;
height = [_backgroundView frame].size.width;
x = [[UIScreen mainScreen] bounds].size.width - width - JLTOAST_OFFSET_LANDSCAPE_Y;
y = ([[UIScreen mainScreen] bounds].size.height - height) / 2;
angle = -M_PI_2;
break;
default:
width = [_backgroundView frame].size.width;
height = [_backgroundView frame].size.height;
x = ([[UIScreen mainScreen] bounds].size.width - width) / 2;
y = [[UIScreen mainScreen] bounds].size.height - height - JLTOAST_OFFSET_PORTRAIT_Y;
angle = 0;
break;
}
[self setTransform:CGAffineTransformMakeRotation( angle )];
[self setFrame:CGRectMake( x, y, width, height )];
}
#pragma mark - hit test
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
// NSLog(#"%# hitTest", [self class]);
return nil;
}
#end
Get rid of the autorelease!
[JLToastCenter defaultCenter] should always return the same object. The first time you call it, it creates the object. (That's called 'lazy initialisation' because you create the object only when needed) Then it stores a pointer to the shared object in a static variable to keep it around.
If you would add the autorelease, the object would be created and released the next time the current autoreleasepool is drained. Then the static variable would contain a pointer to a released object. The next time you would then call [JLToastCenter defaultCenter], and then send a message to the released object, all kinds of things could happen (your app will probably crash).
The answer to the question that you linked to applies exactly to your case. Due to the
dispatch_once(),
center = [[JLToastCenter alloc] init]; // correct
is executed exactly once in the lifetime of your application,
when defaultCenter is called the first time.
Subsequent calls to defaultCenter just return the contents of the center variable,
so you want that object to stay alive.
With
center = [[[JLToastCenter alloc] init] autorelease]; // wrong
the object would be released (and potentially deallocated) as soon as the program control returns to the main
event loop and the current autorelease pool ends.
Therefore no autorelease here! (But WHY do you want to convert a project from ARC to MRC??)

iAds trough adWhirl not fully loading

I just implemented adWhirl to my app with iAds and adMob. Everything compiles correctly and adMob works perfectly, but my iAd's are not being sized correctly. the ad looks like its the right size, but it actually appears to be cut off. About 1/4 of the ad seems like it is missing. Since i have no bugs i don't know exactly where to look to fix this.
here is a screenshot of what my ad bar looks like.
http://imgur.com/waPPD
any help or just a nudge in the right direction would be appreciated!
here is the AdWhirlAdapteriAd.h
#import "AdWhirlAdNetworkAdapter.h"
#import <iAd/ADBannerView.h>
#interface AdWhirlAdapterIAd : AdWhirlAdNetworkAdapter <ADBannerViewDelegate> {
NSString *kADBannerContentSizeIdentifierPortrait;
NSString *kADBannerContentSizeIdentifierLandscape;
}
+ (AdWhirlAdNetworkType)networkType;
#end
here is AdWhirlAdapteriAd.m
#import "AdWhirlAdapterIAd.h"
#import "AdWhirlAdNetworkConfig.h"
#import "AdWhirlView.h"
#import "AdWhirlLog.h"
#import "AdWhirlAdNetworkAdapter+Helpers.h"
#import "AdWhirlAdNetworkRegistry.h"
#implementation AdWhirlAdapterIAd
+ (AdWhirlAdNetworkType)networkType {
return AdWhirlAdNetworkTypeIAd;
}
+ (void)load {
if(NSClassFromString(#"ADBannerView") != nil) {
[[AdWhirlAdNetworkRegistry sharedRegistry] registerClass:self];
}
}
- (void)getAd {
ADBannerView *iAdView = [[ADBannerView alloc] initWithFrame:CGRectZero];
kADBannerContentSizeIdentifierPortrait =
&ADBannerContentSizeIdentifierPortrait != nil ?
ADBannerContentSizeIdentifierPortrait :
ADBannerContentSizeIdentifierPortrait;
kADBannerContentSizeIdentifierLandscape =
&ADBannerContentSizeIdentifierLandscape != nil ?
ADBannerContentSizeIdentifierLandscape :
ADBannerContentSizeIdentifierPortrait;
iAdView.requiredContentSizeIdentifiers = [NSSet setWithObjects:
kADBannerContentSizeIdentifierPortrait,
kADBannerContentSizeIdentifierLandscape,
nil];
UIDeviceOrientation orientation;
if ([self.adWhirlDelegate respondsToSelector:#selector(adWhirlCurrentOrientation)]) {
orientation = [self.adWhirlDelegate adWhirlCurrentOrientation];
}
else {
orientation = [UIDevice currentDevice].orientation;
}
if (UIDeviceOrientationIsLandscape(orientation)) {
iAdView.currentContentSizeIdentifier = kADBannerContentSizeIdentifierLandscape;
}
else {
iAdView.currentContentSizeIdentifier = kADBannerContentSizeIdentifierPortrait;
}
[iAdView setDelegate:self];
self.adNetworkView = iAdView;
[iAdView release];
}
- (void)stopBeingDelegate {
ADBannerView *iAdView = (ADBannerView *)self.adNetworkView;
if (iAdView != nil) {
iAdView.delegate = nil;
}
}
- (void)rotateToOrientation:(UIInterfaceOrientation)orientation {
ADBannerView *iAdView = (ADBannerView *)self.adNetworkView;
if (iAdView == nil) return;
if (UIInterfaceOrientationIsLandscape(orientation)) {
iAdView.currentContentSizeIdentifier = kADBannerContentSizeIdentifierLandscape;
}
else {
iAdView.currentContentSizeIdentifier = kADBannerContentSizeIdentifierPortrait;
}
// ADBanner positions itself in the center of the super view, which we do not
// want, since we rely on publishers to resize the container view.
// position back to 0,0
CGRect newFrame = iAdView.frame;
newFrame.origin.x = newFrame.origin.y = 0;
iAdView.frame = newFrame;
}
- (BOOL)isBannerAnimationOK:(AWBannerAnimationType)animType {
if (animType == AWBannerAnimationTypeFadeIn) {
return NO;
}
return YES;
}
- (void)dealloc {
[super dealloc];
}
#pragma mark IAdDelegate methods
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
// ADBanner positions itself in the center of the super view, which we do not
// want, since we rely on publishers to resize the container view.
// position back to 0,0
CGRect newFrame = banner.frame;
newFrame.origin.x = newFrame.origin.y = 0;
banner.frame = newFrame;
[adWhirlView adapter:self didReceiveAdView:banner];
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[adWhirlView adapter:self didFailAd:error];
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication: (BOOL)willLeave {
[self helperNotifyDelegateOfFullScreenModal];
return YES;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner {
[self helperNotifyDelegateOfFullScreenModalDismissal];
}
#end
Here is where the ads are being called in the app
MainMenuInterface.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "GameManager.h"
#import "AdWhirlView.h"
#import "AdWhirlDelegateProtocol.h"
#import "Reading_FluencyAppDelegate.h"
#import "RootViewController.h"
enum GameStatePP {
kGameStatePlaying,
kGameStatePaused
};
#interface MainMenuInterface : CCLayer <AdWhirlDelegate>
{
CCMenu *mainMenu;
CCMenu *aboutPage;
RootViewController *viewController;
AdWhirlView *adWhirlView;
enum GameStatePP _state;
}
#property(nonatomic,retain) AdWhirlView *adWhirlView;
#property(nonatomic) enum GameStatePP state;
-(void)displayStartButton;
#end
and here is the important stuff in MainMenuInterface.m
- (void)adWhirlWillPresentFullScreenModal {
if (self.state == kGameStatePlaying) {
//[[SimpleAudioEngine sharedEngine] pauseBackgroundMusic];
[[CCDirector sharedDirector] pause];
}
}
- (void)adWhirlDidDismissFullScreenModal {
if (self.state == kGameStatePaused)
return;
else {
self.state = kGameStatePlaying;
//[[SimpleAudioEngine sharedEngine] resumeBackgroundMusic];
[[CCDirector sharedDirector] resume];
}
}
- (NSString *)adWhirlApplicationKey {
return #"23myapplicationkey39203924";
}
- (UIViewController *)viewControllerForPresentingModalView {
return viewController;
}
-(void)adjustAdSize {
[UIView beginAnimations:#"AdResize" context:nil];
[UIView setAnimationDuration:0.2];
CGSize adSize = [adWhirlView actualAdSize];
CGRect newFrame = adWhirlView.frame;
newFrame.size.height = adSize.height;
CGSize winSize = [CCDirector sharedDirector].winSize;
newFrame.size.width = winSize.width;
newFrame.origin.x = (self.adWhirlView.bounds.size.width - adSize.width)/2;
newFrame.origin.y = (winSize.height - adSize.height);
adWhirlView.frame = newFrame;
[UIView commitAnimations];
}
- (void)adWhirlDidReceiveAd:(AdWhirlView *)adWhirlVieww {
[adWhirlView rotateToOrientation:UIInterfaceOrientationLandscapeRight];
[self adjustAdSize];
}
-(void)onEnter {
viewController = [(Reading_FluencyAppDelegate *)[[UIApplication sharedApplication] delegate] viewController];
self.adWhirlView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
self.adWhirlView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
[adWhirlView updateAdWhirlConfig];
CGSize adSize = [adWhirlView actualAdSize];
CGSize winSize = [CCDirector sharedDirector].winSize;
self.adWhirlView.frame = CGRectMake((winSize.width/2)-(adSize.width/2),winSize.height- adSize.height,winSize.width,adSize.height);
self.adWhirlView.clipsToBounds = YES;
[viewController.view addSubview:adWhirlView];
[viewController.view bringSubviewToFront:adWhirlView];
[super onEnter];
}
-(void)onExit {
if (adWhirlView) {
[adWhirlView removeFromSuperview];
[adWhirlView replaceBannerViewWith:nil];
[adWhirlView ignoreNewAdRequests];
[adWhirlView setDelegate:nil];
self.adWhirlView = nil;
}
[super onExit];
}
-(void)dealloc
{
self.adWhirlView.delegate = nil;
self.adWhirlView = nil;
[super dealloc];
}
Maybe the winSize property for your sharedDirector still thinks your in portrait? What if you flipped it so you had:
newFrame.size.width = winSize.height;
newFrame.origin.x = (self.adWhirlView.bounds.size.width - adSize.width)/2;
newFrame.origin.y = (winSize.width - adSize.height);
adWhirlView.frame = newFrame;
for those that need to know in the future, my problem turned out to be that it was calling ads for landscape instead of portrait, than when it called adjustAdSize() it wasnt getting correct sizing.
i changed
- (void)adWhirlDidReceiveAd:(AdWhirlView *)adWhirlVieww {
[adWhirlView rotateToOrientation:UIInterfaceOrientationLandscapeRight];
[self adjustAdSize];
{
to
- (void)adWhirlDidReceiveAd:(AdWhirlView *)adWhirlVieww {
[adWhirlView rotateToOrientation:UIInterfaceOrientationPortrait];
[self adjustAdSize];
{
and it fixed all my problems!

Resources