2

我正在网上寻找,但找不到明确的答案。您认为使这些功能静态化的最佳方法是什么?

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

2 回答 2

1

我用#define解决了

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

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

于 2014-10-28T16:06:29.710 回答
0

视图控制器.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);
   ...
}
于 2014-10-28T12:07:15.507 回答