Swift 放大按钮点击热区的简单实现

591 阅读1分钟

非常简单直接上代码

import UIKit

class BiggerClickAreaButton: UIButton {
    
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        
        var bounds = self.bounds
    
        //若原热区小于40x40,则放大热区,否则保持原大小不变
        let widthDelta: CGFloat = 40
        let heightDelta: CGFloat = 40
        bounds = bounds.insetBy(dx: -0.5 * widthDelta, dy: -0.5 * heightDelta)
        return bounds.contains(point)
    }

}

想要放大按钮的点击区域的话直接让button继承自BiggerClickAreaButton即可