2017-04-21 73 views
0

我在這裏遇到問題。我希望按鈕能夠完美地適應背景圖像的尺寸。縮放按鈕以適應背景圖像

例如:

Image Example

正如你可以看到,圍繞「請求計劃」鑽石箱重疊或越過「查看計劃」的鑽石,並在運行時,當我點擊「查看計劃」鑽石由於它重疊而實際點擊「請求計劃」鑽石。有沒有辦法解決這個問題?

+0

檢查,您可以創建一個正常的按鈕,然後將其旋轉。在這種情況下,你的背景圖像需要被「旋轉」版本 – jokeman

+0

@jokeman我無法找到一個旋轉選項,我該怎麼做 – user3694341

+1

嘗試'self.myButton.transform = self.myButton.transform.rotated(by:CGFloat( M_PI_4))'當初始化你的按鈕 – jokeman

回答

0

您可以創建CustomButton類擴展的UIButton然後覆蓋pointInside

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

,我們可以通過檢查alpha

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]) 
} 
0

在故事板中,無論您希望相等長度,只需拖動到其他對象並選擇「等寬」和「等長」即可。關於重疊,可能要麼改變按鈕或視圖的順序,要麼重新創建它。