所以我试图绘制一些横向一直向下到底的网格线,但是当我切换到横向时,图形不会跟随并且网格线变小。
我已经设置了
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
然而它仍然不起作用,这是我的代码。谁能发现问题?这是自定义视图文件,视图控制器是默认的,除了上面返回yes的代码。
.h file
#import <UIKit/UIKit.h>
#define kGraphHeight 300
#define kDefaultGraphWidth 900
#define kOffsetX 10
#define kStepX 50
#define kGraphBottom 300
#define kGraphTop 0
@interface GraphView : UIView
@end
这是实施
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 0.6);
CGContextSetStrokeColorWithColor(context, [[UIColor lightGrayColor] CGColor]);
// How many lines?
int howMany = (kDefaultGraphWidth - kOffsetX) / kStepX;
// Here the lines go
for (int i = 0; i < howMany; i++)
{
CGContextMoveToPoint(context, kOffsetX + i * kStepX, kGraphTop);
CGContextAddLineToPoint(context, kOffsetX + i * kStepX, kGraphBottom);
}
CGContextStrokePath(context);
}
任何帮助将不胜感激
btw我正在学习本教程
http://buildmobile.com/creating-a-graph-with-quartz-2d/#fbid=YDPLqDHZ_9X
答案 0 :(得分:0)
你需要做三件事:
kGraphTop
和kGraphBottom
常量表示它仅从0到300像素绘制。你可以让kGraphBottom
更大,但这不可靠。相反,您希望找到视图边界的大小并从上到下填充它们。以下是填写边界的方法:
- (void) drawRect:(CGRect)rect
{
// Get the size of the view being drawn to.
CGRect bounds = [self bounds];
CGContextSetLineWidth(context, 0.6);
CGContextSetStrokeColorWithColor(context, [[UIColor lightGrayColor] CGColor]);
// How many lines?
int howMany = (bounds.size.width - kOffsetX) / kStepX;
// Here the lines go
for (int i = 0; i < howMany; i++)
{
// Start at the very top of the bounds.
CGContextMoveToPoint(context, bounds.origin.x+kOffsetX + i * kStepX, bounds.origin.y);
// Draw to the bottom of the bounds.
CGContextAddLineToPoint(context, bounds.origin.x+kOffsetX + i * kStepX, bounds.origin.y+bounds.size.height);
}
CGContextStrokePath(context);
}