Create Custom shaped button using CALayer - ios

what is the best way to achieve the following.
Basically what I want to do is to create an "intractable area" where the user can touch and subsequent actions will take place. For example in the picture the user taps the green area a pop is presented with some options. Similarly there would be different colour "intractable areas or buttons" with in that pizza slice, if you may.
I was thinking of having buttons created through CALayer and try to fit them with in the bounds of the empty state image, but i don't know how to achieve this. Any other ideas are well appreciated.

I would subclass UIButton or UIControl to make your custom button. Then override pointInside:withEvent: to see if the touch event is within the correct region.
See these links for more info and ideas:
Non-Rectangular Buttons on iOS
Abusing UIView (see section on pointInside:withEvent:)
How to create a transparent window with non-rectangular buttons?
iPhone button with non-rectangle shape?
OBShapedButton

Related

Insert blur layer between two existing view layers

I have a UIView with some buttons on it. What I would like to do, is add a full screen blur layer between the UIView and the buttons when the user does a long press on one of them. The visual appearance and location of the buttons shouldn't change.
What is the best way to do this? Also, if possible, I would like to avoid transfering the buttons from one view to another, as this might cause me a lot of trouble (the buttons are draggable).
You can use -[UIView insertSubview:belowSubview:] method to place blur view behind buttons.
I would suggest using Pop animation framework for animations.
As for creating blur view this looks good: https://stackoverflow.com/a/25706250/2754158
You could create a view with the blur effect, and use the method view.insertSubView(blurView, above|belowView: view)

iOS map style config slide up?

iOS 7 has changed how maps are displayed by apps on the iPhone, and especially how the user configures the map.
The map is displayed like this, with an Info button.
When the user taps the Info button, the configuration screen slides up.
How do I recreate a screen overlay like this in my code? While I don't want the user to be dropping pins, I want the user to be able to switch between different map styles or open the address I'm pointing to in Apple's Maps app.
If you are trying to do a simple overlay view, then look into making an UIView slide up through UIView animations. Other than that, you would need an UISegmentedControl and related code. You would also need to blur the view and make it translucent, so look into my other question and answer on how to properly blur an UIView:How to apply blur to a UIView?
I've never looked into it, but I've heard the only easy way to do a translucent background (as in, without writing your own OpenGL code) is to create a UIToolbar instance.
I'm guessing you'd make a toolbar with no items, just treat it like UIView and add your own subviews (UITableView, etc). It is a subclass of UIView.

Changing look when tapping button with layer gradient

I used this question:
How to create a UIButton with a black gradient for iPhone?
... to set up a color gradient as the background of my button and it looks great, but when I tap the button it doesn't visibly change and give the user any feedback that they successfully tapped the button.
How can I set up a second gradient for the highlighted state or something like that?
Well, you are setting a sublayer and not providing any content to the button's views. The sublayers are drawn directly, so the UIButton doesn't think it has any content to "highlight". If you want a different effect on your button when you select it, there are three simple approaches.
One is to make two images with gradients. A pressed and normal state. This is much simpler as you can design them as you want and its a simple one line, one time implementation.
The other option is adding selectors that get called when your button gets UIControlEventTouchDown and whichever other Control Event(s) you want to have it switch back under.
The last simple option is to subclass UIButton and use the UIResponder touch methods to determine when to manually switch between the different background types.

Animate a switch using an image sequence

I would like to know how to solve a problem I'm having. I have an image sequence that basically animates a switch.
I'd like to implement this in iOS. How would you advise me to go for that? Maybe using UIButton? Or rather UISwitch?
For a rather simple solution (tap switches, no swiping possible), I would advise you to use two UIViews stacked.
-1- At the bottom, use a UIImageView which allows you to run image animations.
-2- At the top, use a UIControl which will capture the touché-up-inside events.
Alternatively, you could use a UITapGestureRecognizer as a replacement for -2-. The latter option should only be considered if the resulting control will not be used for iOS < 3.2.
As soon as your tap-recognizer (UIControl or UITapGestureRecognizer) receives the tap-event, you simply play the animation provided through your UIImageView.
For more details on animations using UIImageView see its class reference, specifically on things like animationImages (an array of images).
As UIImageView does not provide you with a reverse animation feature, you will have to assign the images in the reverse order before animating back to the start (that is after a user switched it on and now switches back to off).
For a more complex solution (tap switches, swiping is possible), you may add a UISwipeGesture Recognizer to your custom control and use the status of that UIGestureRecognizer to select the currently visible image of your UIImageView.
iOS 5 allows you to make animated UIImages. You can put the animated UIImage right into the UIButton and animate it on button presses. Animated UIImage

Implementing round buttons

I am building a simple app that displays a grid, similar to a guitar neck, and I wanted to draw oval buttons at each intersection of the grid, and attach behaviors to it, like tapping once for sound. Pretty straight forward stuff...
My question is about the use of class for the buttons.
I am able to draw some good looking buttons with UIBezierPath/bezierPathWithOvalInRect:
and now I want interactivity. I read in this forum from one developer that he used a button underneath an image to capture user interaction. I have tried that but I cannot fade out the button all together, as interaction is removed altogether.
Is it possible to draw rounded buttons with UIButton from interface builder?
What is best practice for something simple like drawing rounded buttons and capturing user interaction?
you can try layer.cornerRadius using the QuartzCore framework.
Or, just use an image as a custom button.

Resources