Creating an Irregular Uibutton in Swift Where Transparent Parts Are Not Tappable

Creating an irregular UIButton in Swift where transparent parts are not tappable

You can use UIBezierPath or CGPath to define your pie chart sections and use their containsPoint: or CGPathContainsPoint to detect touch

How to get non-rectangular shaped button?

I have a UIImage with a mask. How can I set the shape of a button to this image without the transparent color.

Buttons are controls, and controls are views, and views are inherently rectangular, so a button will always occupy a rectangular space in the view hierarchy. However, the visible part of a view (and therefore a button) can be whatever you want it to be... a view can have a transparent background, and can draw itself however it likes. A view can also choose to pretend that a touch event doesn't hit it, potentially making the view seem to have a non-rectangular shape for the purpose of delivering touches. You can do that by overriding hitTest(_:with:).

Also, realize that you don't always need to use buttons in order to interact with objects on the screen. If you have an image of a house, for example, and you want the user to be able to tap different parts of the house to change its color or texture, you could display the house in a view that knows where different parts of the house are in the image. You could use gesture recognizers or the normal touch handling mechanism to let the user interact with the different regions, and those regions can be any shape you like.

Adding multiple tappable shapes to UIView

In the end this is what I did:

  • Looped through all of the stand shapes I needed to have and created a dictionary with UIBezierpath of their coordinates on the floorplan image and the standNo for the shape.
  • I then added these dictionaries to an array.
  • When the screen was tapped I would note the X and Y of the tap position and looped through the array of dictionaries and it checked if the UIBezierpath contained a point made up of the X and Y tapped coordinates.
  • If a shape was found that had the X and Y coordinates within its bounds I would draw a CAShapelayer using the UIBezierpath and adding a fillColor so it showed up on the map. An alertView was then displayed which showed more information about the exhibitor.

This method seemed WAY more efficient than actually drawing out hundreds of UIButtons or even CAShaplayers and even with 300+ areas on the floorplan to check through the process appears instant.

Round UIButton

You'd probably have to create a custom control for that, but do you really need a round button?

Every platform has its UI conventions, and the iPhone is no exception. Users expect the buttons to be rounded rectangles.

UPDATE in response to comment:

If I'm getting this right, you're not looking for a round button, but rather a clickable (touchable) image.

You can use an UIImageView and its touchesBegan method.

UPDATE 2:

Wait a second. What kind of radio button are we talking about? For some reason I thought you were trying to imitate a real radio. If you're talking about a radio button group, please use a UISegmentedControl or a UIPicker.

Create clickable body diagram with Swift (iOS)

Fixed!

First I added sublayers to the UIImageView like this

var path = UIBezierPath()
path.moveToPoint(CGPointMake(20, 30))
path.addLineToPoint(CGPointMake(40, 30))

// add as many coordinates you need...

path.closePath()

var layer = CAShapeLayer()
layer.path = path.CGPath
layer.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.5).CGColor
layer.hidden = true

bodyImage.layer.addSublayer(layer)

Than I overrided the touchesbegan function in order to show and hide when the shapes are tapped.

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

if let touch = touches.first! as? UITouch {
// get tapped position
let position = touch.locationInView(self.bodyImage)

// loop all sublayers
for layer in self.bodyImage.layer.sublayers! as! [CAShapeLayer] {

// check if tapped position is inside shape-path
if CGPathContainsPoint(layer.path, nil, position, false) {
if (layer.hidden) {
layer.hidden = false
}
else {
layer.hidden = true
}
}
}
}
}

UITapGestureRecognizer not working in UIImageView

UITapGestureRecognizer *oneTouch=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(OneTouchHandeler)];

[oneTouch setNumberOfTouchesRequired:1];

[imageView addGestureRecognizer:oneTouch];

imageView.userInteractionEnabled = YES;


Related Topics



Leave a reply



Submit