Recon
Recon

Reputation: 1

SWIFT Removing Subviews

I'm trying to have the ability to remove the subview the user chooses with three taps. The problem I'm having is I can only get it to remove in the order the subviews were created. I have played around with 'viewWithTag' but can't figure out how to get it to do what I want.

Can I achieve what I want with the 'tapGesture' removing the subview from the location I tapped?

I'm a noob (as the kids say), so any help is much appreciated! Thanks!

@IBAction func unwindToParent(segue:UIStoryboardSegue){
    var source = segue.sourceViewController  as PropViewController
    var propImage = UIImage(named: name as String!)
        clipView = UIImageView(image: propImage!)

        clipView.frame = CGRectMake(0, 0, 200.0, 200.0)
        clipView.center = CGPoint (x: view.bounds.size.width/2, y: view.bounds.size.height/2)
        clipView.contentMode = UIViewContentMode.ScaleAspectFit
        clipView.userInteractionEnabled = true
        clipView.multipleTouchEnabled = true
        clipView.layer.cornerRadius = 10.0



        setTag = tagCounter
        tagCounter++
        clipView.tag = setTag


        addPinchGestureRecognizer(clipView)
        addPanGestureRecognizer(clipView)
        addRotationGestureRecognizer(clipView)
        addTapGestureRecognizer(clipView)

        view.addSubview(clipView)
        view.bringSubviewToFront(clipView)

    let recognizer = UITapGestureRecognizer(target: self, action:Selector("Trash:"))
        recognizer.numberOfTapsRequired = 3
        recognizer.delegate = self
        clipView.addGestureRecognizer(recognizer)

}


    func Trash(gesture: UITapGestureRecognizer){

         clipView.viewWithTag(setTag)?.removeFromSuperview()

        }

Upvotes: 0

Views: 638

Answers (2)

Aggressor
Aggressor

Reputation: 13551

Theres probably a half dozen ways I can see this being done.

The 'simplest' is probably getting the location of the tap

tap.locationInSubview

And then checking if any of the views contain that point

uiViewObject.containtsPoint()

If it contains the point remove it

Or you could add gestures to each view and, as suggested by jrturton, uses the taps view source as the view to remove.

Upvotes: 0

jrturton
jrturton

Reputation: 119242

The gesture recogniser has a view property - that will be the tapped view.

Never use tags for anything.

Upvotes: 2

Related Questions