tokenary/Tokenary iOS/Library/ButtonWithExtendedArea.swift

23 lines
807 B
Swift
Raw Normal View History

2021-12-09 16:25:30 +03:00
// Copyright © 2021 Tokenary. All rights reserved.
import UIKit
class ButtonWithExtendedArea: UIButton {
var minimumHitArea = CGSize(width: 60, height: 60)
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if isHidden || !isUserInteractionEnabled || alpha < 0.01 { return nil }
// increase the hit frame to be at least as big as `minimumHitArea`
let buttonSize = bounds.size
let widthToAdd = max(minimumHitArea.width - buttonSize.width, 0)
let heightToAdd = max(minimumHitArea.height - buttonSize.height, 0)
let largerFrame = bounds.insetBy(dx: -widthToAdd / 2, dy: -heightToAdd / 2)
// perform hit test on larger frame
return (largerFrame.contains(point)) ? self : nil
}
}