【问题标题】:How to implement Static CGRect and CGFloat如何实现静态 CGRect 和 CGFloat
【发布时间】:2014-10-28 12:02:29
【问题描述】:

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

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

【问题讨论】:

  • 你的UIView类有多个实例?
  • No .. 这是视图 CGRect 只有这些措施

标签: xcode uiview ios8 cgrect cgfloat


【解决方案1】:

我用#define解决了

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

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

【讨论】:

  • 当然,如前所述..还有更多方法可以做任何事情:)
【解决方案2】:

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);
   ...
}

【讨论】:

  • 您好,感谢您的帮助...我只是有一个问题...我需要将常量应用于所有值​​:对于CGRect常量,CGFloat和其他CGFloat
  • 我刚刚通过输入您当前拥有的代码编辑了我的帖子...如您所见,我需要以任何方式指定三个函数..我想避免不得不不断重复 CGRect 等.. 但有一个常数或插入一个#define
  • 哦,好的..我会再做一个答案
  • 你去。那是你要找的吗?
  • 我正在使用视图控制器,但在 UIView 类上工作。我已经更新了 UIView 类中的所有代码
猜你喜欢
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-27
相关资源
最近更新 更多