Skip to content

Commit

Permalink
impressionLog 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
박길호 committed Nov 5, 2022
1 parent b6d4491 commit aa381e0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 57 deletions.
38 changes: 22 additions & 16 deletions Example-iOS/Test/ViewDidAppearViiewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ class UITestCell: UICollectionViewCell {
required init?(coder: NSCoder) {
super.init(coder: coder)

self.viewDidAppear = { isShow in
print("\n111 viewDidAppear: \(self.tag) : \(isShow)")
}
// self.viewDidAppear = { isShow in
// print("\n세로 viewDidAppear: \(self.tag) : \(isShow)")
// }
self.isImpressionCheckZone = true
self.impressionLog = {
print("222 impressionLog: \(self.tag)")
print("세로 impressionLog: \(self.tag)")
}
}
}
Expand All @@ -104,17 +104,23 @@ class UITest2Cell: UICollectionViewCell, UICollectionViewDelegate, UICollectionV
}

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if let viewDidAppear = cell.viewDidAppear {
cell.viewDidAppearIsVisible = true
viewDidAppear(true)
}
// cell.viewDidAppearIsVisible = true
// if let viewDidAppear = cell.viewDidAppear {
// viewDidAppear(true)
// }
//
// cell.impressionLogIsVisible = true
// if let impressionLog = cell.impressionLog {
// impressionLog()
// }
}

func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if let viewDidAppear = cell.viewDidAppear {
cell.viewDidAppearIsVisible = false
viewDidAppear(false)
}
// cell.viewDidAppearIsVisible = false
// cell.impressionLogIsVisible = false
// if let viewDidAppear = cell.viewDidAppear {
// viewDidAppear(false)
// }
}

}
Expand All @@ -125,12 +131,12 @@ class UITest2SubCell: UICollectionViewCell {
required init?(coder: NSCoder) {
super.init(coder: coder)

self.viewDidAppear = { isShow in
print("\n333 viewDidAppear: \(self.tag) : \(isShow)")
}
// self.viewDidAppear = { isShow in
// print("\n가로 viewDidAppear: \(self.tag) : \(isShow)")
// }
self.isImpressionCheckZone = true
self.impressionLog = {
print("444 impressionLog: \(self.tag)")
print("가로 impressionLog: \(self.tag)")
}
}
}
Expand Down
81 changes: 40 additions & 41 deletions Sources/EasyConstraints/EasyConstraints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1297,17 +1297,16 @@ private class ViewDidAppearCADisplayLink {
return
}

DispatchQueue.main.async {
for view: UIView in self.views {
autoreleasepool {
self.setViewVisible(view: view, isVisible: view.isVisible)
let windowRect: CGRect = view.superview?.convert(view.frame, to: nil) ?? .zero
if windowRect == .zero {
view.viewDidAppear?(false)
view.viewDidAppear = nil
if let index = self.views.firstIndex(of: view) {
self.views.remove(at: index)
}
for view: UIView in self.views {
autoreleasepool {
self.setViewVisible(view: view, isVisible: view.isVisible)
let windowRect: CGRect = view.superview?.convert(view.frame, to: nil) ?? .zero
if windowRect == .zero {
view.viewDidAppear?(false)
view.viewDidAppear = nil
view.impressionLog = nil
if let index = self.views.firstIndex(of: view) {
self.views.remove(at: index)
}
}
}
Expand All @@ -1318,39 +1317,21 @@ private class ViewDidAppearCADisplayLink {
if view.viewDidAppearIsVisible != isVisible {
view.viewDidAppearIsVisible = isVisible
view.viewDidAppear?(isVisible)
if isVisible, view.isImpressionCheckZone == false {
view.impressionLog?()
}
if isVisible == false {
view.impressionLogIsVisible = false
}
}

if view.impressionLogIsVisible == false {
if view.isImpressionCheckZone {
if impressionLogZone(view: view) {
view.impressionLogIsVisible = true
view.impressionLog?()
}
}
else {
if isVisible, view.isImpressionCheckZone, view.impressionLogIsVisible != true {
if view.isVisiblePercentCheckZone() {
view.impressionLogIsVisible = true
view.impressionLog?()
}
}
}

func impressionLogZone(view: UIView) -> Bool {
let myFrame: CGRect = view.convert(view.bounds, to: view.superview)
let intersection: CGRect = view.superview?.bounds.intersection(myFrame) ?? .zero
// print("intersection: \(intersection)")

let heightPercent: CGFloat = intersection.height / view.bounds.height
let widthPercent: CGFloat = intersection.width / view.bounds.width
let visiblePercent: CGFloat = widthPercent * heightPercent

// print("visiblePercent: \(visiblePercent)")
guard visiblePercent > 0 else { return false }

if visiblePercent < view.impressionCheckZonePercent {
return false
}
return true
}

func start() {
stop()
Expand Down Expand Up @@ -1412,10 +1393,6 @@ extension UIView {
return objc_getAssociatedObject(self, &ViewDidAppearCADisplayLinkKeys.viewDidAppearIsVisible) as? Bool
}
set {

if let value = newValue, value == true {
impressionLogIsVisible = false
}
objc_setAssociatedObject( self, &ViewDidAppearCADisplayLinkKeys.viewDidAppearIsVisible, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
Expand Down Expand Up @@ -1523,6 +1500,28 @@ extension UIView {

return true
}


/// 뷰의 보이는 영역 %로 체크
/// - Parameter percent: 0.01~ 1 사이값
/// - Returns: true / false
public func isVisiblePercentCheckZone() -> Bool {
let myFrame: CGRect = self.convert(self.bounds, to: self.superview)
let intersection: CGRect = self.superview?.bounds.intersection(myFrame) ?? .zero
// print("intersection: \(intersection)")

let heightPercent: CGFloat = intersection.height / self.bounds.height
let widthPercent: CGFloat = intersection.width / self.bounds.width
let visiblePercent: CGFloat = widthPercent * heightPercent

// print("visiblePercent: \(visiblePercent)")
guard visiblePercent > 0 else { return false }

if visiblePercent < self.impressionCheckZonePercent {
return false
}
return true
}
}

// MARK: - UIView Extension SafeArea
Expand Down

0 comments on commit aa381e0

Please sign in to comment.