Skip to content

Commit

Permalink
Reduce the number of calls to setFrame (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
takumatt committed Mar 24, 2023
1 parent c44cdff commit b2d3984
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 9 deletions.
75 changes: 75 additions & 0 deletions ScrollEdgeControl-Demo/UIControl+Closure.swift
@@ -0,0 +1,75 @@
import UIKit

@MainActor
private final class Proxy {

static var key: Void?
private weak var base: UIControl?

init(_ base: UIControl) {
self.base = base
}

var onTouchUpInside: (@MainActor () -> Void)? {
didSet {
base?.addTarget(
self,
action: #selector(touchUpInside(sender:)),
for: .touchUpInside
)
}
}

var onValueChanged: (@MainActor () -> Void)? {
didSet {
base?.addTarget(
self,
action: #selector(valueChanged(sender:)),
for: .valueChanged
)
}
}

@objc private dynamic func touchUpInside(sender: AnyObject) {
onTouchUpInside?()
}

@objc private dynamic func valueChanged(sender: AnyObject) {
onValueChanged?()
}
}

extension UIControl {

/// [Local extension]
public func onTap(_ closure: @MainActor @escaping () -> Swift.Void) {
proxy.onTouchUpInside = closure
}

public func onValueChanged(_ closure: @MainActor @escaping () -> Swift.Void) {
proxy.onValueChanged = closure
}

private var proxy: Proxy {
get {
if let handler = objc_getAssociatedObject(self, &Proxy.key) as? Proxy {
return handler
} else {
self.proxy = Proxy(self)
return self.proxy
}
}
set {
objc_setAssociatedObject(self, &Proxy.key, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}

extension UIButton {
static func make(title: String, _ onTap: @escaping () -> Void) -> UIButton {
let button = UIButton(frame: .zero)
button.setTitle(title, for: .normal)
button.onTap(onTap)
return button
}
}
8 changes: 4 additions & 4 deletions ScrollEdgeControl.xcodeproj/project.pbxproj
Expand Up @@ -26,7 +26,6 @@
4B98480927F33C5200ED3FA9 /* CompositionKit in Frameworks */ = {isa = PBXBuildFile; productRef = 4B98480827F33C5200ED3FA9 /* CompositionKit */; };
4B98480B27F33CB000ED3FA9 /* DemoVerticalStickyHeaderViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B98480A27F33CB000ED3FA9 /* DemoVerticalStickyHeaderViewController.swift */; };
4B9A4D0D29BDAFC70043C4B5 /* UIView+Frame.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B9A4D0C29BDAFC70043C4B5 /* UIView+Frame.swift */; };
4BB7DA652807F141004A5992 /* UIControl+Closure.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BB7DA642807F141004A5992 /* UIControl+Closure.swift */; };
4BC42830275157320047A850 /* ScrollEdgeControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BC4282F275157320047A850 /* ScrollEdgeControl.swift */; };
4BC42833275157C00047A850 /* Advance in Frameworks */ = {isa = PBXBuildFile; productRef = 4BC42832275157C00047A850 /* Advance */; };
4BC4283B275158240047A850 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BC4283A275158240047A850 /* AppDelegate.swift */; };
Expand All @@ -35,6 +34,7 @@
4BC42850275158F80047A850 /* RxCocoa in Frameworks */ = {isa = PBXBuildFile; productRef = 4BC4284F275158F80047A850 /* RxCocoa */; };
4BC42852275158F80047A850 /* RxRelay in Frameworks */ = {isa = PBXBuildFile; productRef = 4BC42851275158F80047A850 /* RxRelay */; };
4BC42854275158F80047A850 /* RxSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 4BC42853275158F80047A850 /* RxSwift */; };
C400406E29C1A0F1004A834D /* UIControl+Closure.swift in Sources */ = {isa = PBXBuildFile; fileRef = C400406D29C1A0F1004A834D /* UIControl+Closure.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -73,14 +73,14 @@
4B98480527F31E8300ED3FA9 /* ScrollStickyVerticalHeaderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScrollStickyVerticalHeaderView.swift; sourceTree = "<group>"; };
4B98480A27F33CB000ED3FA9 /* DemoVerticalStickyHeaderViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoVerticalStickyHeaderViewController.swift; sourceTree = "<group>"; };
4B9A4D0C29BDAFC70043C4B5 /* UIView+Frame.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+Frame.swift"; sourceTree = "<group>"; };
4BB7DA642807F141004A5992 /* UIControl+Closure.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "UIControl+Closure.swift"; path = "../../../muukii/FluidInterfaceKit/Sources/FluidInterfaceKit-Demo/UIControl+Closure.swift"; sourceTree = "<group>"; };
4BC42825275157080047A850 /* ScrollEdgeControl.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ScrollEdgeControl.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4BC4282F275157320047A850 /* ScrollEdgeControl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScrollEdgeControl.swift; sourceTree = "<group>"; };
4BC42838275158240047A850 /* ScrollEdgeControl-Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "ScrollEdgeControl-Demo.app"; sourceTree = BUILT_PRODUCTS_DIR; };
4BC4283A275158240047A850 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
4BC42843275158250047A850 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
4BC42846275158250047A850 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
4BC42848275158250047A850 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
C400406D29C1A0F1004A834D /* UIControl+Closure.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIControl+Closure.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -173,8 +173,8 @@
4B39D0502752745C00D013F4 /* DemoHorizontalViewController.swift */,
4B39D0522752746600D013F4 /* DemoVerticalViewController.swift */,
4B98480A27F33CB000ED3FA9 /* DemoVerticalStickyHeaderViewController.swift */,
4BB7DA642807F141004A5992 /* UIControl+Closure.swift */,
4BC4283A275158240047A850 /* AppDelegate.swift */,
C400406D29C1A0F1004A834D /* UIControl+Closure.swift */,
4B5E52D427515C830075AE52 /* Book.swift */,
4B5E52D627515C900075AE52 /* BookContainerViewController.swift */,
4BC42843275158250047A850 /* Assets.xcassets */,
Expand Down Expand Up @@ -336,10 +336,10 @@
4B5E52D527515C830075AE52 /* Book.swift in Sources */,
4B39D0532752746600D013F4 /* DemoVerticalViewController.swift in Sources */,
4B39D05727528ACD00D013F4 /* Components.swift in Sources */,
C400406E29C1A0F1004A834D /* UIControl+Closure.swift in Sources */,
4B39D0512752745C00D013F4 /* DemoHorizontalViewController.swift in Sources */,
4B39D05527528A1900D013F4 /* DebuggingRefreshIndicatorView.swift in Sources */,
4B98480B27F33CB000ED3FA9 /* DemoVerticalStickyHeaderViewController.swift in Sources */,
4BB7DA652807F141004A5992 /* UIControl+Closure.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
18 changes: 13 additions & 5 deletions ScrollEdgeControl/Core/ScrollEdgeControl.swift
Expand Up @@ -577,7 +577,15 @@ public final class ScrollEdgeControl: UIControl {

}

func setFrame(_ frame: CGRect) {
func setFrame(_ frame: CGRect, scrollView: UIScrollView) {

guard scrollView.bounds.contains(frame) else {
self.layer.isHidden = true
return
}

self.layer.isHidden = false

setSize(frame.size)
setPosition(point: frame.origin)
}
Expand Down Expand Up @@ -628,7 +636,7 @@ public final class ScrollEdgeControl: UIControl {
size: sizeForVertical
)

setFrame(frame)
setFrame(frame, scrollView: scrollView)

case .bottom:

Expand All @@ -641,7 +649,7 @@ public final class ScrollEdgeControl: UIControl {
size: sizeForVertical
)

setFrame(frame)
setFrame(frame, scrollView: scrollView)

case .left:

Expand All @@ -653,7 +661,7 @@ public final class ScrollEdgeControl: UIControl {
size: sizeForHorizontal
)

setFrame(frame)
setFrame(frame, scrollView: scrollView)

case .right:

Expand All @@ -666,7 +674,7 @@ public final class ScrollEdgeControl: UIControl {
size: sizeForHorizontal
)

setFrame(frame)
setFrame(frame, scrollView: scrollView)

}

Expand Down

0 comments on commit b2d3984

Please sign in to comment.