From cee5ce08f9116facdd762b1be34f0d8c7809dc38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cstanhu=E2=80=9D?= <3421902@qq.com> Date: Mon, 19 Nov 2018 17:07:27 +0800 Subject: [PATCH] fix a uiscroll bug --- ViewChaos/ViewChaos.swift | 20 ++- ViewChaosDemo/.DS_Store | Bin 6148 -> 6148 bytes .../ViewChaosDemo.xcodeproj/project.pbxproj | 4 + ViewChaosDemo/ViewChaosDemo/AppDelegate.swift | 2 +- .../ViewChaosDemo/TableViewController.swift | 142 ++++++++++++++++++ .../ViewChaosDemo/ViewController.swift | 10 ++ 6 files changed, 171 insertions(+), 7 deletions(-) create mode 100644 ViewChaosDemo/ViewChaosDemo/TableViewController.swift diff --git a/ViewChaos/ViewChaos.swift b/ViewChaos/ViewChaos.swift index a4b5e62..7c1a117 100644 --- a/ViewChaos/ViewChaos.swift +++ b/ViewChaos/ViewChaos.swift @@ -549,7 +549,7 @@ class ViewChaos: UIView { viewBound.removeFromSuperview() let touch = touches.first - let point = touch?.location(in: self) + let point = touch?.location(in: self) //这个坐标是这个圆本身的坐标 left = Float(point!.x) top = Float(point!.y) let topPoint = touch?.location(in: self.window) @@ -669,13 +669,21 @@ class ViewChaos: UIView { var pt = point if view is UIScrollView{ pt.x += (view as! UIScrollView).contentOffset.x +// if view.frame.origin.y < 64{ +// pt.y += (view as! UIScrollView).contentOffset.y + 64 - view.frame.origin.y +// } +// else{ +// pt.y += (view as! UIScrollView).contentOffset.y +// } pt.y += (view as! UIScrollView).contentOffset.y + } - if view.point(inside: point, with: nil) && !view.isHidden && view.alpha > 0.01 && view != viewBound && !view.isDescendant(of: self){//这里的判断很重要. + + if view.point(inside: pt, with: nil) && !view.isHidden && view.alpha > 0.01 && view != viewBound && !view.isDescendant(of: self){//这里的判断很重要. if !(view is AbstractView) {//issue12 在这里过滤掉AbstractView就行,就可以是获取最上面的AbstractView了 arrViewHit.append(view) for subView in view.subviews{ - let subPoint = CGPoint(x: point.x - subView.frame.origin.x , y: point.y - subView.frame.origin.y) + let subPoint = CGPoint(x: pt.x - subView.frame.origin.x , y: pt.y - subView.frame.origin.y) hitTest(subView, point: subPoint) } } @@ -686,9 +694,9 @@ class ViewChaos: UIView { arrViewHit .removeAll() hitTest(view, point: point) let viewTop = arrViewHit.last -// for v in arrViewHit{ -// Chaos.Log("\(type(of: v))") -// } + for v in arrViewHit{ + Chaos.Log("\(type(of: v))") + } arrViewHit.removeAll() return viewTop } diff --git a/ViewChaosDemo/.DS_Store b/ViewChaosDemo/.DS_Store index fb65c8f25691655b2f5d31a86d6c46092f1fe0c9..ad09deac55b1479d72dc2409b4f0c73297ed26ad 100644 GIT binary patch delta 46 zcmZoMXfc@J&&V<{U^gSnWFAIkM&8K=j8c;o7=4(8c1_M>bYM)_ypd6lZ8JN^Uw!}} Cmkf0P delta 51 zcmZoMXfc@J&&WJ6U^gT4WFAK4$&rkT^ Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { window = UIWindow(frame: UIScreen.main.bounds) let mainViewController = ViewController() let rootNavigationController = UINavigationController(rootViewController: mainViewController) diff --git a/ViewChaosDemo/ViewChaosDemo/TableViewController.swift b/ViewChaosDemo/ViewChaosDemo/TableViewController.swift new file mode 100644 index 0000000..478ec7a --- /dev/null +++ b/ViewChaosDemo/ViewChaosDemo/TableViewController.swift @@ -0,0 +1,142 @@ +// +// TableViewController.swift +// ViewChaosDemo +// +// Created by Stan Hu on 2018/11/19. +// Copyright © 2018 Qfq. All rights reserved. +// + +import UIKit + +class TableViewController: UIViewController { + + let tb = UITableView() + var arrSource = [String]() + let word = "摄影小哥似乎看穿了我的心思,却不知道为什么用着佳能1D的大佬,极力向我推荐索尼,莫非是最近看到了姨夫的微笑?现在看来价格差距不算大的横向型号有大法A6000L,佳能M6和富士XA5。于是我在贴吧论坛开始转悠,基本的论调是大法性能优秀,佳能镜头便宜,富士直出色彩美丽。看完一圈以后,我又看了看小哥拍的照片,告诉自己,专业的人考虑专业的事情,我这样的小白不需要想太多,闭着眼睛买就对了(双11来了时间不多了!),于是赶上双11狗东不送赠品降价(我也用不着赠品,SD卡屯了好几张,相机包淘宝50买个mini的就好),就像快门声一样咔嚓一下,手就没了,不,是草就没了" + override func viewDidLoad() { + super.viewDidLoad() + view.tag = 111 + tb.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height) + tb.dataSource = self + tb.delegate = self + tb.register(UITableViewCell.self, forCellReuseIdentifier: "cell") + for _ in 0..<20{ + var start = Int(arc4random()) % word.count + var end = Int(arc4random()) % word.count + if start > end{ + (start,end) = (end,start) + } + else if start == end{ + start = 0 + } + arrSource.append(word.substring(from: start, to: end)) + } + tb.tableFooterView = UIView() + tb.separatorStyle = .singleLine + view.addSubview(tb) + // Do any additional setup after loading the view. + } + + + +} +extension TableViewController:UITableViewDataSource,UITableViewDelegate{ + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return arrSource.count + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) + cell.textLabel?.text = arrSource[indexPath.row] + cell.textLabel?.numberOfLines = 0 + return cell + } + + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + let cell = tableView.cellForRow(at: indexPath) + print(cell!.textLabel!.frame) + if let fm = view.window?.convert(cell!.textLabel!.frame, from: cell!){ + print(fm) + } + + } + + +} + + +extension String{ + func substring(from: Int?, to: Int?) -> String { + if let start = from { + guard start < self.count else { + return "" + } + } + + if let end = to { + guard end >= 0 else { + return "" + } + } + + if let start = from, let end = to { + guard end - start >= 0 else { + return "" + } + } + + let startIndex: String.Index + if let start = from, start >= 0 { + startIndex = self.index(self.startIndex, offsetBy: start) + } else { + startIndex = self.startIndex + } + + let endIndex: String.Index + if let end = to, end >= 0, end < self.count { + endIndex = self.index(self.startIndex, offsetBy: end + 1) + } else { + endIndex = self.endIndex + } + + return String(self[startIndex ..< endIndex]) + } + + func substring(from: Int) -> String { + return self.substring(from: from, to: nil) + } + + func substring(to: Int) -> String { + return self.substring(from: nil, to: to) + } + + func substring(from: Int?, length: Int) -> String { + guard length > 0 else { + return "" + } + + let end: Int + if let start = from, start > 0 { + end = start + length - 1 + } else { + end = length - 1 + } + + return self.substring(from: from, to: end) + } + + func substring(length: Int, to: Int?) -> String { + guard let end = to, end > 0, length > 0 else { + return "" + } + + let start: Int + if let end = to, end - length > 0 { + start = end - length + 1 + } else { + start = 0 + } + + return self.substring(from: start, to: to) + } +} diff --git a/ViewChaosDemo/ViewChaosDemo/ViewController.swift b/ViewChaosDemo/ViewChaosDemo/ViewController.swift index 1fd6696..c00485c 100644 --- a/ViewChaosDemo/ViewChaosDemo/ViewController.swift +++ b/ViewChaosDemo/ViewChaosDemo/ViewController.swift @@ -13,6 +13,7 @@ class ViewController: UIViewController { var btnShowMark = UIButton(frame: CGRect(x: 10, y: 20, width: 80, height: 30)) let btnLog = UIButton(frame: CGRect(x: 100, y: 360, width: 100, height: 30)) + let btnTable = UIButton(frame: CGRect(x: 150, y: 100, width: 100, height: 30)) var view1 = UIView() var view2 = UIView(frame: CGRect(x: 50, y: 400, width: 300, height: 200)) @@ -27,6 +28,11 @@ class ViewController: UIViewController { btn?.addTarget(self, action: #selector(ViewController.click(_:)), for: UIControl.Event.touchUpInside) view.addSubview(btn!) + btnTable.backgroundColor = UIColor.purple + btnTable.setTitle("ToTable", for: .normal) + btnTable.addTarget(self, action: #selector(toTable), for: .touchUpInside) + view.addSubview(btnTable) + btnLog.setTitle("添加Log", for: .normal) btnLog.backgroundColor = UIColor(red: 0, green: 1, blue: 0, alpha: 0.5) btnLog.addTarget(self, action: #selector(printLog), for: .touchUpInside) @@ -46,6 +52,10 @@ class ViewController: UIViewController { view.addSubview(view2) } + @objc func toTable() { + navigationController?.pushViewController(TableViewController(), animated: true) + } + @objc func printLog() { VCLog(message: "this is a log") VCLog(message: "this is a logMarkView.showTaggingView(view: view)MarkView.showTaggingView(view: view)MarkView.showTaggingView(view: view)MarkView.showTaggingView(view: view)")