Pie*_*rce 4 iphone xcode drawrect ios swift
我对swift很新,而且我一直以编程方式完成所有编码.我终于扩展并开始使用Interface Builder学习一些很棒的东西,什么不是.
我在使用drawRect(rect:CGRect)函数创建的UIView类中创建了一些很酷的自定义绘图,现在我希望能够在循环中多次调用该类以在我的视图中进行布局.每当我尝试以编程方式实例化视图时,似乎没有调用drawRect.我没有得到任何错误,没有什么是绘画.这是我的用于布局自定义视图的代码,其中TesterView是我自定义的UIView子类,它执行自定义绘图:
func testView() {
let testerView:TesterView = TesterView()
self.view.addSubview(testerView)
testerView.translatesAutoresizingMaskIntoConstraints = false
let height = NSLayoutConstraint(item: testerView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200)
let width = NSLayoutConstraint(item: testerView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200)
let centerX = NSLayoutConstraint(item: testerView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
let bottom = NSLayoutConstraint(item: testerView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)
self.view.addConstraints([height, width, centerX, bottom])
}
Run Code Online (Sandbox Code Playgroud)
任何和所有的帮助将不胜感激.我还没有开始尝试在循环中调用视图,因为我甚至无法让它工作一次.我最终需要做的是循环并多次实例化该类.
Arc*_*676 11
你没有drawRect:明确打电话.而是将needsDisplay属性设置为YES.
从View Programming Guide:创建自定义视图
导致视图重新显示的最常见方法是告诉它图像无效.[...]
NSView定义了两种将视图图像标记为无效的方法:setNeedsDisplay:它使视图的整个边界矩形无效,并使视图的setNeedsDisplayInRect:一部分无效.
当视图的实际内容发生变化时,您有责任通知系统您的视图需要重新绘制.您可以通过调用视图
setNeedsDisplay或视图setNeedsDisplayInRect:方法来完成此操作.
- (void)drawRect:(CGRect)rect
[...]
你永远不应该直接调用这种方法.要使视图的一部分无效,从而导致重绘该部分,请调用setNeedsDisplay或setNeedsDisplayInRect:方法.
您可以实现绘图,drawRect:然后[myView setNeedsDisplay:YES]在需要重绘视图时调用(例如,对于游戏,在循环中).
| 归档时间: |
|
| 查看次数: |
3723 次 |
| 最近记录: |