Skip to content

Commit

Permalink
location feature is available
Browse files Browse the repository at this point in the history
  • Loading branch information
DuckDeck committed Nov 9, 2015
1 parent a5052ca commit 893533d
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 35 deletions.
8 changes: 8 additions & 0 deletions ViewChaos/ViewChaos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class ViewChaos: UIView {
windowInfo.addSubview(lblInfo)

NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleTraceView:", name: "handleTraceView", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleTraceViewClose:", name: "handleTraceViewClose", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleTraceContraints:", name: "handleTraceContraints", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleTraceAddSubView:", name: "handleTraceAddSubView", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleTraceShow:", name: "handleTraceShow", object: nil)
Expand Down Expand Up @@ -133,6 +134,13 @@ class ViewChaos: UIView {

}

func handleTraceViewClose(notif:NSNotification){
if viewNeat == nil{
return
}
viewNeat?.removeFromSuperview()
viewNeat = nil
}

func handleTraceAddSubView(notif:NSNotification){
if let viewSuper = notif.object
Expand Down
1 change: 1 addition & 0 deletions ViewChaos/ViewChaosInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ class ViewChaosInfo: UIView,UITableViewDataSource,UITableViewDelegate {
func close(sender:UIButton){
if btnClose.titleForState(UIControlState.Normal) == "Close"{
self.removeFromSuperview()
NSNotificationCenter.defaultCenter().postNotificationName("handleTraceViewClose", object: nil)
}
else if btnClose.titleForState(UIControlState.Normal) == "Stop"{
stopTrace()
Expand Down
128 changes: 100 additions & 28 deletions ViewChaos/ViewNeat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,84 @@
//

import UIKit

enum Neat:Int{
case location = 0,size
}
class ViewNeat: UIView {
var btnLocation:UIButton
var btnSize:UIButton
var viewControl:UIView?
var vLocaton:UIView
var vSize:UIView
var vFont:UIView
var vBorder:UIView
var vColor:UIView
var isTouch = false
var neatType:Neat = .location
var vRockerArea:UIView
var vRocker: UIView
var left,top:Float //右 上
var scaleX = 1
var scaleY = 1
var lblTopLeft:UILabel
var lblRightBotoom:UILabel
var timer:NSTimer?
init(){
btnLocation = UIButton()
btnSize = UIButton()
vLocaton = UIView()
vSize = UIView()
vFont = UIView()
vBorder = UIView()
vColor = UIView()
vRockerArea = UIView()
vRocker = UIView()
lblTopLeft = UILabel()
lblRightBotoom = UILabel()
left = 0
top = 0
super.init(frame:CGRect(x: 10, y: UIScreen.mainScreen().bounds.height - 150, width: UIScreen.mainScreen().bounds.width-20, height: 150))
timer = NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: "timerFire:", userInfo: nil, repeats: true)
btnLocation.frame = CGRect(x: 10, y: 100, width: 40, height: 40)
btnLocation.layer.cornerRadius = 20
btnLocation.backgroundColor = UIColor(red: 0.3, green: 0.3, blue: 0.3, alpha: 0.5)
btnLocation.setTitle("位置", forState: UIControlState.Normal)
btnLocation.addTarget(self, action: "showLocation:", forControlEvents: UIControlEvents.TouchUpInside)
addSubview(btnLocation)

vLocaton.frame = CGRectMake(10, 0, UIScreen.mainScreen().bounds.width-20, 100)
addSubview(vLocaton)

vRocker.frame = CGRect(x: UIScreen.mainScreen().bounds.width / 2 - 20, y: 40, width: 40, height: 40)

vRockerArea.frame = CGRect(x: self.frame.size.width / 2 - 50, y: 0, width: 100, height: 100)
vRockerArea.layer.borderWidth = 0.5
vRockerArea.layer.borderColor = UIColor.orangeColor().CGColor
addSubview(vRockerArea)

vRocker.frame = CGRect(x: vRockerArea.frame.size.width / 2 - 20, y: vRockerArea.frame.size.height / 2 - 20, width: 40, height: 40)
vRocker.layer.borderColor = UIColor(red: 0.3, green: 0.3, blue: 0.3, alpha: 0.5).CGColor
vRocker.layer.borderWidth = 0.5
vRocker.layer.cornerRadius = 20
vRocker.clipsToBounds = true
vRocker.backgroundColor = UIColor.redColor()
addSubview(vRocker)




vRockerArea.addSubview(vRocker)

btnSize.frame = CGRect(x: 70, y: 100, width: 40, height: 40)
btnSize.layer.cornerRadius = 20
btnSize.backgroundColor = UIColor(red: 0.3, green: 0.3, blue: 0.3, alpha: 0.5)
btnSize.setTitle("大小", forState: UIControlState.Normal)
btnSize.addTarget(self, action: "showSize:", forControlEvents: UIControlEvents.TouchUpInside)
addSubview(btnSize)





}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

let touch = touches.first
// let point = touch?.locationInView(self)
// left = Float(point!.x)
// top = Float(point!.y)
if let point = touch?.locationInView(vRocker)
{
if point.x > 0 && point.x < vRocker.frame.size.width && point.y > 0 && point.y < vRocker.frame.size.height{
isTouch = true
left = Float(point.x)
top = Float(point.y)
if timer!.valid{
timer?.fire()
timer?.resumeChaosTimer()
}
}
}
}
Expand All @@ -82,26 +95,64 @@ class ViewNeat: UIView {
return
}
let touch = touches.first
if let point = touch?.locationInView(self){
if let point = touch?.locationInView(vRockerArea){
// Chaos.Log("X:\(point.x) Y:\(point.y)")
//Chaos.Log("CehterX:\(vRockerArea.center.x) CenterY:\(vRockerArea.center.y)")
//限定在一个圆形范围内
let x = point.x - self.center.x
let y = point.y - self.center.y
if x * x + y*y >= 100{
// let x = point.x - 50
// let y = point.y - 50
// if x * x + y*y >= 250{
// return
// }
let newFrame = CGRect(x: point.x - CGFloat(left), y: point.y - CGFloat(top), width: vRocker.frame.size.width, height: vRocker.frame.size.height)
if newFrame.origin.x <= 0 || newFrame.origin.x >= 60 || newFrame.origin.y <= 0 || newFrame.origin.y >= 60{
return
}
self.frame = CGRect(x: point.x - CGFloat(left), y: point.y - CGFloat(top), width: self.frame.size.width, height: self.frame.size.height)
//self.vRocker.frame = CGRect(x: point.x - CGFloat(left), y: point.y - CGFloat(top), width: vRocker.frame.size.width, height: vRocker.frame.size.height)
//用这种方法创建摇杆全部失败,
self.vRocker.frame = newFrame
//换算到中间
let newPoint = CGPoint(x: point.x - 50, y: point.y - 50)
scaleX = Int(newPoint.x) / 5
scaleY = Int(newPoint.y) / 5
}
}

override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
isTouch = false
timer?.pauseChaosTimer()
UIView.animateWithDuration(0.2, animations: { () -> Void in
self.vRocker.frame = CGRect(x: self.vRockerArea.frame.size.width / 2 - 20, y: self.vRockerArea.frame.size.height / 2 - 20, width: 40, height: 40)
}) { (finished) -> Void in

}
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
isTouch = false
isTouch = false
timer?.pauseChaosTimer()
UIView.animateWithDuration(0.2, animations: { () -> Void in
self.vRocker.frame = CGRect(x: self.vRockerArea.frame.size.width / 2 - 20, y: self.vRockerArea.frame.size.height / 2 - 20, width: 40, height: 40)
}) { (finished) -> Void in

}

}


func timerFire(sender:NSTimer){
if !isTouch{
return
}
if viewControl != nil{
switch(neatType){
case .location:
Chaos.Log("scaleX:\(scaleX) scaleY:\(scaleY)")
viewControl!.frame = CGRect(x: viewControl!.frame.origin.x + CGFloat(scaleX) * 0.5, y: viewControl!.frame.origin.y + CGFloat(scaleY) * 0.5, width: viewControl!.frame.size.width, height: viewControl!.frame.size.height)
case .size: break
}
}
}

override convenience init(frame: CGRect) {
self.init()
}
Expand All @@ -110,8 +161,29 @@ class ViewNeat: UIView {
}

func showLocation(sender:UIButton){

neatType = .location
}





}

extension NSTimer{
func pauseChaosTimer(){ //这样可以尽量不导致冲突
if !self.valid
{
return
}
self.fireDate = NSDate.distantFuture()
}

func resumeChaosTimer(){
if !self.valid
{
return
}
self.fireDate = NSDate()
}
}
14 changes: 7 additions & 7 deletions ViewChaosDemo/ViewChaosDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
import UIKit

class ViewController: UIViewController {

var btn:UIButton?
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "ViewChaos"
view.backgroundColor = UIColor.whiteColor()
let btn = UIButton(frame: CGRect(x: 10, y: 100, width: 100, height: 30))
btn.backgroundColor = UIColor.redColor()
btn.setTitle("Next", forState: UIControlState.Normal)
btn.name = "btn"
btn.addTarget(self, action: "click:", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(btn)
btn = UIButton(frame: CGRect(x: 10, y: 100, width: 100, height: 30))
btn?.backgroundColor = UIColor.redColor()
btn?.setTitle("Next", forState: UIControlState.Normal)
btn?.name = "btn"
btn?.addTarget(self, action: "click:", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(btn!)
}

func click(sender:UIButton)
Expand Down

0 comments on commit 893533d

Please sign in to comment.