2016-06-22 25 views
0

除了兩個彼此接近的按鈕之外,以下代碼完美運行。有什麼辦法可以使用這個擴展來處理我喜歡使用的按鈕?或者,我可以排除不需要使用此擴展名的按鈕嗎?如何在不改變背景圖片的情況下增加UIButton的水龍頭面積

extension UIButton { 
    public override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? { 
     let buttonSize = self.frame.size 
     let widthToAdd = (44-buttonSize.width > 0) ? 44-buttonSize.width : 0 
     let heightToAdd = (44-buttonSize.height > 0) ? 44-buttonSize.height : 0 
     let largerFrame = CGRect(x: 0-(widthToAdd/2), y: 0-(heightToAdd/2), width: buttonSize.width+widthToAdd, height: buttonSize.height+heightToAdd) 
     return (CGRectContainsPoint(largerFrame, point)) ? self : nil 
    } 
} 

回答

0

不要讓事情變得複雜,也不要在代碼中直接選擇44號。你不需要擴展UIButton,因爲你不需要用這種奇怪的方式來實現所有的UIButton行爲。

我只是做一個更大的透明按鈕(清除顏色),然後添加一個小的UIImageView作爲子視圖。這是我猜測的最簡單的方法。

相關問題