Xcode 如何实现静态CGRect和CGFloat

Xcode 如何实现静态CGRect和CGFloat,xcode,uiview,ios8,cgrect,cgfloat,Xcode,Uiview,Ios8,Cgrect,Cgfloat,我在网上查,但找不到明确的答案。。          在您看来,使这些函数保持静态的最佳方法是什么 CGRect ScreenRect = [[UIScreen mainScreen] bounds]; CGFloat screenwidth = screenRect.size.width; CGFloat screenHeight = 64; 我需要在UIView类中多次调用它们,但我不知道如何以干净优雅的方式实现这一点 实际代码示例 -(id)initializeNetworkDet

我在网上查,但找不到明确的答案。。          在您看来,使这些函数保持静态的最佳方法是什么

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];
}
ViewController.m

@interface ViewController ()

CGFloat screenwidth; 
CGFloat screenHeight;

@end

@implementation ViewController

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

-(void)declareVariables
{
   _screenwidth = screenRect.size.width;
   _screenHeight = 64;
}
您可以使用#define或have declareVariables方法,您将在viewDidLoad上调用该方法,然后再调用任何其他方法。这样,无论何时需要这个值,只要使用

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

-(void)setHideAnimationNetwork{

   screenRect = CGRectMake(0, -220, _screenWidth, _screenHeight);
   ...
}
我被“定义”解决了


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

,并且您的
UIView
类有多个实例?否。。这是视图CGRect只有这些测量Hello谢谢你的帮助。。。我只是有个问题。。。我需要将常数应用于所有值​​: 对于CGRect常量、CGFloat和其他CGFloat,我只是通过输入您当前拥有的代码来编辑我的帖子。。。正如您所见,我需要以任何方式指定这三个函数。。我希望避免不断重复CGRect等。。但要有一个常数或插入一个定义,好吧,我再回答一个问题。这就是你要找的吗?我正在开发一个视图控制器,但是在一个UIView类上。。如前所述,我已使用UIViewSure类中的所有代码进行了更新。。有更多方法可以执行任何操作:)
#define RECT_SCREEN_Width_Size [UIScreen mainScreen].bounds.size.width
#define RECT_SCREEN_Height_Size 64