- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在网格中显示水平和/或垂直线。所以我创建了一个 UIView
子类 GridLines
,它在 drawRect:
中绘制线条。然后我创建其中两个(一个垂直,一个不是),并将它们作为 subview 添加到我的主视图 (Canvas
) 中,也是 UIView
派生的。我添加到 Canvas
的其他自定义 subview 已正确显示、删除等,但 GridLines
对象不是,它们的 drawRect:
永远不会被叫到。当我在 [Canvas drawRect:]
(现已禁用)中使用此画线代码时,它会正确显示网格。
查看类似的问题,似乎大多数有此问题的人都在非 UIView
派生类中调用 drawRect
,但我的是 UIView
.
我是不是漏掉了什么?
网格线.h:
#import <UIKit/UIKit.h>
@interface GridLines : UIView
{
BOOL mVertical;
}
- (id) initWithFrame: (CGRect) _frame vertical: (BOOL) _vertical;
@end
网格线.m:
#import "GridLines.h"
@implementation GridLines
- (id) initWithFrame: (CGRect) _frame vertical: (BOOL) _vertical
{
if ((self = [super initWithFrame: _frame]))
{
mVertical = _vertical;
}
return self;
}
- (void) drawRect: (CGRect) _rect
{
NSLog(@"[GridLines drawRect]");
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor clearColor] set];
UIRectFill([self bounds]);
if (mVertical)
{
for (int i = 50; i < self.frame.size.width; i += 50)
{
CGContextBeginPath(context);
CGContextMoveToPoint(context, (CGFloat)i, 0.0);
CGContextAddLineToPoint(context, (CGFloat)i, self.frame.size.height);
[[UIColor grayColor] setStroke];
CGContextSetLineWidth(context, 1.0);
CGContextDrawPath(context, kCGPathFillStroke);
}
}
else
{
for (int j = 50; j < self.frame.size.height; j += 50)
{
CGContextBeginPath(context);
CGContextMoveToPoint(context, 0.0, (CGFloat)j);
CGContextAddLineToPoint(context, self.frame.size.width, (CGFloat)j);
[[UIColor grayColor] setStroke];
CGContextSetLineWidth(context, 1.0);
CGContextDrawPath(context, kCGPathFillStroke);
}
}
}
@end
在另一个 UIView 派生类中:
- (void) createGrids
{
mHorizontalLines = [[GridLines alloc] initWithFrame: self.frame vertical: NO];
mVerticalLines = [[GridLines alloc] initWithFrame: self.frame vertical: YES];
[self addSubview: mHorizontalLines];
[self addSubview: mVerticalLines];
}
最佳答案
您可能遇到了frame v bounds
问题。
mHorizontalLines = [[GridLines alloc] initWithFrame: self.frame vertical: NO];
mVerticalLines = [[GridLines alloc] initWithFrame: self.frame vertical: YES];
frame
保存在 superview 的坐标空间中,如果 self.frame
的偏移大于它的宽度,当您将这些新 View 设置到该框架时,它们'将被裁剪到 self
的 rect。
尝试将其更改为 self.bounds
,它应该有 origin = {0,0}
关于iphone - UIView 的 drawRect 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6742070/
为了澄清这个问题的目的:我知道如何使用 subview 和使用 drawRect 创建复杂的 View 。我试图完全理解何时以及为什么使用一个而不是另一个。 我也明白提前优化并在进行任何分析之前以更困
我是 Swift 的新手,正在尝试让一个非常简单的应用程序运行。我想要做的就是让 UIView.drawRect 在我按下按钮时更新。它会在应用程序首次加载时更新/绘制,然后无论我尝试什么,它都不会更
我有一个名为 Icon 的基于 UIView 的类。在那里,我想要类似于这样的代码: UIView *finalView = [[UIView alloc] initWithFrame:CGRectM
我正在覆盖 drawRect: 在我的一个 View 中,它甚至在不调用 [super drawrect:rect] 的情况下也能工作。这是如何运作的? - (void)drawRect:(CGRec
我不是很明白CALayer的display和drawInContext与 View 中的drawRect有什么关系。 如果我有一个每 1 秒设置一次 [self.view setNeedsDispla
我在 viewController 上放置了一个 View ,我想在此 View 中绘制一个矩形,但具有自定义高度。我根据 viewController 中的参数确定高度。例如。如果我的参数是 50,
这个非常简单的绘图命令创建了一个 11x11 像素的跟踪红色矩形: _sp.graphics.lineStyle( 1, 0xFF0000, 1, true, LineScaleMode.NORMAL
我每两秒从 NSTimer 函数调用 setNeedsDisplayInRect 一次。这工作得很好,并在随机位置绘制一个具有随机颜色的正方形。然而,在某些情况下,我想在 NSTimer 函数中绘制一
你好,我是新来的,也是 Java 编程的新手。最近我正在尝试在 JPanel 中编写绘图功能。问题是,当我添加新的绘制矩形(只需在 JRadiobutton 中选择绘制矩形,然后拖动空白区域)时,它工
我有一个放在WebView上方的UIView。在这个UIView中,我必须画图,但是应用程序因此错误而崩溃 在LibraryViewController.m中 self.drawView =[[Dr
我正在使用 Java 编写一个文件管理器。我需要将矩形选择添加到我的程序中,就像在 Windows 中一样(以便选择矩形内的多个文件)。我的问题是,每当我向 DrawRect 面板添加布局来放置图标时
我正在使用 UIBezierPath 创建自定义 View 绘画。每当状态根据服务器的响应发生变化时,我想更新自定义 View 填充颜色。我正在使用 fillColor属性作为参数来更新 View 的
我有一个名为 TestView 的 UIView 子类,它重写了 DrawRect ,如下所示: import Foundation import UIKit class TestView: UIVi
我的应用程序中有一个泄漏问题,我正在创建一个颜色的自动释放对象,但通过仪器说问题存在。 Malloc 1 04:12.221.102 32 UIKit +[UIColor allocW
我需要知道是否可以全局覆盖 UI 类的 drawRect。类似于“外观”类属性的东西。本质上,我需要为应用程序中的所有按钮使用我自己的绘图例程。最好我不想继承并且必须在 Storyboard 中的每个
我将绘制一条类似于下图中红色曲线的曲线(可以是贝塞尔曲线或我认为对我的目的最方便的任何曲线)。我想找到曲线上的点(illo 中的蓝点)。这些点很可能是曲线长度的相等部分的划分。 我能找到这些点吗?到目
我有两个 UIView 子类,比如“A 类”和“B 类”。类 A 在其初始化程序中具有代码 [self addSubview:instanceOfClassB];。而在 B 类的 drawRect:
我一直在使用 UITextField 的子类在我的文本字段末尾绘制一个“m3”字符串,但有些东西不起作用。我知道正在调用该方法,因为我已经使用 NSLog 对其进行了测试,我做错了什么? 我的类(cl
我有一个 UITableViewCell 的子类,我正在尝试在 drawRect 中画一条线。 有一个属性separatorColor在子类的标题中: @interface DXDecisionsTa
我将动画代码放在我的 UIView 子类的 drawRect 中。我真的在寻找类似 UIViewController 的 viewWillAppear 的东西。但我想使用 UIView 而不是 UIV
我是一名优秀的程序员,十分优秀!