【发布时间】:2014-07-24 05:29:09
【问题描述】:
我有 UIView 子类的代码。
import UIKit
class DrawingSquareView: UIView {
override func drawRect(rect: CGRect)
{
var apertureRect : CGRect = CGRectMake(80, 200, 160 , 158)
var context : CGContextRef = UIGraphicsGetCurrentContext()
/* draw the transparent rect */
CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 0.0)
CGContextSetBlendMode(context, kCGBlendModeCopy)
CGContextFillRect(context, apertureRect)
// /* draw a white border */
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0)
CGContextStrokeRect(context, apertureRect)
}
override func awakeFromNib()
{
var cropButtons : UIButton = UIButton(frame: CGRectMake(70, 150, 160 , 158))
cropButtons.setTitle("Crop Image", forState: UIControlState.Normal)
cropButtons.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
self.addSubview(cropButtons)
}
init(frame: CGRect)
{
super.init(frame: frame)
}
override func hitTest(point: CGPoint, withEvent event: UIEvent!) -> UIView!
{
var view : UIView = self.superview
return view.viewWithTag(99)
}
}
还有这段代码在 UIViewController 类中调用 UIView 子类
@IBAction func CropButtonClicked(sender: UIButton)
{
var DrawingSquareObject : DrawingSquareView = DrawingSquareView(frame:self.view.frame)
self.view.addSubview(DrawingSquareObject)
}
但是当我单击 superview 中的按钮时,它确实显示了我在 UIView 子类中使用 draRect: 创建的矩形,但它显示了我在 awakefromNib() 中创建的 UIButton .我尝试在init(frame:)、init(coder:)、addsubview(view:) 函数中添加子视图,它们都没有显示UIButton。另外我想知道如何从superview 中删除这个DrawingSquareView。
请帮助我,提前谢谢。
【问题讨论】: