Skip to content

Commit

Permalink
fix a uiscroll bug
Browse files Browse the repository at this point in the history
  • Loading branch information
DuckDeck committed Nov 19, 2018
1 parent d008113 commit cee5ce0
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 7 deletions.
20 changes: 14 additions & 6 deletions ViewChaos/ViewChaos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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
}
Expand Down
Binary file modified ViewChaosDemo/.DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions ViewChaosDemo/ViewChaosDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
AE85E0781CE5BB1500B242A1 /* shake_pop_menu.gif in Resources */ = {isa = PBXBuildFile; fileRef = AE85E0771CE5BB1500B242A1 /* shake_pop_menu.gif */; };
AE9509DA1CA6867500931F14 /* DrawView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE9509D91CA6867500931F14 /* DrawView.swift */; };
AEADFD4E211AC03900E6B58D /* LogView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEADFD4D211AC03900E6B58D /* LogView.swift */; };
AEB502A521A29E5B00981FB9 /* TableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEB502A421A29E5B00981FB9 /* TableViewController.swift */; };
AEC534EE1F29AB4800649DF0 /* shake.gif in Resources */ = {isa = PBXBuildFile; fileRef = AEC534ED1F29AB4800649DF0 /* shake.gif */; };
AEC534F01F29AB4C00649DF0 /* mark.gif in Resources */ = {isa = PBXBuildFile; fileRef = AEC534EF1F29AB4C00649DF0 /* mark.gif */; };
AEF4BDC61F0C71E900D22223 /* MarkInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEF4BDC51F0C71E900D22223 /* MarkInfo.swift */; };
Expand Down Expand Up @@ -88,6 +89,7 @@
AE85E0771CE5BB1500B242A1 /* shake_pop_menu.gif */ = {isa = PBXFileReference; lastKnownFileType = image.gif; path = shake_pop_menu.gif; sourceTree = "<group>"; };
AE9509D91CA6867500931F14 /* DrawView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DrawView.swift; sourceTree = "<group>"; };
AEADFD4D211AC03900E6B58D /* LogView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogView.swift; sourceTree = "<group>"; };
AEB502A421A29E5B00981FB9 /* TableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewController.swift; sourceTree = "<group>"; };
AEC534ED1F29AB4800649DF0 /* shake.gif */ = {isa = PBXFileReference; lastKnownFileType = image.gif; path = shake.gif; sourceTree = "<group>"; };
AEC534EF1F29AB4C00649DF0 /* mark.gif */ = {isa = PBXFileReference; lastKnownFileType = image.gif; path = mark.gif; sourceTree = "<group>"; };
AEF4BDC51F0C71E900D22223 /* MarkInfo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MarkInfo.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -166,6 +168,7 @@
A2E6F0251BEAFF7D0042FC43 /* Info.plist */,
A2E6F0381BEB08680042FC43 /* NextViewController.swift */,
A2E6F03A1BEB08770042FC43 /* Main.storyboard */,
AEB502A421A29E5B00981FB9 /* TableViewController.swift */,
);
path = ViewChaosDemo;
sourceTree = "<group>";
Expand Down Expand Up @@ -293,6 +296,7 @@
A2E6F01A1BEAFF7D0042FC43 /* AppDelegate.swift in Sources */,
A211FED41BF03D0C004A0A29 /* ViewNeat.swift in Sources */,
A211FED21BF02A83004A0A29 /* ViewChaosInfo.swift in Sources */,
AEB502A521A29E5B00981FB9 /* TableViewController.swift in Sources */,
AEF4BDC81F0C780800D22223 /* MarkView.swift in Sources */,
AE08A9C91C9A72F7005767E1 /* ZoomView.swift in Sources */,
AEADFD4E211AC03900E6B58D /* LogView.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion ViewChaosDemo/ViewChaosDemo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


private func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> 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)
Expand Down
142 changes: 142 additions & 0 deletions ViewChaosDemo/ViewChaosDemo/TableViewController.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
10 changes: 10 additions & 0 deletions ViewChaosDemo/ViewChaosDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand All @@ -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)")
Expand Down

0 comments on commit cee5ce0

Please sign in to comment.