简体   繁体   中英

Scaling a button to fit the background image

Im having an issue here. I want the button to fit the background image's dimensions perfectly.

For example:

图片范例

As you can see, the box around the "Request Plan" diamond overlaps or goes over the "View Plan" diamond, and in run-time when I click on the "View Plan" diamond it actually clicks on the "Request Plan" diamond because of it overlapping. Is there a way I can fix this?

In the storyboard, whatever you want to be equal lengthed just drag to the other object and select "Equal Widths" and "Equal Lengths". Regarding the overlapping, maybe either change the order of the button or view, or recreate it.

You can create CustomButton class extend UIButton then override pointInside

override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
    *we need check at this point inside or outside*
    return self.alphaAtPoint(point) >= 127
}

And We can check it by check alpha value

func alphaAtPoint(point: CGPoint) -> CGFloat {
   var pixel: [UInt8] = [0, 0, 0, 0]
   let colorSpace = CGColorSpaceCreateDeviceRGB();
   let alphaInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)
   let context = CGBitmapContextCreate(&pixel, 1, 1, 8, 4, colorSpace, alphaInfo)

   CGContextTranslateCTM(context, -point.x, -point.y);

   self.layer.renderInContext(context)

   return CGFloat(pixel[3])
}

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