Ios 从UIImageView派生的动态cretated控件缺少函数

Ios 从UIImageView派生的动态cretated控件缺少函数,ios,swift,dynamic,uiimageview,Ios,Swift,Dynamic,Uiimageview,我有一个视图控制器,我试图通过单击按钮在主视图上动态创建一个CanvasView自定义类 我可以创建CanvasView实例并在视图上查看它,但是对于运行时生成的实例,CanvasView类中的函数根本不会触发 在视图上创建了一个名为canvasView1的设计时视图,该视图一切正常 我是IOS新手,所以我可能犯了一个愚蠢的错误。 有什么想法吗? 提前感谢你的帮助 class ViewController: UIViewController { @IBOutlet weak var

我有一个视图控制器,我试图通过单击按钮在主视图上动态创建一个CanvasView自定义类

我可以创建CanvasView实例并在视图上查看它,但是对于运行时生成的实例,CanvasView类中的函数根本不会触发

在视图上创建了一个名为canvasView1的设计时视图,该视图一切正常

我是IOS新手,所以我可能犯了一个愚蠢的错误。 有什么想法吗? 提前感谢你的帮助

class ViewController: UIViewController {


    @IBOutlet weak var click: UIButton!


    @IBAction func clicked(_ sender: Any) {
    enter code here
        var  imageView1 : CanvasView!
        imageView1 = CanvasView(frame:CGRect(x: 330, y: 330, width: 100, height: 200));
        imageView1.backgroundColor = UIColor.blue
        self.view.addSubview(imageView1)
    }
    @IBOutlet weak var canvasView1: CanvasView!
这是我的CanvasView类,它来自UIImageView

import UIKit

let π = Double.pi

        class CanvasView: UIImageView {


            // Parameters
          private let defaultLineWidth:CGFloat = 3

          private var drawColor: UIColor = UIColor.red


            override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
                guard let touch = touches.first else { return }

                UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
                let context = UIGraphicsGetCurrentContext()

                // Draw previous image into context
                image?.draw(in: bounds)

                drawStroke(context: context, touch: touch)

                // Update image
                image = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
            }
     private func drawStroke(context: CGContext?, touch: UITouch) {
        let previousLocation = touch.previousLocation(in: self)
        let location = touch.location(in: self)

        // Calculate line width for drawing stroke
        let lineWidth = lineWidthForDrawing(context: context, touch: touch)

        // Set color
        drawColor.setStroke()

        // Configure line
        context!.setLineWidth(lineWidth)
        context!.setLineCap(.round)


        // Set up the points
        context?.move(to: CGPoint(x:previousLocation.x, y:previousLocation.y))
        context?.addLine(to: CGPoint(x:location.x, y:location.y))
        // Draw the stroke
        context!.strokePath()

      }

      private func lineWidthForDrawing(context: CGContext?, touch: UITouch) -> CGFloat {

        let lineWidth = defaultLineWidth

        return lineWidth
      }

        func clearCanvas(animated: Bool) {
        if animated {
            UIView.animate(withDuration: 0.5, animations: {
            self.alpha = 0
            }, completion: { finished in
              self.alpha = 1
              self.image = nil
          })
        } else {
          image = nil
        }
      }
    }
导入UIKit
设π=Double.pi
类CanvasView:UIImageView{
//参数
私有let defaultLineWidth:CGFloat=3
私有变量drawColor:UIColor=UIColor.red
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
guard let touch=touch.first else{return}
UIGraphicsBeginImageContextWithOptions(bounds.size,false,0.0)
let context=UIGraphicsGetCurrentContext()
//将前一个图像绘制到上下文中
图像?绘制(在:边界内)
drawStroke(上下文:上下文,触摸:触摸)
//更新图像
image=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()
}
private func drawStroke(上下文:CGContext?,触摸:UITouch){
让previousLocation=touch.previousLocation(in:self)
让位置=触摸。位置(in:self)
//计算绘图笔划的线宽
让线宽=线宽用于绘图(上下文:上下文,触摸:触摸)
//定色
drawColor.setStroke()
//配置行
上下文!.setLineWidth(线宽)
上下文!.setLineCap(.round)
//设置点
上下文?.move(到:CGPoint(x:previousLocation.x,y:previousLocation.y))
context?.addLine(to:CGPoint(x:location.x,y:location.y))
//划一
上下文!.strokePath()
}
专用func线宽绘图(上下文:CGContext?,触摸:UITouch)->CGFloat{
设lineWidth=defaultLineWidth
返回线宽
}
func clearCanvas(动画:Bool){
如果动画{
UIView.animate(持续时间:0.5,动画:{
self.alpha=0
},完成:{在中完成
self.alpha=1
self.image=nil
})
}否则{
图像=零
}
}
}
如注释所示:

  imageView1.isUserInteractionEnabled = true
解决了这个问题。
谢谢。

如果您将isUserInteractionEnabled=true设置为
imageView1.isUserInteractionEnabled=true,它会工作吗。您应该检查在Interface Builder中创建的属性(我假设这就是设计时的含义),这与您的问题无关,但是为什么
drawstrope
方法的
context
参数声明为可选?那你为什么要一次又一次地强制打开它?这不是我的密码。我从raywenderlich网站上找到的。