【问题标题】:Graph not following orientation图表不遵循方向
【发布时间】:2012-03-22 12:27:06
【问题描述】:

所以我正在尝试绘制一些网格线,这些网格线在横向一直到底部,但是当我切换到横向时,图形不跟随并且网格线变小。

我已经设置了

- (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);

}

任何帮助将不胜感激

顺便说一句,我正在关注本教程

http://buildmobile.com/creating-a-graph-with-quartz-2d/#fbid=YDPLqDHZ_9X

【问题讨论】:

    标签: objective-c iphone xcode cocos2d-iphone


    【解决方案1】:

    你需要做三件事:

    1. 在 Interface Builder 中,选择包含图形的视图并将其拖动以填满屏幕。
    2. 在 Interface Builder 中,在视图上设置 Autosizing 遮罩,使其在旋转后继续填充屏幕。您可以更改外部视图的模拟指标以确保发生这种情况。
    3. kGraphTopkGraphBottom 常量意味着它只绘制 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-01
      相关资源
      最近更新 更多