title: Swift - Initialize a subclass of UIView
body: <p>I have an already setup view and want to wrap it in a subclass of <code>UIView</code>.</p>
  
  <pre><code>class ElementView: UIView {
      var selected = false
  }
  </code></pre>
  
  <p>The problem is that I cannot initialize <code>ElementView</code> with the already existing <code>view</code>.
  Swift doesn't allow assigning to self too.</p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>Any ideas how to implement this?
  Thanks :)</p>
  
  <hr>
  
  <p>Clarification:</p>
  
  <p>I will give you the larger context hoping it'd be more clear:</p>
  
  <p>I am implementing a UIScrollView subclass. The scroll view contains an array of UIView objects which are added externally (from the user of the class).
  In my custom UIScrollView class I want to implement a tap gesture recognizer for each object. That's already done:</p>
  
  <pre><code>let singleTap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
  singleTap.cancelsTouchesInView = false
  addGestureRecognizer(singleTap)
  </code></pre>
  
  <p>and the handler:</p>
  
  <pre><code>func handleTap(recognizer: UITapGestureRecognizer) {
      //some code to handle the tap
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>recognizer.view returns the view to which the recognizer is attached to, which is what I need. But, this way I have no possibility of implementing a state for the UIView.</p>
  
  <p>That's why I wanted to implement a custom wrapper for UIView which should contain the state information (which is also a problem). That's how I came up to asking this question...</p>