简体   繁体   English

使用比例使绘图适合iPad和iPhone

[英]Using ratios to make drawing fit on iPad and iPhone

I'm trying to draw an (American) football field for both an iPad and iPhone device using UIBezierPath , by drawing 12 horizontal lines, two of which mark the beginning of the endzone. 我正在尝试使用UIBezierPath绘制iPad和iPhone设备的(美式)橄榄球场,方法是绘制12条水平线,其中两条线标记为结束区域的起点。

As you can see from below, the first line starts at the far left of the screen (0) and 1/12 of the way down, and extends all the way across (size.bounds.size.width) horizontally. 正如您从下面看到的那样,第一行从屏幕的最左端(0)开始,一直向下到1/12,并一直沿水平(size.bounds.size.width)延伸。 The next line starts at the far left but 2/12 CGPointMake(0, self.bounds.size.height / 12 *2 ) of the way down the view etc. It is calculated in this way for all 12 lines. 下一行从最左端开始,但位于视图等位置的2/12 CGPointMake(0, self.bounds.size.height / 12 *2 ) 。以这种方式为所有12行计算。 I thought, since I was setting the lines using formulas (ie self.bounds.size.height/ 12) rather than hard numbers that the whole field would fit on the view no matter what size screen I was using on either device (iPad or iPhone). 我想,因为我使用公式(即self.bounds.size.height / 12)设置行,而不是硬数字,所以无论我在任一设备(iPad或iPad苹果手机)。

It works that way on iPad, however, when I view it on the iPhone, it shows one endzone at the top of the iPad screen but then only extends to the 20 yard line at the bottom (in portrait view). 它在iPad上以这种方式工作,但是,当我在iPhone上查看它时,它在iPad屏幕的顶部显示了一个结束区域,但是仅延伸到底部的20码线(在纵向视图中)。 The situation is even worse when I view it in the smaller iPhone screen (3.5 inch) in that it only extends to about the 30 yard line at the bottom of the portrait view. 当我在较小的iPhone屏幕(3.5英寸)上查看时,情况甚至更糟,因为它只能延伸到纵向视图底部的大约30码线。

Why isn't it happening the way I planned? 为什么它不按我的计划进行?

Update: the code below is in the - (void)drawRect:(CGRect)rect method in a subclass of UIView, called FieldView. 更新:下面的代码在UIView子类FieldView中的- (void)drawRect:(CGRect)rect方法中。 I connected the UIView to the main view controller as a property @property (strong, nonatomic) IBOutlet FieldView *fieldView; 我将UIView作为属性@property (strong, nonatomic) IBOutlet FieldView *fieldView;连接到主视图控制器@property (strong, nonatomic) IBOutlet FieldView *fieldView; , and call [self.fieldView setNeedsDisplay] in the view controller so that drawRect in FieldView is run. ,然后在视图控制器中调用[self.fieldView setNeedsDisplay] ,以便运行FieldView中的drawRect

Update 2 更新2

The heights for these three lines are 这三行的高度是

2014-04-14 21:47:26.783 qbgeo[2189:a0b] height 85.333336   self.bounds.size.height / 12
2014-04-14 21:47:26.785 qbgeo[2189:a0b] height 170.666672  self.bounds.size.height / 12 * 2
2014-04-14 21:47:26.788 qbgeo[2189:a0b] height 256.000000  self.bounds.size.height / 12 * 3

UIBezierPath *goalLine = [[UIBezierPath alloc] init];
[goalLine moveToPoint:CGPointMake(0, self.bounds.size.height / 12)];
[goalLine addLineToPoint: CGPointMake(self.bounds.size.width, self.bounds.size.height / 12)];
[[UIColor whiteColor] setStroke];
goalLine.lineWidth = kActivationInset2;
[goalLine strokeWithBlendMode:kCGBlendModeNormal alpha:0.5]; 

UIBezierPath *tenYardLine = [[UIBezierPath alloc] init];
[tenYardLine moveToPoint:CGPointMake(0, self.bounds.size.height / 12 *2 )];
[tenYardLine addLineToPoint: CGPointMake(self.bounds.size.width, self.bounds.size.height / 12 * 2)];
[[UIColor whiteColor] setStroke];
tenYardLine.lineWidth = kActivationInset2;
[tenYardLine strokeWithBlendMode:kCGBlendModeNormal alpha:0.5];

UIBezierPath *twentyYardLine = [[UIBezierPath alloc] init];
[twentyYardLine moveToPoint:CGPointMake(0, self.bounds.size.height / 12 *3 )];
[twentyYardLine addLineToPoint: CGPointMake(self.bounds.size.width, self.bounds.size.height / 12 * 3)];
[[UIColor whiteColor] setStroke];
twentyYardLine.lineWidth = kActivationInset2;
[twentyYardLine strokeWithBlendMode:kCGBlendModeNormal alpha:0.5];

This code worked fine for me. 这段代码对我来说很好用。 Try it and see if it works in your hands, 试试看,看看它是否在您手中,

- (void)drawRect:(CGRect)rect {
    for (int i =1; i<12; i++) {
        UIBezierPath *line = [[UIBezierPath alloc] init];
        [line moveToPoint:CGPointMake(0, self.bounds.size.height / 12 * i)];
        [line addLineToPoint: CGPointMake(self.bounds.size.width, self.bounds.size.height / 12 * i)];
        [[UIColor whiteColor] setStroke];
        line.lineWidth = (i==1 || i==11)? 3 : 1;
        [line strokeWithBlendMode:kCGBlendModeNormal alpha:0.5];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM