如何实现Static CGRect和CGFloat

时间:2014-10-28 12:02:30

标签: xcode uiview ios8 cgrect cgfloat

我在网上看,但我找不到明确的答案.. 在您看来,将这些功能设为静态的最佳方法是什么?

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

我需要在我的UIView类中多次调用它们,并且无法弄清楚如何以干净优雅的方式实现它?

示例实际代码

-(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];
}

2 个答案:

答案 0 :(得分:1)

我用#define

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

直接在代码#define中调用它们,适用于所有设备

答案 1 :(得分:0)

ViewController.m

@interface ViewController ()

CGFloat screenwidth; 
CGFloat screenHeight;

@end

@implementation ViewController

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

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

您可以使用#define或者使用declareVariables方法,在任何其他方法之前调用viewDidLoad。这样,只要您需要此值,只需使用

从任何方法调用它们
-(void)showNetworkMessage {
   screenRect = CGRectMake(0, -220, _screenWidth, _screenHeight);
   ...
}

-(void)setHideAnimationNetwork{

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