简体   繁体   中英

How to create iOS UiView that combines multiple buttons next to each other and enables swiping to selecting all of them

Right now i have a few buttons next to each other like this:

在此处输入图片说明

User has to press all of them to select the whole row (i have a few rows). It would be natural to enable them to swipe over them to select them all. I also need to get information about how many buttons were selected.

Could you please help me how to achieve this? Do i have to create my own view or implement some gesture listeners?

Depending on what you exactly trying to achieve there are 2 easy ways :

  1. Simplest : add a UISwipeGestureRecognizer to your view (or to a view with only the 4 buttons on it) and implement delegate interface within your view controller. When a swipe (in the good direction) is done, you receive an event and you perform your "unlock".

UISwipeGestureRecognizer extends UIGestureRecognizer (doc here ) :

Clients of gesture recognizers can also ask for the location of a gesture by calling locationInView: or locationOfTouch:inView:.

So with that + implementing UIGestureRecognizerDelegate (doc here ) you should be able to check exactly what you want.

  1. If you want to manually track the user finger you can just extend UIView and implement :
    • touchBegan
    • touchMoved
    • touchEnded

You can here have the full control ( CGRectContainsPoint() will be useful).

Edit : Something important you have to keep in mind to make your choice is speed. If the user can select the row even with a slow and non-linear gesture (put the finger on the first button then > slowly go to second > go to third > back to second > go to last for instance), you should extends UIView and on the touch began make the first image highlight and then follow the finger until the touchEnd on the last button. It should be more user friendly (depending on what you are trying to achieve of course).

签出此开源项目Swipe-to-Select Gridview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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