How to adapt or custom the shape of a IBOutlet button as the inner button's image in Swift iOS8

二次信任 提交于 2019-12-12 01:24:41

问题


I have an IBOutlet button dragged from Interface Builder (it's easier to play with it in auto layout )

I rotated it by 45° in code as learned here

var transform = CGAffineTransformMakeTranslation(0.5, 0.5)
var angle = CGFloat(M_PI / 3.9) // degress rotation 3.9
transform = CGAffineTransformRotate(transform, angle)
button.transform = transform
button.layer.rasterizationScale = UIScreen.mainScreen().scale
button.layer.shouldRasterize = true
button.setNeedsDisplay()

now, I have diamond, triangle, rectangle shaped images to put inside as many IBOutlets. this IBOutlets are required to be side by side. But there are margins of the button that I don't know how to cut off.

so, purple is good, yellow is bad. purple area will be "the button" image but I need to cut off the yellow margins area (now is set clear color, but is clickable anyway, and I don't want it to be)

I need to make a UI on iPhone made by many shapes, each of them will be side by side the other, and I don't want the unwanted area of the button to "cover" half the button next to it. any help?

thanks in advance


回答1:


Button tapping is based on a rectangular, non-rotated area. You can't make a button's tappable area an irregular shape, and can't overlap the bounds rectangles.

You COULD rotate the coordinate system of the superview that contains all your buttons, so that they all were tilted 45 degrees. That way you could have rows and columns of square buttons that LOOK like diamonds, and did respond to taps in each region.

You could also create a custom view class that manages a grid of irregularly shaped objects that ACT like buttons, and detect taps based on the opaque parts of the images they contain, but that would be quite a bit of work, and involve some fairly advanced techniques that are beyond the scope of a SO post to explain.



来源:https://stackoverflow.com/questions/30642131/how-to-adapt-or-custom-the-shape-of-a-iboutlet-button-as-the-inner-buttons-imag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!