|
// |
|
// ShapedButton.swift |
|
// |
|
// |
|
// Created by clmntcrl on 16/09/2014. |
|
// Copyright (c) 2014 clmntcrl. All rights reserved. |
|
// |
|
|
|
import UIKit |
|
|
|
class ShapedButton: UIButton { |
|
|
|
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool { |
|
if !super.pointInside(point, withEvent: event) { |
|
return false |
|
} |
|
|
|
UIGraphicsBeginImageContext(bounds.size) |
|
layer.renderInContext(UIGraphicsGetCurrentContext()) |
|
let image = UIGraphicsGetImageFromCurrentImageContext() |
|
|
|
// ↓ cf. UIImage+ColorAtPixel from https://github.com/ole/OBShapedButton |
|
|
|
let pointX = trunc(point.x) |
|
let pointY = trunc(point.y) |
|
let cgImage = image.CGImage |
|
let width = image.size.width |
|
let height = image.size.height |
|
var pixelData: [CUnsignedChar] = [0, 0, 0, 0] |
|
let bytesPerPixel: UInt = 4 |
|
let bytesPerRow: UInt = bytesPerPixel * 1 |
|
let bitsPerComponent: UInt = 8 |
|
let colorSpace = CGColorSpaceCreateDeviceRGB() |
|
let bitmapInfo = CGBitmapInfo.ByteOrder32Big | CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue) |
|
let context = CGBitmapContextCreate(&pixelData, 1, 1, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo) |
|
CGContextSetBlendMode(context, kCGBlendModeCopy) |
|
|
|
CGContextTranslateCTM(context, -pointX, pointY - height) |
|
CGContextDrawImage(context, CGRectMake(0.0, 0.0, width, height), cgImage) |
|
|
|
// let r = CGFloat(pixelData[0]) / 255.0 |
|
// let g = CGFloat(pixelData[1]) / 255.0 |
|
// let b = CGFloat(pixelData[2]) / 255.0 |
|
let a = CGFloat(pixelData[3]) / 255.0 |
|
|
|
return a != 0.0 |
|
} |
|
|
|
} |
Hi All, Here is it in Swift 3 .
https://gist.github.com/rizvvan/d6def79882bda9f916463b2feaeaf64a