0

Alright, so I have these image views that I move around using a good amount of code (this being my reason for not wanting to rework my whole script moving a UIButton that the image view is a subview of - it will screw up everything).

They are little circles here:

bubble1.frame = CGRectMake(0, 0, screenSize.width*(21/330), screenSize.width*(21/330))
        bubble1.center = CGPointMake(lineView.bounds.width * 0.2, lineView.bounds.height * 0.98) //change when make smaller
        bubble1.layer.cornerRadius = bubble1.bounds.width * 0.5
        bubble1.backgroundColor = colorWithHexString(chosenColor)
        bubble1.userInteractionEnabled = false
        //bubble1.alpha = 0.0

        let bubbleBtn1 = UITapGestureRecognizer(target:self, action:#selector(HistoryViewController.bubble1Tap(_:)))
        bubble1.addGestureRecognizer(bubbleBtn1)

As you can see they have tapgesturerecognizers attached to them so when they are touched something is triggered.

This works well except because the circles are so small (they need to be that small) it is hard to trigger the touch - I have looked around for ways to expand the area of the recognizer to no avail.

I then tried to add a larger 70x70 button as a subview to my image view, and i know the larger button was added because I saw it as a larger square on top of my circle, except for some reason it only triggered its action WHERE the underlying bubble1 was, rendering the button useless.

How can I make the larger button trigger its action where the smaller image view is NOT? Ive looked at Qs like How to increase selection area of UIButton? but this is more geared to the issue tat my button does not work outside its superview's area.

How can I solve this?

Community
  • 1
  • 1
blue
  • 7,175
  • 16
  • 81
  • 179
  • Add the `UIImageView` as a subview of your larger `UIButton`. – Daniel Storm Aug 01 '16 at 19:34
  • I don't want to do that as then if I need to move the image view i need to move the whole button instead, which causes problems for me. Also having issues centering the imagveiw on the button – blue Aug 01 '16 at 20:36
  • In what circumstance would you want to move the image, but not the button? – GetSwifty Aug 01 '16 at 20:39

1 Answers1

1

I would recommend going the opposite direction and creating a view that contains both the button and the bubble1 as subViews. That is how the UIView hierarchy is intended to work and any solutions messing with that will be fragile and against the design philosophy of the APIs :)

To deal with any centering issues, you can just set your bubble's center to it's superView's Bounds center which ideally would be handled with layout Constraints

GetSwifty
  • 7,568
  • 1
  • 29
  • 46