Guest User

Untitled

a guest
Jul 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. extension UIView {
  2. func alphaFromPoint(point: CGPoint) -> CGFloat {
  3. var pixel: [UInt8] = [0, 0, 0, 0]
  4. let colorSpace = CGColorSpaceCreateDeviceRGB();
  5. let alphaInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
  6. let context = CGContext(data: &pixel, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: colorSpace, bitmapInfo: alphaInfo.rawValue)
  7.  
  8. context?.translateBy(x: -point.x, y: -point.y)
  9. self.layer.render(in: context!)
  10.  
  11. let floatAlpha = CGFloat(pixel[3])
  12. return floatAlpha
  13. }
  14. }
  15.  
  16. @IBDesignable
  17. class SOXShapedTapButton: UIButton {
  18.  
  19. @IBInspectable var treshold: CGFloat = 1.0 {
  20. didSet {
  21. if treshold > 1.0 {
  22. treshold = 1.0
  23. }
  24. if treshold < 0.0 {
  25. treshold = 0.0
  26. }
  27. }
  28. }
  29.  
  30. override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
  31. return self.alphaFromPoint(point: point) > treshold
  32. }
  33. }
Add Comment
Please, Sign In to add comment