Skip to content

Commit

Permalink
All new features will completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Stan authored and Stan committed Mar 30, 2016
1 parent cd59279 commit ebf8f36
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 14 deletions.
217 changes: 216 additions & 1 deletion ViewChaos/DrawView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,223 @@
//

import UIKit

enum DrawMode {
case Draw,Capture
}
class DrawView: UIView {

var btnCaptureScreen = UIButton()
var btnCleanTrace = UIButton()
var drawMode = DrawMode.Draw
var pointOrigin = CGPointZero
var pointLast = CGPointZero
private var arrTraces:Array<Trace>?
override init(frame: CGRect) {
super.init(frame: frame)
arrTraces = [Trace]()
self.frame = UIScreen.mainScreen().bounds
self.backgroundColor = UIColor(red: 0.3, green: 0.3, blue: 0.3, alpha: 0.2)
btnCaptureScreen.frame = CGRect(x: UIScreen.mainScreen().bounds.size.width - 90, y: UIScreen.mainScreen().bounds.size.height - 25, width: 90, height: 25)
btnCaptureScreen.backgroundColor = UIColor(red: 0.0, green: 0.898, blue: 0.836, alpha: 0.5)
btnCaptureScreen.setTitle("启用截屏", forState: UIControlState.Normal)
btnCaptureScreen.addTarget(self, action: #selector(DrawView.captureScreenClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
addSubview(btnCaptureScreen)

btnCleanTrace.frame = CGRect(x: UIScreen.mainScreen().bounds.size.width - 150, y: UIScreen.mainScreen().bounds.size.height - 25, width: 50, height: 25)
btnCleanTrace.backgroundColor = UIColor(red: 0.0, green: 0.898, blue: 0.836, alpha: 0.5)
btnCleanTrace.setTitle("清理", forState: UIControlState.Normal)
btnCleanTrace.addTarget(self, action: #selector(DrawView.clearTraceClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
addSubview(btnCleanTrace)
btnCleanTrace.hidden = true
}



func captureScreenClick(sender:UIButton) {
//这里用截图功能
if let title = sender.titleForState(UIControlState.Normal)
{
if title == "启用截屏"{
Chaos.toast("现在开始截图")
drawMode = .Capture
self.backgroundColor = UIColor.clearColor()
btnCaptureScreen.setTitle("保存", forState: UIControlState.Normal)
}
else{
UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, true, 0)//这个应该是设定一个区域
UIApplication.sharedApplication().keyWindow!.layer.renderInContext(UIGraphicsGetCurrentContext()!)//将整个View渲染到Context里
let imgCapture = UIGraphicsGetImageFromCurrentImageContext()
UIImageWriteToSavedPhotosAlbum(imgCapture, self, nil, nil)
self.backgroundColor = UIColor(red: 0.3, green: 0.3, blue: 0.3, alpha: 0.2)
Chaos.toast("保存截图成功")
btnCaptureScreen.setTitle("启用截屏", forState: UIControlState.Normal)
clearTrace()
drawMode = .Draw
}
}
}



func clearTraceClick(sender:UIButton){
clearTrace()
}

func clearTrace() {
arrTraces?.removeAll()
btnCleanTrace.hidden = true
pointOrigin = CGPointZero
pointLast = CGPointZero
setNeedsDisplay()
}

override func drawRect(rect: CGRect) {

super.drawRect(rect)
let ctg = UIGraphicsGetCurrentContext()
CGContextClearRect(ctg, rect)
CGContextSetLineWidth(ctg, 1)
CGContextSetLineJoin(ctg, CGLineJoin.Round)
CGContextSetLineCap(ctg, CGLineCap.Round)
//画当前的线
if arrTraces?.count > 0
{
CGContextBeginPath(ctg)
for var i=0; i<arrTraces?.count;i += 1
{
var tempTrace = arrTraces![i]
if tempTrace.arrPoints.count > 0
{
CGContextBeginPath(ctg)
var point = tempTrace.arrPoints[0]
CGContextMoveToPoint(ctg, point.x, point.y)
let count:Int! = tempTrace.arrPoints.count
for j in 0 ..< count-1
{
point = tempTrace.arrPoints[j+1]
CGContextAddLineToPoint(ctg, point.x, point.y)
}
CGContextSetStrokeColorWithColor(ctg, tempTrace.color.CGColor)
let width:CGFloat! = tempTrace.thickness
CGContextSetLineWidth(ctg, width)
CGContextStrokePath(ctg)
}
}
}

if drawMode == .Capture{
CGContextSetFillColorWithColor(ctg, UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 0.3).CGColor)
CGContextSetStrokeColorWithColor(ctg, UIColor.redColor().CGColor)
// let dash:UnsafeMutablePointer<CGFloat> = UnsafeMutablePointer<CGFloat>.alloc(2)
// dash.initialize(1)
// dash.successor().initialize(1)
//
// CGContextSetLineDash(ctg, 0, dash, 2)
CGContextStrokeRect(ctg, CGRect(x: pointOrigin.x, y: pointOrigin.y, width: pointLast.x - pointOrigin.x, height: pointLast.y - pointOrigin.y))
CGContextFillRect(ctg, CGRect(x: pointOrigin.x, y: pointOrigin.y, width: pointLast.x - pointOrigin.x, height: pointLast.y - pointOrigin.y))
}
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
if drawMode == .Draw{
for touch in touches
{
if let touchPoint:UITouch = touch
{
let point = touchPoint.locationInView(self)
var trace = Trace()
trace.arrPoints = Array<CGPoint>()
trace.arrPoints.append(point)
trace.color = UIColor.redColor()
trace.thickness = 1
arrTraces?.append(trace)
}
self.setNeedsDisplay()
}

}
else{
if let touch = touches.first{
pointOrigin = touch.locationInView(self)
}
}
btnCleanTrace.hidden = false
}


override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesMoved(touches, withEvent: event)
if drawMode == .Draw{
for touch in touches
{
if let touchPoint:UITouch = touch
{
let point = touchPoint.locationInView(self)
//怎么找到上一个Point
let previousPoint = touchPoint.previousLocationInView(self)
for var i=0;i<arrTraces?.count;i += 1
{
var tempTraces = arrTraces![i]
let lastPoint = tempTraces.arrPoints.last
if lastPoint == previousPoint
{
arrTraces![i].arrPoints.append(point)
break
}
}
self .setNeedsDisplay()
}
}
}
else{
if let touch = touches.first{
pointLast = touch.locationInView(self)
setNeedsDisplay()
}
}
}



