Skip to content

Commit

Permalink
fix all the swift 3 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
DuckDeck committed Oct 17, 2016
1 parent 214739d commit a1e9c8e
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 47 deletions.
4 changes: 2 additions & 2 deletions ViewChaos.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "ViewChaos"
s.version = "0.0.5"
s.version = "1.0.0"
s.summary = "A iOS view diagnose tool."

# This description is used to generate tags and improve search results.
Expand Down Expand Up @@ -80,7 +80,7 @@ Pod::Spec.new do |s|
# Supports git, hg, bzr, svn and HTTP.
#

s.source = { :git => "https://github.com/DuckDeck/ViewChaos.git", :tag => "0.0.5" }
s.source = { :git => "https://github.com/DuckDeck/ViewChaos.git", :tag => "1.1.0" }


# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
Expand Down
48 changes: 22 additions & 26 deletions ViewChaos/DrawView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,14 @@ class DrawView: UIView {
if drawMode == .draw{
for touch in touches
{
if let touchPoint:UITouch = touch
{
let point = touchPoint.location(in: self)
var trace = Trace()
trace.arrPoints = Array<CGPoint>()
trace.arrPoints.append(point)
trace.color = UIColor.red
trace.thickness = 1
arrTraces?.append(trace)
}
let touchPoint:UITouch = touch
let point = touchPoint.location(in: self)
var trace = Trace()
trace.arrPoints = Array<CGPoint>()
trace.arrPoints.append(point)
trace.color = UIColor.red
trace.thickness = 1
arrTraces?.append(trace)
self.setNeedsDisplay()
}

Expand All @@ -180,24 +178,22 @@ class DrawView: UIView {
if drawMode == .draw{
for touch in touches
{
if let touchPoint:UITouch = touch
{
let point = touchPoint.location(in: self)
//怎么找到上一个Point
let previousPoint = touchPoint.previousLocation(in: self)
var i = 0
while i < arrTraces?.count {
var tempTraces = arrTraces![i]
let lastPoint = tempTraces.arrPoints.last
if lastPoint == previousPoint
{
arrTraces![i].arrPoints.append(point)
break
}
i += 1
let touchPoint:UITouch = touch
let point = touchPoint.location(in: self)
//怎么找到上一个Point
let previousPoint = touchPoint.previousLocation(in: self)
var i = 0
while i < arrTraces?.count {
var tempTraces = arrTraces![i]
let lastPoint = tempTraces.arrPoints.last
if lastPoint == previousPoint
{
arrTraces![i].arrPoints.append(point)
break
}
self .setNeedsDisplay()
i += 1
}
self .setNeedsDisplay()
}
}
else{
Expand Down
76 changes: 59 additions & 17 deletions ViewChaos/ViewChaos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ extension UIWindow:UIActionSheetDelegate {
struct UIWindow_SwizzleToken {
init() {
Chaos.hookMethod(UIWindow.self, originalSelector: #selector(UIWindow.makeKeyAndVisible), swizzleSelector: #selector(UIWindow.vcMakeKeyAndVisible))
Chaos.hookMethod(UIView.self, originalSelector: #selector(UIView.willMoveToSuperview(_:)), swizzleSelector: #selector(UIView.vcWillMoveToSuperview(_:)))
Chaos.hookMethod(UIView.self, originalSelector: #selector(UIView.willMove(toSuperview:)), swizzleSelector: #selector(UIView.vcWillMoveToSuperview(_:)))

Chaos.hookMethod(UIView.self, originalSelector: #selector(UIView.willRemoveSubview(_:)), swizzleSelector: #selector(UIView.vcWillRemoveSubview(_:)))
Chaos.hookMethod(UIView.self, originalSelector: #selector(UIView.didAddSubview(_:)), swizzleSelector: #selector(UIView.vcDidAddSubview(_:)))
}
Expand All @@ -25,7 +26,7 @@ extension UIWindow:UIActionSheetDelegate {
return sharedInstance
}
}
var u = UIWindow_SwizzleToken.shareWindow
_ = UIWindow_SwizzleToken.shareWindow

}

Expand All @@ -51,17 +52,41 @@ extension UIWindow:UIActionSheetDelegate {
}
}

open override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) {

open override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) {
switch self.chaosFeature
{
case ChaosFeature.none.rawValue:
//这里放一个菜单
let menu = UIActionSheet(title: "使用功能", delegate: self, cancelButtonTitle:"取消", destructiveButtonTitle: nil)
menu.addButton(withTitle: "启用放大镜")
menu.addButton(withTitle: "显示边框")
menu.addButton(withTitle: "显示透明度")
menu.show(in: self)
let alert = UIAlertController(title: "使用功能", message: nil, preferredStyle: .actionSheet)
let action1 = UIAlertAction(title: "启用放大镜", style: .default, handler: { (action) in
NotificationCenter.default.post(name: NSNotification.Name(rawValue: setZoomViewWork), object: nil)
Chaos.toast("放大镜已经启用")
let view = ZoomViewBrace(frame: CGRect())
view.tag = -1000
self.insertSubview(view, at: 100)
self.chaosFeature = ChaosFeature.zoom.rawValue
})
let action2 = UIAlertAction(title: "显示边框", style: .default, handler: { (action) in
Chaos.toast("边框显示功能已经启用")
self.chaosFeature = ChaosFeature.border.rawValue
self.showBorderView(view: self)
let view = DrawView(frame: CGRect())
view.tag = -7000
self.insertSubview(view, at: 600)

})
let action3 = UIAlertAction(title: "显示透明度", style: .default, handler: { (action) in
Chaos.toast("透明显示功能已经启用")
self.chaosFeature = ChaosFeature.alpha.rawValue
self.showAlphaView(view: self)

})
let action4 = UIAlertAction(title: "取消", style: .cancel, handler: nil)
alert.addAction(action1)
alert.addAction(action2)
alert.addAction(action3)
alert.addAction(action4)
self.rootViewController?.present(alert, animated: true, completion: nil)
case ChaosFeature.zoom.rawValue:
UIAlertView.setMessage("关闭放大镜").addFirstButton("取消").addSecondButton("确定").alertWithButtonClick({ (buttonIndex, alert) -> Void in
if buttonIndex == 1{
Expand Down Expand Up @@ -99,6 +124,7 @@ extension UIWindow:UIActionSheetDelegate {


public func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {

if buttonIndex == 1{
NotificationCenter.default.post(name: NSNotification.Name(rawValue: setZoomViewWork), object: nil)
Chaos.toast("放大镜已经启用")
Expand Down Expand Up @@ -484,7 +510,7 @@ class ViewChaos: UIView {
override func touchesCancelled(_ touches: Set<UITouch>?, with event: UIEvent?) {
isTouch = false
viewBound.removeFromSuperview()
Chaos.delay(1.5) { () -> () in
let _ = Chaos.delay(1.5) { () -> () in
UIView.animate(withDuration: 0.5, animations: { () -> Void in
self.windowInfo.alpha = 0
}, completion: { (finished) -> Void in
Expand All @@ -496,7 +522,7 @@ class ViewChaos: UIView {
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
isTouch = false
viewBound.removeFromSuperview()
Chaos.delay(1.5) { () -> () in
let _ = Chaos.delay(1.5) { () -> () in
UIView.animate(withDuration: 0.5, animations: { () -> Void in
self.windowInfo.alpha = 0
}, completion: { (finished) -> Void in
Expand Down Expand Up @@ -719,31 +745,47 @@ extension NSObject{
}
}

private var UIWindow_Feature = 0
private var UIWindow_Feature = "UIWindow_Feature"

extension UIWindow{
@objc var chaosFeature:Int{
get{
return objc_getAssociatedObject(self, &UIWindow_Feature) as! Int
let v = objc_getAssociatedObject(self, &UIWindow_Feature)
if let str = v as? String{
if let n = Int(str)
{
return n
}
}
return 0
// return objc_getAssociatedObject(self, &UIWindow_Feature) as! Int
}
set{
objc_setAssociatedObject(self, &UIWindow_Feature, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
objc_setAssociatedObject(self, &UIWindow_Feature, "\(newValue)", objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
//Issue1, 要添加不同的类型的属性,就要设置正确的objc_AssociationPolicy,如果是Class,就要用OBJC_ASSOCIATION_RETAIN_NONATOMIC,Int要用OBJC_ASSOCIATION_ASSIGN,String要用OBJC_ASSOCIATION_COPY_NONATOMIC
//不然后可能会造成数据丢失或者其他异常
//注意objc_AssociationPolicy类型一定要正确,不然可能会从内存里丢失
//Issue 12: 在最新的Swift3里面,好像不能保存Int到AssociatedObject里面,反正每次都取不出来。我换成STRING 就OK了
}
}
}


private var UIView_Level = 1
private var UIView_Level = "UIView_Level"
extension UIView{
@objc var viewLevel:Int{
get{
return objc_getAssociatedObject(self, &UIView_Level) as! Int
let v = objc_getAssociatedObject(self, &UIView_Level)
if let str = v as? String{
if let n = Int(str)
{
return n
}
}
return 0
}
set{
objc_setAssociatedObject(self, &UIView_Level, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
objc_setAssociatedObject(self, &UIView_Level, "\(newValue)", objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions ViewChaos/ZoomView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class ZoomView: UIWindow {
let imgRect = CGRect(x: 0, y: 0, width: w, height: h)
context!.draw(imgRef!, in: imgRect)
let data = context!.data
imgCaptureData = UnsafeMutablePointer<CUnsignedChar>(data)
imgCaptureData = data!.assumingMemoryBound(to: CUnsignedChar.self)
//imgCaptureData = UnsafeMutablePointer<CUnsignedChar>(data)
}
}
}
Expand Down Expand Up @@ -246,7 +247,9 @@ extension UIImage{
let imgRect = CGRect(x: 0, y: 0, width: w, height: h)
context!.draw(imgRef!, in: imgRect)
let data = context!.data
let info = UnsafeMutablePointer<CUnsignedChar>(data)
// let info = UnsafeMutablePointer<CUnsignedChar>(data)

let info = data!.assumingMemoryBound(to: CUnsignedChar.self)
let offset:Int = Int(4 * ((w * round(point.x)) + round(point.x)))
let a = info[offset]
let r = info[offset + 1]
Expand Down
Binary file modified ViewChaosDemo/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
Expand Down

0 comments on commit a1e9c8e

Please sign in to comment.