2

I'm looking on the net but I can not find a clear answer ..          In your opinion what is the best way to make these functions static?

CGRect ScreenRect = [[UIScreen mainScreen] bounds]; 
CGFloat screenwidth = screenRect.size.width; 
CGFloat screenHeight = 64; 

I need to call them several times in my UIView class and can not figure out how to implement this in a clean and elegant?

example actual code

-(id)initializeNetworkDetect:(NSString *)detectMessage {
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = 64;

    networkView = [self initWithFrame:CGRectMake(0, -220, screenWidth, screenHeight)];

    if (networkView) {
        networkView.backgroundColor = BCKNETWORKVIEW;
        labelNetwork = [[UILabel alloc] init];
        [labelNetwork setFrame:CGRectMake(0, 4, 320, 30)];
        [labelNetwork setFont:[UIFont boldSystemFontOfSize:11]];
        [labelNetwork setBackgroundColor : [UIColor clearColor]];
        [labelNetwork setTextColor: [UIColor whiteColor]];
        [labelNetwork setAdjustsFontSizeToFitWidth:TRUE];
        [labelNetwork setText:detectMessage];
        labelNetwork.textAlignment = NSTextAlignmentCenter;
        labelNetwork.lineBreakMode = NSLineBreakByWordWrapping;
        labelNetwork.numberOfLines = 2;
        [networkView addSubview:labelNetwork];

        NSString *successAlertString = [[NSString alloc] init];
        successAlertString = detectMessage;

        tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endTapping)];
        tapGesture.cancelsTouchesInView = NO;
        [self setUserInteractionEnabled:YES];
        [self addGestureRecognizer:tapGesture];
    }

    return self;
}

-(void)showNetworkMessage {
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = 64;

    screenRect = CGRectMake(0, 0, screenWidth, screenHeight);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.25];
    [UIView setAnimationDelegate:self];
    self.frame = screenRect;
    [UIView commitAnimations];
}

-(void)setHideAnimationNetwork{

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = 64;

    screenRect = CGRectMake(0, -220, screenWidth, screenHeight);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.25];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(deleteAnimation)];
    self.frame = screenRect;
    [UIView commitAnimations];
}
kAiN
  • 2,559
  • 1
  • 26
  • 54

2 Answers2

1

I'm solved with #define

#define RECT_SCREEN_Width_Size [UIScreen mainScreen].bounds.size.width
#define RECT_SCREEN_Height_Size 64

call them directly in the code #define and is suitable for all devices

kAiN
  • 2,559
  • 1
  • 26
  • 54
0

ViewController.m

@interface ViewController ()

CGFloat screenwidth; 
CGFloat screenHeight;

@end

@implementation ViewController

-(void) viewDidLoad
{
   [self declareVariables];
}

-(void)declareVariables
{
   _screenwidth = screenRect.size.width;
   _screenHeight = 64;
}

You can either use #define or have declareVariables method, that you will call on viewDidLoad, before any other method. This way, whenever you need this value, you simply call them from any method using

-(void)showNetworkMessage {
   screenRect = CGRectMake(0, -220, _screenWidth, _screenHeight);
   ...
}

-(void)setHideAnimationNetwork{

   screenRect = CGRectMake(0, -220, _screenWidth, _screenHeight);
   ...
}
SteBra
  • 4,188
  • 6
  • 37
  • 68
  • Hello and thanks for the help ... I just have a problem ... I need the constant is applied to all values​​: For CGRect constant, CGFloat and other CGFloat – kAiN Oct 28 '14 at 12:19
  • I just edited my post by entering the code you currently own ... as you can see I need to specify in any way the three functions .. I would like to avoid having to continually repeats CGRect etc .. but have a constant or insert a #define – kAiN Oct 28 '14 at 12:34
  • Oh, ok..I'll make another answer – SteBra Oct 28 '14 at 12:35
  • There you go. Is that what you were looking for ? – SteBra Oct 28 '14 at 12:42
  • I'm working on a view controller but on a UIView class .. I have updated with all the code in the class UIView – kAiN Oct 28 '14 at 12:51
  • Again, i'm not realy sure what you're talking about :) – SteBra Oct 28 '14 at 12:54
  • If you want to create the CGRect and CGFloat property and then assign a unique value to these properties for recall ever in the whole class UIView as I do? – kAiN Oct 28 '14 at 12:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/63787/discussion-between-stebra-and-rory). – SteBra Oct 28 '14 at 12:58