required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}
struct Trace {
private var _arrPoints:Array<CGPoint>?
private var _color:UIColor = UIColor.whiteColor()
private var _thickness:CGFloat = 1

var arrPoints:Array<CGPoint>{
mutating get{
if _arrPoints == nil
{
_arrPoints = Array<CGPoint>()
}
return _arrPoints!
}
set{
_arrPoints = newValue
}
}

var color:UIColor{
get{
return _color
}
set{
_color = newValue
}
}

var thickness:CGFloat{
get{
return _thickness
}
set{
_thickness = newValue
}
}
}
35 changes: 23 additions & 12 deletions ViewChaos/ViewChaos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import UIKit
enum ChaosFeature:Int{
case None=0,Zoom,Border,Alpha,Draw
case None=0,Zoom,Border,Alpha
}
extension UIWindow:UIActionSheetDelegate {
#if DEBUG
Expand Down Expand Up @@ -46,7 +46,6 @@ extension UIWindow:UIActionSheetDelegate {
menu.addButtonWithTitle("启用放大镜")
menu.addButtonWithTitle("显示边框")
menu.addButtonWithTitle("显示透明度")
menu.addButtonWithTitle("画画截图")
menu.showInView(self)
case ChaosFeature.Zoom.rawValue:
UIAlertView.setMessage("关闭放大镜").addFirstButton("取消").addSecondButton("确定").alertWithButtonClick({ (buttonIndex, alert) -> Void in
Expand Down Expand Up @@ -77,7 +76,8 @@ extension UIWindow:UIActionSheetDelegate {
self.removeAlphaView(self)
}
})
default:break

default:break
}

}
Expand All @@ -97,17 +97,16 @@ extension UIWindow:UIActionSheetDelegate {
Chaos.toast("边框显示功能已经启用")
self.chaosFeature = ChaosFeature.Border.rawValue
showBorderView(self)
let view = DrawView(frame: CGRectZero)
view.tag = -7000
self.insertSubview(view, atIndex: 600)

}
if buttonIndex == 3{
Chaos.toast("边框显示功能已经启用")
Chaos.toast("透明显示功能已经启用")
self.chaosFeature = ChaosFeature.Alpha.rawValue
showAlphaView(self)
}
if buttonIndex == 3{
Chaos.toast("画画截图功能已经启用")
self.chaosFeature = ChaosFeature.Draw.rawValue
//等会再做
}
}

private func showBorderView(view:UIView){
Expand Down Expand Up @@ -137,6 +136,9 @@ extension UIWindow:UIActionSheetDelegate {
if v.tag == -5000{
v.removeFromSuperview()
}
if view.tag == -7000{
view.removeFromSuperview()
}
}//第二个功能完成
}

Expand All @@ -147,11 +149,20 @@ extension UIWindow:UIActionSheetDelegate {
print("view:\(v.dynamicType) level:\(v.viewLevel) alpha:\(v.alpha)")
//Issue11 这个功能目前只能适用于Alpha属性
//对于那个对于背景颜色的透明属性是无法完成的,所以用处不算大
if v.viewLevel > 4{
//实际上光用alpha是不够的,这样对于background无法获取
//所以在这里在判断
if v.viewLevel > 3{
let fm = v.convertRect(v.bounds, toView: self)
let vBorder = UIView(frame: fm)
vBorder.tag = -6000
vBorder.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1 - v.alpha)
var alp = 1 - v.alpha
if alp == 0{
//这个时侯获取background的透明度
if let co = v.backgroundColor{
alp = 1 - CGFloat(co.alpha) / 255.0
}
}
vBorder.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: alp / 2)//除以2更好辨认一点
self.insertSubview(vBorder, atIndex: 600)
}
showAlphaView(v)
Expand All @@ -164,7 +175,7 @@ extension UIWindow:UIActionSheetDelegate {
if v.tag == -6000{
v.removeFromSuperview()
}
}
}//第三个功能完成
}

#endif
Expand Down
10 changes: 10 additions & 0 deletions ViewChaos/ViewNeat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,16 @@ extension UIColor{
return self.description
}
}

var alpha:Int{
var red:CGFloat = 0.0
var green:CGFloat = 0.0
var blue:CGFloat = 0.0
var a:CGFloat = 0.0
self.getRed(&red, green: &green, blue: &blue, alpha: &a)
return Int(a * 255)
}

}


Expand Down
2 changes: 1 addition & 1 deletion ViewChaosDemo/ViewChaosDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ViewController: UIViewController {
self.navigationItem.title = "ViewChaos"
view.backgroundColor = UIColor.whiteColor()
btn = UIButton(frame: CGRect(x: 10, y: 100, width: 100, height: 30))
btn?.backgroundColor = UIColor.redColor()
btn?.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0.2)
btn?.setTitle("Next", forState: UIControlState.Normal)
btn?.chaosName = "btn"
btn?.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents.TouchUpInside)
Expand Down

0 comments on commit ebf8f36

Please sign in to comment.