Using UIView as a Button

I am trying to use the UIView that I created on the Storyboard as a button. I suggested that it would be possible to use UIButton by setting the type to custom. However, I was unable to add subviews to the custom UIButton in the Storyboard.

As such, I just spent the last hour inventing the wheel, making my own custom gesture redesigners to redefine the functionality of the buttons.

Of course, this is not the best way to do this, so my question is for more experienced iOS developers than me - is this the best way to create a custom button?

To be clear, he must:

  • Use the UIView that I created as the hittable scope.
  • You can show a different state depending on whether it is currently highlighted or not (i.e., touch).
  • Follow some steps when actually pressed.

Thank you for your help.

+4
source share
4 answers

You can use UIButton, set the type to custom, and then programmatically add your views ...

+1
source

Subclass of UIControl. You can add subqueries to it and respond to actions

0
source

Why are you implementing your own GestureRecognizer? I recommend using UIView so you can add subviews to the interface builder and add UITapGestureRecognizer. You can even do this graphically since you do not need iOS4 support.

0
source

Change your UIView to UIControl in the storyboard. Then use the [controlViewName addTarget:self action:@selector(*click handler method*) forControlEvents:UIControlEventTouchDown]; . The click handler method is a placeholder for the name of your handler method. Use this method, with the exception of changing UIControlEventTouchDown for UIControlEventTouchInside and UIControlEventTouchDragExit to invoke the method when the user finishes his click and pulls his finger from the view, respectively. I used this for something that I'm working on right now, and it works great.

In Touch Down you want to: highlight all the subitems
In a touch inside you want to: display all subzones and execute segue or do what the button should do

In Touch Drag Exit mode you need: unhighlight all subviews

See LiCheng's second answer in this similar to SO post.

0
source

Source: https://habr.com/ru/post/1400307/


All Articles