简体   繁体   English

Swift-初始化UIView的子类

[英]Swift - Initialize a subclass of UIView

I have an already setup view and want to wrap it in a subclass of UIView . 我已经有一个安装视图,并希望将其包装在UIView的子类中。

class ElementView: UIView {
    var selected = false
}

The problem is that I cannot initialize ElementView with the already existing view . 问题是我无法使用已经存在的view初始化ElementView Swift doesn't allow assigning to self too. Swift也不允许分配给self。

init(view: UIView) {
    //here I would have to call super.init(coder: NSCoder?) or super.init(frame: CGRect)
    //none of which actually properly initializes the object
}

Any ideas how to implement this? 任何想法如何实现这一点? Thanks :) 谢谢 :)


Clarification: 澄清:

I will give you the larger context hoping it'd be more clear: 我将为您提供更大的背景信息,希望它会更加清晰:

I am implementing a UIScrollView subclass. 我正在实现UIScrollView子类。 The scroll view contains an array of UIView objects which are added externally (from the user of the class). 滚动视图包含一个UIView对象数组,这些对象从外部(从类的用户)添加到外部。 In my custom UIScrollView class I want to implement a tap gesture recognizer for each object. 在我的自定义UIScrollView类中,我想为每个对象实现一个轻击手势识别器。 That's already done: 已经完成了:

let singleTap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
singleTap.cancelsTouchesInView = false
addGestureRecognizer(singleTap)

and the handler: 和处理程序:

func handleTap(recognizer: UITapGestureRecognizer) {
    //some code to handle the tap
}

The problem arises when I want to handle the tap, I want to store the previous state of the view (was it tapped before or not) in order to toggle that state when the tap happens. 当我要处理轻击,我想存储视图的先前状态(是否被轻击)以在轻击发生时切换该状态时,就会出现问题。 I want to do different stuff to the UIView depending on its state. 我想根据其状态对UIView做不同的事情。

recognizer.view returns the view to which the recognizer is attached to, which is what I need. ognitor.view返回识别器附加到的视图,这是我所需要的。 But, this way I have no possibility of implementing a state for the UIView. 但是,这样我就不可能为UIView实现状态。

That's why I wanted to implement a custom wrapper for UIView which should contain the state information (which is also a problem). 这就是为什么我想为UIView实现一个自定义包装,该包装应该包含状态信息(这也是一个问题)。 That's how I came up to asking this question... 这就是我提出这个问题的方式...

In order to create a custom init for the UIView subclass you have to call the required init for the UIView superclass. 为了为UIView子类创建自定义init,必须为UIView超类调用所需的init。 Also while creating the custom init you have to send the Frame of the view to the superclass. 同样,在创建自定义init时,您必须将视图的Frame发送给超类。 Upon fulfilling these requirements you are free to pass on any arguments to the newly created init including the tap recognizer info. 满足这些要求后,您可以将任何参数(包括分接头识别器信息)传递给新创建的init

Remember, if you are creating any variables - they have to be not nil upon the creation of the instance, thus in variable declaration you have to create some initial argument (for example - 0 for Int , etc.) for the initializer to work. 请记住,如果要创建任何变量-创建实例时它们必须不nil ,因此在变量声明中,必须创建一些初始参数(例如-0表示Int等),以使初始化程序起作用。 Here is the example code: 这是示例代码:

var variableOne: Int = 0
var variableTwo: Int = 0

init(variableOne: Int, variableTwo: Int) {
    self.variableOne = variableOne
    self.variableTwo = variableTwo
    super.init(frame: CGRectZero)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

It sounds like you are trying to mimic a copy constructor, and it sounds like you are trying to build it in IB. 听起来好像您正在尝试模仿副本构造函数,听起来好像您正在尝试在IB中构建它。 The short answer is what you are doing doesn't make sense. 简短的答案是您所做的没有意义。 It further sounds like that you wanted your code above to own or assume the identity of the argument view (your reference to not being able to assign to self). 进一步听起来,您希望上面的代码拥有或假定参数视图的身份(您对无法分配给self的引用)。 If this assumption is correct, your code would make even less sense - you would just need to assign to a variable. 如果这个假设是正确的,那么您的代码将毫无意义-您只需要分配给变量即可。

Just create the view class, with the code that you have posted, you do not need to implement a constructor, since you have provided a default value for your selected variable. 只需使用发布的代码创建视图类,就无需实现构造函数,因为已为所选变量提供了默认值。 Your will with then be populated from IB via the coder based constructor. 然后通过基于编码器的构造函数从IB填充您的遗嘱。

If you are trying to clone or copy a given view, then refer to "Can UIView be copied" 如果要克隆或复制给定的视图,请参阅“可以复制UIView”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM