iOS UIButton之改變有效點擊區域

  • 解決方案 經過重寫- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event; 以改變按鈕的有效點擊區域
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    if (_qi_clickAreaReduceValue > 0) {
        if (_qi_clickAreaReduceValue >= CGRectGetWidth(self.bounds)/2) {
            _qi_clickAreaReduceValue = CGRectGetWidth(self.bounds)/2;
        }
        CGRect bounds = CGRectInset(self.bounds, _qi_clickAreaReduceValue, _qi_clickAreaReduceValue);
        return CGRectContainsPoint(bounds, point);
    }
    
    // 獲取bounds 實際大小
    CGRect bounds = self.bounds;
    // 若熱區小於 44 * 44 則放大熱區 不然保持原大小不變
    CGFloat widthDelta = MAX(44.f - bounds.size.width, 0.f);
    CGFloat heightDelta = MAX(44.f - bounds.size.height, 0.f);
    // 擴大bounds
    bounds = CGRectInset(bounds, -0.5 * widthDelta, -0.5 * heightDelta);
    // 點擊的點在新的bounds 中 就會返回YES
    return CGRectContainsPoint(bounds, point);
}
複製代碼

附:個人博客地址git

相關文章
相關標籤/搜索